I am building an alarm clock that brews coffee. Before when I had the code partially completed, the LCD screen displayed very well. Now that I added the function of debouncing two more buttons, moving a servo and showing the time using an RTC, the LCD shows all garbled text. I have attached an image of what it displays. I suspect that it is a coding issue because I uploaded the example sketch and it worked. can someone please look at my code and see whats wrong. Thank you soososos much for the help.
Sorry, haven't looked at the code (yet) - it is inconvenient when posted as an attachment.
Can you post it in a "code" box (the "scroll" icon above the Post Reply window) ?
Here's the question - do you have a copy saved of the "last working" code? That would be the thing to work from- and if you do not, then you have learned a very important lesson.
Unfortunetly no i don't have a copy of my last working code. Haha yes it is a very important lesson learned. I have put my code below and im very sorry since it is very messy.
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <Servo.h>
RTC_DS1307 RTC;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
boolean coffeeLastButton = LOW;
boolean coffeeCurrentButton = LOW;
int coffeeMenu = 0;
int selectionButtonOne = 0; //INSERT PIN NUMBER HERE
boolean selectionButtonOneLast = LOW; //Used for debouncing
boolean selectionButtonOneCurrent = LOW; //Used for debouncing
int selectionButtonTwo = 0;//INSERT PIN NUMBER HERE
boolean selectionButtonTwoLast = LOW; //Used for debouncing
boolean selectionButtonTwoCurrent = LOW; //Used for debouncing
int alarmHour = 0;
int alarmMin = 0;
int coffeeChoice = 0;
Servo FREservo;
Servo COLservo;
void setup(){
FREservo.attach(8);/*INSERT PIN NUMBER HERE*/
COLservo.attach(9);/*INSERT PIN NUMBER HERE*/
FREservo.write(0);
COLservo.write(0);
pinMode(6, INPUT_PULLUP);
pinMode(selectionButtonOne, INPUT_PULLUP);
pinMode(selectionButtonTwo, INPUT_PULLUP);
Serial.begin(9600);
Wire.begin();
RTC.begin();
lcd.begin(16, 2);
if (! RTC.isrunning()) {
//lcd.setCursor(0, 0);
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
boolean menudebounce(boolean coffeeLast){ //This section debounces the Menu button
boolean coffeeCurrent = digitalRead(6);
if (coffeeLast != coffeeCurrent){
delay(5);
coffeeCurrent = digitalRead(6);
}
return coffeeCurrent;
}
boolean buttonOnedebounce(boolean OneLast){ //This section debounces the SelectionOne button
boolean OneCurrent = digitalRead(selectionButtonOne);
if (OneLast != OneCurrent){
delay(5);
OneCurrent = digitalRead(selectionButtonOne);
}
return OneCurrent;
}
boolean buttonTwodebounce(boolean TwoLast){ //This section debounces the SelectionOne button
boolean TwoCurrent = digitalRead(selectionButtonTwo);
if (TwoLast != TwoCurrent){
delay(5);
TwoCurrent = digitalRead(selectionButtonTwo);
}
return TwoCurrent;
}
void loop(){
coffeeCurrentButton = menudebounce(coffeeLastButton);
selectionButtonOneCurrent = buttonOnedebounce(selectionButtonOneLast);
selectionButtonTwoCurrent = buttonTwodebounce(selectionButtonTwoLast);
if(coffeeLastButton == LOW && coffeeCurrentButton == HIGH){
coffeeMenu = coffeeMenu++;
coffeeLastButton = HIGH;
}
coffeeLastButton = coffeeCurrentButton;
if(selectionButtonOneLast == LOW && selectionButtonOneCurrent == HIGH){
selectionButtonOne = !selectionButtonOne;
selectionButtonOneLast = HIGH;
}
selectionButtonOneLast = selectionButtonOneCurrent;
if(selectionButtonTwoLast == LOW && selectionButtonTwoCurrent == HIGH){
selectionButtonTwo = !selectionButtonTwo;
selectionButtonTwoLast = HIGH;
}
selectionButtonTwoLast = selectionButtonTwoCurrent;
if(coffeeMenu == 3){ //Limits Menu button to values 0, 1, 2
coffeeMenu = 0;
}
Serial.println(coffeeMenu);
if(coffeeMenu == 2){
lcd.setCursor(0, 0);
lcd.print (alarmHour);
lcd.print (":");
lcd.print (alarmMin);
}
if(alarmHour == 24){
alarmHour = 0;
}
if(alarmMin == 60){
alarmMin = 0;
}
while(coffeeMenu == 2){
if(selectionButtonOne = HIGH){
alarmHour++;
}
}
while(coffeeMenu == 2){
if(selectionButtonTwo = HIGH){
alarmMin++;
}
}
DateTime now = RTC.now();
DateTime future (now.unixtime() + alarmHour * 3600 + alarmMin * 60);
if(now.unixtime() == future.unixtime()){ //This section says when now timestamp == to alarm timestamp make an action happen
}
if(coffeeMenu == 1 && coffeeChoice == 0){
lcd.setCursor(0, 0);
lcd.print("Coffee Settings");
lcd.setCursor(0, 2);
lcd.print("FRE COL");
}
else{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
}
while(coffeeMenu == 1 && coffeeChoice == 0){
if(selectionButtonOne == HIGH){
coffeeChoice = 1;
}
if(selectionButtonTwo == HIGH){
coffeeChoice = 2;
}
}
if(coffeeChoice = 1){
FREservo.write(180);
if(coffeeMenu == 1){
lcd.setCursor(0, 0);
lcd.print("Coffee Settings");
lcd.setCursor(0, 2);
lcd.print("FRE<-- COL");
}
else{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
}
}
if(coffeeChoice = 2){ //if the coffee choice is Columbian then print the screen indicating the choice and drop in the coffee grounds using the servo
COLservo.write(180);
if(coffeeMenu == 1){
lcd.setCursor(0, 0);
lcd.print("Coffee Settings");
lcd.setCursor(0, 2);
lcd.print("FRE -->COL");
}
else{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
}
}
while(coffeeChoice > 0){ //This section says if a coffe is chosen make sure that the buttons do nothing
if(selectionButtonOne = HIGH){
}
if(selectionButtonTwo = HIGH){
}
}
if(coffeeMenu == 0){
lcd.setCursor(0, 0);
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(' ');
lcd.setCursor(0, 1);
if (now.hour()<10)
lcd.print('0');
lcd.print(now.hour(), DEC);
lcd.print(':');
if (now.minute()<10)
lcd.print('0');
lcd.print(now.minute(), DEC);
lcd.print(':');
if (now.second()<10)
lcd.print('0');
lcd.print(now.second(), DEC);
/* lcd.setCursor(0, 3);
int dayofweek = now.dayOfWeek();
switch(dayofweek){
case 1:
lcd.print("Monday");
break;
case 2:
lcd.print("Tuesday");
break;
case 3:
lcd.print("Wednesday");
break;
case 4:
lcd.print("Thursday");
break;
case 5:
lcd.print("Friday");
break;
case 6:
lcd.print("Saturday");
break;
case 0:
lcd.print("Sunday");
break;
delay(1000);
}
*/
}
else{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
}
}
How are you powering the servo?
Try just disconnecting it and see if it works better. You normally need a seprate supply for a servo due to the noise it generates.
But what have you connected? Disconnect everything. If that doesn't work, start to comment out everything that's new/not needed for the LCD until it works again.
I have the RTC and the LCD connected, thats it. I unplugged the RTC and it still continued the same way. Tomorrow in class I will try to comment out every thing that is new.
Hey, I just found one problem. Unfortunately, another problem has arose. I realised that I had attached one of my servos to a pin that was connected to the LCD. Now the garbled text is gone but my menu button doesn't change the menu on the LCD.
I've just finished similar project. I call it WonderClock. It's an alarm clock with the capability to activate two external loads.
With regards to the garbled display on your LCD, I had a similar issue. Mine was cause by wrong subscript.
Here is a code snippet that will definitely garble LCD display:
char aMyArray[3] = {"Element1","Element2","Element3"};
int nSubscript = 0;
nSubscript--;
lcd.print(aMyArray[nSubscript]);
What happened here is that the nSubscript variable became -1 after the nSubscript-- operation. The only valid values that variable nSubscript can have is 0, 1 and 2. Below and above this, it will be out of bounds and Arduino tells us so in a cryptic way which is, garbled display.
Plus, I know that this is a software issue because I changed it to pin 13 and now when I press the button the onboard LED goes on and off I have attached my modified code.
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <Servo.h>
RTC_DS1307 RTC;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
boolean coffeeLastButton = LOW;
boolean coffeeCurrentButton = LOW;
int coffeeMenu = 0;
int selectionButtonOne = 2; //INSERT PIN NUMBER HERE
boolean selectionButtonOneLast = LOW; //Used for debouncing
boolean selectionButtonOneCurrent = LOW; //Used for debouncing
int selectionButtonTwo = 4;//INSERT PIN NUMBER HERE
boolean selectionButtonTwoLast = LOW; //Used for debouncing
boolean selectionButtonTwoCurrent = LOW; //Used for debouncing
int alarmHour = 0;
int alarmMin = 0;
int coffeeChoice = 0;
Servo FREservo;
Servo COLservo;
void setup(){
FREservo.attach(3);/*INSERT PIN NUMBER HERE*/
COLservo.attach(5);/*INSERT PIN NUMBER HERE*/
FREservo.write(0);
COLservo.write(0);
pinMode(13, INPUT_PULLUP);
pinMode(selectionButtonOne, INPUT_PULLUP);
pinMode(selectionButtonTwo, INPUT_PULLUP);
Serial.begin(9600);
Wire.begin();
RTC.begin();
lcd.begin(16, 2);
if (! RTC.isrunning()) {
lcd.setCursor(0, 0);
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
boolean menudebounce(boolean coffeeLast){ //This section debounces the Menu button
boolean coffeeCurrent = digitalRead(13);
if (coffeeLast != coffeeCurrent){
delay(5);
coffeeCurrent = digitalRead(13);
}
return coffeeCurrent;
}
boolean buttonOnedebounce(boolean OneLast){ //This section debounces the SelectionOne button
boolean OneCurrent = digitalRead(selectionButtonOne);
if (OneLast != OneCurrent){
delay(5);
OneCurrent = digitalRead(selectionButtonOne);
}
return OneCurrent;
}
boolean buttonTwodebounce(boolean TwoLast){ //This section debounces the SelectionOne button
boolean TwoCurrent = digitalRead(selectionButtonTwo);
if (TwoLast != TwoCurrent){
delay(5);
TwoCurrent = digitalRead(selectionButtonTwo);
}
return TwoCurrent;
}
void loop(){
coffeeCurrentButton = menudebounce(coffeeLastButton);
selectionButtonOneCurrent = buttonOnedebounce(selectionButtonOneLast);
selectionButtonTwoCurrent = buttonTwodebounce(selectionButtonTwoLast);
if(coffeeLastButton == LOW && coffeeCurrentButton == HIGH){
coffeeMenu = coffeeMenu++;
coffeeLastButton = HIGH;
}
coffeeLastButton = coffeeCurrentButton;
if(selectionButtonOneLast == LOW && selectionButtonOneCurrent == HIGH){
selectionButtonOne = !selectionButtonOne;
selectionButtonOneLast = HIGH;
}
selectionButtonOneLast = selectionButtonOneCurrent;
if(selectionButtonTwoLast == LOW && selectionButtonTwoCurrent == HIGH){
selectionButtonTwo = !selectionButtonTwo;
selectionButtonTwoLast = HIGH;
}
selectionButtonTwoLast = selectionButtonTwoCurrent;
if(coffeeMenu == 3){ //Limits Menu button to values 0, 1, 2
coffeeMenu = 0;
}
Serial.println(coffeeMenu);
if(coffeeMenu == 2){
lcd.setCursor(0, 0);
lcd.print (alarmHour);
lcd.print (":");
lcd.print (alarmMin);
}
if(alarmHour == 24){
alarmHour = 0;
}
if(alarmMin == 60){
alarmMin = 0;
}
while(coffeeMenu == 2){
if(selectionButtonOne = HIGH){
alarmHour++;
}
}
while(coffeeMenu == 2){
if(selectionButtonTwo = HIGH){
alarmMin++;
}
}
DateTime now = RTC.now();
DateTime future (now.unixtime() + alarmHour * 3600 + alarmMin * 60);
if(now.unixtime() == future.unixtime()){ //This section says when now timestamp == to alarm timestamp make an action happen
}
if(coffeeMenu == 1 && coffeeChoice == 0){
lcd.setCursor(0, 0);
lcd.print("Coffee Settings");
lcd.setCursor(0, 2);
lcd.print("FRE COL");
}
else{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
}
while(coffeeMenu == 1 && coffeeChoice == 0){
if(selectionButtonOne == HIGH){
coffeeChoice = 1;
}
if(selectionButtonTwo == HIGH){
coffeeChoice = 2;
}
}
if(coffeeChoice = 1){
FREservo.write(180);
if(coffeeMenu == 1){
lcd.setCursor(0, 0);
lcd.print("Coffee Settings");
lcd.setCursor(0, 2);
lcd.print("FRE<-- COL");
}
else{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
}
}
if(coffeeChoice = 2){ //if the coffee choice is Columbian then print the screen indicating the choice and drop in the coffee grounds using the servo
COLservo.write(180);
if(coffeeMenu == 1){
lcd.setCursor(0, 0);
lcd.print("Coffee Settings");
lcd.setCursor(0, 2);
lcd.print("FRE -->COL");
}
else{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
}
}
while(coffeeChoice > 0){ //This section says if a coffe is chosen make sure that the buttons do nothing
if(selectionButtonOne = HIGH){
}
if(selectionButtonTwo = HIGH){
}
}
if(coffeeMenu == 0){
lcd.setCursor(0, 0);
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(' ');
lcd.setCursor(0, 1);
if (now.hour()<10)
lcd.print('0');
lcd.print(now.hour(), DEC);
lcd.print(':');
if (now.minute()<10)
lcd.print('0');
lcd.print(now.minute(), DEC);
lcd.print(':');
if (now.second()<10)
lcd.print('0');
lcd.print(now.second(), DEC);
/* lcd.setCursor(0, 3);
int dayofweek = now.dayOfWeek();
switch(dayofweek){
case 1:
lcd.print("Monday");
break;
case 2:
lcd.print("Tuesday");
break;
case 3:
lcd.print("Wednesday");
break;
case 4:
lcd.print("Thursday");
break;
case 5:
lcd.print("Friday");
break;
case 6:
lcd.print("Saturday");
break;
case 0:
lcd.print("Sunday");
break;
delay(1000);
}
*/
}
else{
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
}
}