Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include the library code:
- #include <Wire.h>
- byte inData;
- char inChar;
- String BuildINString="";
- String DisplayString="";
- String WorkingString="";
- long DisplayValue;
- long A;
- int B;
- unsigned long startMillis1; //some global variables available anywhere in the program
- unsigned long currentMillis1;
- unsigned long startMillis2; //some global variables available anywhere in the program
- unsigned long currentMillis2;
- const unsigned long period1 = 10000; //the value is a number of milliseconds
- const unsigned long period2 = 30000; //the value is a number of milliseconds
- bool warmup = true;
- void setup() {
- startMillis1 = millis(); //initial start time
- startMillis2 = millis(); //initial start time
- //Display Welcome Message
- delay(3000);
- //********************************************************************
- //Added new code to check if ELM327 responded
- Retry:
- //Set up Serial communication at 9600 baud
- Serial.begin(9600); //initialize Serial
- Serial.println("ATZ");
- delay(2000);
- ReadData();
- // If used substring(1,4)=="ATZ" needed a space before ATZ in Serial Monitor and it did not work
- if (BuildINString.substring(1,3)=="TZ") // MIATA RESPONSE TO ATZ IS ATZ[[[ELM327 V1.5 OR AT LEAST THAT IS WHAT ARDUINO HAS IN THE BUFFER
- {
- //lcd.print(BuildINString); //Echo response to screen "Welcome ELM327"
- delay(1500);
- }
- else
- {
- delay(1500);
- goto Retry;
- }
- //*****************************************************************
- //Added this from the test Code
- Serial.println("0100"); // Works with This only
- lcd.setCursor(0, 0);
- lcd.print("Initialzing....."); //Initialize BUS //lcd.print("0100 Sent");
- delay(4000);
- ReadData();
- lcd.setCursor(0, 0); //Added 12-10-2016
- lcd.print("Initialized....."); //Added 12-10-2016
- delay(1000);
- lcd.clear();
- Serial.println("0105");
- delay(250);
- ReadData();
- WorkingString = BuildINString.substring(11,13);
- A = strtol(WorkingString.c_str(),NULL,16); //convert hex to decimnal
- A = A-40;
- if (A>80) {warmup = true;} else {warmup = false;}
- }
- void loop() {
- BuildINString = "";
- currentMillis1 = millis(); //get the current "time" (actually the number of milliseconds since the program started)
- if (currentMillis1 - startMillis1 >= period1) //test whether the period has elapsed
- {
- ReadTemp();
- startMillis1 = currentMillis1; //IMPORTANT to save the start time of the current LED state.
- }
- currentMillis2 = millis(); //get the current "time" (actually the number of milliseconds since the program started)
- if (currentMillis2 - startMillis2 >= period2) //test whether the period has elapsed
- {
- ReadVoltage();
- startMillis2 = currentMillis2; //IMPORTANT to save the start time of the current LED state.
- }
- ReadRPM();
- ReadSpeed();
- }
- }
- //FUNKCJE FUNKCJE FUNKCJE
- void ReadData()
- {
- BuildINString="";
- while(Serial.available() > 0)
- {
- inData=0;
- inChar=0;
- inData = Serial.read();
- inChar=char(inData);
- BuildINString = BuildINString + inChar;
- }
- }
- void ReadTemp()
- {
- Serial.println("0105");
- delay(250);
- ReadData();
- WorkingString = BuildINString.substring(11,13);
- A = strtol(WorkingString.c_str(),NULL,16); //convert hex to decimnal
- DisplayValue = A;
- DisplayString = String(DisplayValue - 40) + " C"; // Subtract 40 from decimal to get the right temperature
- lcd.setCursor(11, 1);
- lcd.print(DisplayString);
- }
- void ReadVoltage()
- {
- Serial.println("AT RV");
- delay(250);
- ReadData();
- WorkingString = BuildINString.substring(6,11);
- DisplayString = WorkingString;
- lcd.setCursor(11, 0);
- lcd.print(DisplayString);
- }
- void ReadRPM()
- {
- Serial.println("010C");
- delay(250);
- ReadData();
- WorkingString = BuildINString.substring(11,13);
- A = strtol(WorkingString.c_str(),NULL,16);
- WorkingString = BuildINString.substring(14,16);
- B = strtol(WorkingString.c_str(),NULL,16);
- DisplayValue = ((A * 256)+B)/4;
- DisplayString = String(DisplayValue) + " RPM ";
- lcd.setCursor(0, 0);
- lcd.print(DisplayString);
- }
- void ReadSpeed()
- {
- Serial.println("010D");
- delay(250);
- ReadData();
- WorkingString = BuildINString.substring(11,13);
- A = strtol(WorkingString.c_str(),NULL,16);
- DisplayValue = A;
- DisplayString = String(DisplayValue) + "km/h ";
- lcd.setCursor(0, 1);
- lcd.print(DisplayString);
- }
Advertisement
Add Comment
Please, Sign In to add comment