04.06.2011, 20:15
Sooo, hier der Code für ein Getriebeöl-Thermometer (wie gehabt 4x20 Display, ELM an Serial1, Debug-Messages an Serial). Für eigene Konstruktion müssen die Display-Portbits geändert werden und bei einem Nicht-Mega alles "Serial1.xxx" gegen Serial.xxx ersetzt werden.
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(22, 23, 24, 25, 26, 27, 28); // Mega
int backLight = 29;
char buffer[24] ; int a = 0; int b = 0; int pointer = 0;
void setup() {
pinMode(backLight, OUTPUT); digitalWrite(backLight, HIGH); // Licht an!
lcd.begin(4,20); lcd.clear();
Serial.begin(9600); Serial1.begin(9600); // Serialports konfigurieren
Serial1.print("at z"); Serial1.print((char)13);
delay(1000);
elm2usb();
Serial1.print("AT E0"); Serial1.print((char)13);
delay(1000);
elm2usb();
Serial1.print("ATH1"); Serial1.print((char)13);
delay(1000);
elm2usb();
Serial1.print("ATSHE93A60"); Serial1.print((char)13);
delay(1000);
elm2usb();
}
int hex2dec(byte x) {
if (x >= '0' && x <= '9') { return x - '0'; } else if (x >= 'A' && x <= 'F') { return x - 'A' + 10; }
}
void elm2usb() { // alles was vom ELM kommt an die USB-Schnittstelle
while (Serial1.available() < 1) {}
delay(200);
while (Serial1.available() >0) { Serial.write(Serial1.read()); }
}
int gearOil() {
pointer = 0; a = 0; b = 0;
Serial1.flush();
Serial1.print("10 C6"); Serial1.print((char)13);
while (Serial1.available() < 3) {}
delay(100);
while (Serial1.available() >0) {
buffer[pointer] = Serial1.read();
Serial.print(buffer[pointer]); // DEBUG
pointer++; }
Serial1.flush();
a = hex2dec(buffer[13]) + hex2dec(buffer[12]) * 16;
return (a-40); // 40 Grad offset
}
void loop() {
lcd.setCursor(0,0); lcd.print("Getriebe: C ");
lcd.setCursor(10,0); lcd.print(gearOil());
delay(2000);
}