Hi all,
I have a 20x4 LCD connected to an Arduino Uno. The following sketch displays all characters as expected:
#include <LiquidCrystal.h>;
LiquidCrystal lcd(12, 11, 5, 4, 1, 0);
void setup() {
lcd.begin(20,4);
lcd.setCursor(0,0);
lcd.print("Solltemp. (\337C):");
lcd.setCursor(0,1);
lcd.print("Isttemp. (\337C):");
lcd.setCursor(0,2);
lcd.print("Sollzeit (min):");
lcd.setCursor(0,3);
lcd.print("Restzeit (min):");
}
void loop() {
}
Then I inserted this code into my project:
// Tasten zum Einstellen eines Wertes, der im EEPROM abgelegt wird
//
// Matthias Busse Version 1.0 vom 28.10.2014
#include <LiquidCrystal.h>
#include <EEPROM.h>
LiquidCrystal lcd(12, 11, 5, 4, 1, 0);
int LED=13, lampe=6, temp_soll=130, caladr=1, speichern=0;
volatile unsigned long alteZeit=0, entprellZeit=20, cal=0;
void setup() {
Serial.begin(38400);
pinMode(LED, OUTPUT); // LED Pin
pinMode(2, INPUT); // Pin 2 ist INT0
digitalWrite(2, HIGH); // interner Pull up Widerstand auf 5V
attachInterrupt(0, interruptRoutineTemp, LOW);
// Pin 2 (INT 0) geht auf LOW (0V) dann interruptRoutine aufrufen
pinMode(A0,INPUT); // Taste 0 hoch
digitalWrite(A0,HIGH); // interner pullup Widerstand
pinMode(A1,INPUT); // Taste 1 runter
digitalWrite(A1,HIGH);
temp_soll=eepromReadInt(caladr); // Startwert aus dem EEPROM lesen
lcd.begin(20,4);
lcd.setCursor(0,0);
lcd.print("Solltemp. (\337C):");
lcd.setCursor(0,1);
lcd.print("Isttemp. (\337C):");
lcd.setCursor(0,2);
lcd.print("Sollzeit (min):");
lcd.setCursor(0,3);
lcd.print("Restzeit (min):");
}
void loop() {
lcd.setCursor(16,0);
if(cal==1) {
if(readButton(0)) temp_soll+=1; // temp_soll höher
if(readButton(1)) temp_soll-=1; // temp_soll niedriger
lcd.print(temp_soll);
if(temp_soll > 150) temp_soll=150; // die obere Grenze
if(temp_soll < 20) temp_soll=20; // die untere Grenze
speichern=1; // Wert verändert, bei verlassen der cal > speichern
}
if(speichern==1) { // Wert soll gespeichert werden
eepromWriteInt(caladr, temp_soll);
speichern=0; // Wert wurde gespeichert
}
delay(100);
}
int readButton(int pin) { // Taste einlesen
if(analogRead(pin) < 500) { // Analog Eingang abfragen
delay(entprellZeit);
if(analogRead(pin) < 500)
return 1; // war gedrückt
}
return 0; // war nicht gedrückt
}
void interruptRoutineTemp() {
if((millis() - alteZeit) > entprellZeit) {
// innerhalb der entprellZeit nichts machen
digitalWrite(LED, !digitalRead(LED)); // LED umschalten
alteZeit = millis(); // letzte Schaltzeit merken
cal = !cal; // Einstellmodus wechseln
}
}
int eepromReadInt(int adr) {
// Integer aus dem EEPROM lesen
byte low, high;
low=EEPROM.read(adr);
high=EEPROM.read(adr+1);
return low + ((high << 8)&0xFF00);
} //eepromReadInt
void eepromWriteInt(int adr, int wert) {
// Integer in das EEPROM schreiben
byte low, high;
low=wert&0xFF;
high=(wert >> 8)&0xFF;
EEPROM.write(adr, low); // dauert 3,3ms
EEPROM.write(adr+1, high);
return;
} //eepromWriteInt
The 4 lines of the first sketch should appear in the LCD,
then, at position 16,0 the value of temp_soll should be displayed. The rest of the project is not ready yet, but it should work until here.
But all I get are 14 strange characters in the first row (see attachment). When I press the button of D2 the interrupt is called, I notice it because the led lights up.
When I press button 1 or 2 (when called the interrupt), the first characters are changing.
I have no idea what's going wrong here. Any ideas?
Thank you,
-richard