I'm making a programme where an LCD has 4 responses and pressing a switch cycles through them.
I found when I plugged the arduino in the initial message before the switch was pressed was fine. But when i pressed the switch the actual text on the LCD kept flashing and wouldn't stay solid. If the switch was pressed again it displayed the next message but it also wouldn't stay solid. This happened for all of the responses apart from the 4th. When the switch was pressed to change to the 4th response it wouldn't stay solid, but if the switch was pressed again it became solid. If the switch was pressed again after that it went back to the 1st response as it was supposed to, but it was still flashing.
This is the script:
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int counter = 0;
int prevSwitchState = 0;
int reply;
void setup(){
lcd.begin(16,2);
pinMode(switchPin,INPUT);
lcd.print("Press the button");
lcd.setCursor(0,1);
lcd.print("To begin!");
}
void loop(){
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState){
if (switchState == HIGH) {
counter = counter + 1;
}
if (counter == 5) {
counter = 0;
}
}
if (counter == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("1 blah blah");
lcd.setCursor(0,1);
lcd.print("1 blah blah");
}
if (counter == 2){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("2 blah blah");
lcd.setCursor(0,1);
lcd.print("2 blah blah");
}
if (counter == 3){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("3 blah blah");
lcd.setCursor(0,1);
lcd.print("3 blah blah");
}
if (counter == 4){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("4 blah blah");
lcd.setCursor(0,1);
lcd.print("4 blah blah");
}
prevSwitchState = switchState;
}
Any help or ideas as to the cause of the problem would be greatly appreciated.
Thanks for replying.
I tried the changes you suggested and it solved the problem of the LCD blinking but it now doesn't respond if the switch is pressed a second time.
When I uploaded the programme it displayed the initial message without blinking. If i then pressed switch it displayed the first message without blinking. But after that pressing the switch did nothing.
lcd.clear(); is like a sledge hammer. Its a great tool but it has to be used wisely especially on very short fast programs. By the time the info has been passed to the lcd the clear is probably already sent to wipe the display.
if the counter didn't change then the info being sent to the lcd didn't change so add something like above the counter "ifs".
if (counter != prevcounter){//data did not change
lcd.clear();
}
remove all the other lcd.clear(); from the counter "ifs"
and at the bottom put
prevcounter = counter;
now the lcd will only be cleared if the counter has changed and new info is being written