#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
byte inByte1 = 0; // incoming serial byte1 -- header
byte inByte2 = 0; // incoming serial byte2 -- header
byte inByte3 = 0; // incoming serial byte3 -- word1
byte inByte4 = 0; // incoming serial byte4 -- word1
byte inByte5 = 0; // incoming serial byte5 -- word2
byte inByte6 = 0; // incoming serial byte6 -- word2
byte B = 0; // number of packets
word AF = 0; //stoichiometric AFR value of current fuel setting in the LM-1
unsigned int L = 0; // Lambda value
unsigned int LPrev = 0;
byte F = 0; // Function value
byte FPrev = 0; // Function value
byte NoData = 0;
void setup() {
Serial.begin(19200);
lcd.begin(16, 2);
lcd.print("TOYOTA MARK 110 ");
delay(6000);
}
void loop() {
if (Serial.available() > 6) { //checks if data on serial coonection are available
inByte1 = Serial.read(); //reads first byte of data
if (bitRead(inByte1, 7) && bitRead(inByte1, 5) && bitRead(inByte1, 1)) { //checks if inByte1 is header of data packet
inByte2 = Serial.read(); //reads second byte of data
if (bitRead(inByte2, 7)) { //checks if inByte2 is second byte of header
bitWrite(B, 0, bitRead(inByte2, 0));
bitWrite(B, 1, bitRead(inByte2, 1));
bitWrite(B, 2, bitRead(inByte2, 2));
bitWrite(B, 3, bitRead(inByte2, 3));
bitWrite(B, 4, bitRead(inByte2, 4));
bitWrite(B, 5, bitRead(inByte2, 5));
bitWrite(B, 6, bitRead(inByte2, 6));
bitWrite(B, 7, bitRead(inByte1, 0)); //calculates number of packets from header
if (B > 1) { //checks if packet consists any data except header
inByte3 = Serial.read(); //read afr packet
inByte4 = Serial.read(); //read afr packet
bitWrite(F, 0, bitRead(inByte3, 2));
bitWrite(F, 1, bitRead(inByte3, 3));
bitWrite(F, 2, bitRead(inByte3, 4)); //calculates wb controller status variable
bitWrite(AF, 0, bitRead(inByte4, 0));
bitWrite(AF, 1, bitRead(inByte4, 1));
bitWrite(AF, 2, bitRead(inByte4, 2));
bitWrite(AF, 3, bitRead(inByte4, 3));
bitWrite(AF, 4, bitRead(inByte4, 4));
bitWrite(AF, 5, bitRead(inByte4, 5));
bitWrite(AF, 6, bitRead(inByte4, 6));
bitWrite(AF, 7, bitRead(inByte3, 0)); //calculates afr which is defined as stechoimetric
inByte5 = Serial.read(); //read lambda packet
inByte6 = Serial.read(); //read lambda packet
bitWrite(L, 0, bitRead(inByte6, 0));
bitWrite(L, 1, bitRead(inByte6, 1));
bitWrite(L, 2, bitRead(inByte6, 2));
bitWrite(L, 3, bitRead(inByte6, 3));
bitWrite(L, 4, bitRead(inByte6, 4));
bitWrite(L, 5, bitRead(inByte6, 5));
bitWrite(L, 6, bitRead(inByte6, 6));
bitWrite(L, 7, bitRead(inByte5, 0));
bitWrite(L, 8, bitRead(inByte5, 1));
bitWrite(L, 9, bitRead(inByte5, 2));
bitWrite(L, 10, bitRead(inByte5, 3));
bitWrite(L, 11, bitRead(inByte5, 4));
bitWrite(L, 12, bitRead(inByte5, 5)); //calculates lambda value
if (F != FPrev) update_F(); //checks if status changed since last iteration and if it so runs screen update for status
if (L != LPrev) update_L(); //checks if lambda changed since last iteration and if it so runs screen update for lambda
FPrev = F;
LPrev = L; //saves last state of lambda and status
NoData = 0; //clears counter which is needed to determine absence of incoming data
} //if (B>1)
} //if (bitRead(inByte2,7))
} // if (bitRead(inByte1,7) && bitRead(inByte1,5) && bitRead(inByte1,1) )
} //if (Serial.available() > 6)
else NoData++;
if (NoData > 15) { //checks if last 15 iterations were not incoming data and display it on screen if it so.
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("NO SIGNAL");
NoData = 0;
}
Serial.flush(); //clears serial buffer in order to avoid buffer overflow
delay(10);
//new_comment1
} //void loop()
void update_F() //update status on screen
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Status:");
lcd.setCursor(8, 0);
switch (F) {
case 0: //Lambda valid and Aux data valid, normal operation.
lcd.print("OK");
lcd.setCursor(0, 1);
lcd.print("AirFlow:");
break;
case 1: //Lambda value contains O2 level in 1/10%
lcd.print("Too lean");
lcd.setCursor(0, 1);
lcd.print("%O2:");
break;
case 2: //Free air Calib in progress, Lambda data not valid
lcd.print("Calibration");
lcd.setCursor(0, 3);
lcd.print(L);
break;
case 3: //Need Free air Calibration Request, Lambda data not valid
lcd.print("Need cali");
break;
case 4: //Warming up, Lambda value is temp in 1/10% of operating temp.
lcd.print("PROGREV");
lcd.setCursor(0, 1);
lcd.print("Progress:");
lcd.setCursor(12, 1);
lcd.print("%");
break;
case 5: //Heater Calibration, Lambda value contains calibration countdown.
lcd.print("Heater Cali");
break;
case 6: //Error code in Lambda value
lcd.print("Error");
lcd.setCursor(0, 1);
lcd.print("Code:");
break;
case 7: //reserved
break;
} //switch (F)
}
void update_L() //update lambda level on screen
{
switch (F) {
case 0: //Lambda valid and Aux data valid, normal operation.
lcd.setCursor(5, 1);
lcd.print((0.5 + (L * 0.001)) * (AF / 10));
lcd.setCursor(0, 2);
break;
case 1: //Lambda value contains O2 level in 1/10%
lcd.setCursor(5, 1);
lcd.print(L / 10.0);
break;
case 2: //Free air Calib in progress, Lambda data not valid
break;
case 3: //Need Free air Calibration Request, Lambda data not valid
lcd.setCursor(0, 3);
lcd.print(L);
break;
case 4: //Warming up, Lambda value is temp in 1/10% of operating temp.
lcd.setCursor(10, 1);
lcd.print(L / 10);
break;
case 5: //Heater Calibration, Lambda value contains cal ibration countdown.
lcd.setCursor(0, 1);
lcd.print(L);
break;
case 6: //Error code in Lambda value
lcd.setCursor(6, 1);
lcd.print(L);
break;
case 7: //reserved
break;
} //switch (F)
}