I have an arduino mega 2560 set up with a lcd, rtc, relay, and button. Basically what I am trying to do is set the LCD to print the time. Control a light with the relay to come on a certain time and have an override button. What I am having problems with is sometimes when I use the button to override the light timer the LCD displays garbled characters and for some reason I cannot get the timer to turn the light on properly with a delay.
Here is my code:
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
#include <LiquidCrystal.h> // we need this library for the LCD commands
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
byte decToBcd(byte val)
{
return ( (val/1016) + (val%10) );
}
byte bcdToDec(byte val)
{
return ( (val/1610) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
int Blue=22;
int switchPin=24;
boolean lastButton=HIGH;
boolean currentButton=HIGH;
boolean ledOn=false;
const int setupdelay=5000;
const long relaydelay=30000;
const int timedelay=1000;
unsigned long previousMillis=0;
unsigned long previousMillis1=0;
unsigned long previousMillis2=0;
unsigned long previousMillis3=0;
void setup()
{
pinMode(Blue, OUTPUT);
digitalWrite(Blue,HIGH);
pinMode(switchPin, INPUT);
Wire.begin();
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print("What Time Is It");
lcd.setCursor(0,1);
lcd.print("Mr.Wolf?");
delay(5000);
lcd.clear(); // clear LCD screen
}
boolean debounce(boolean last)
{
boolean current=digitalRead(switchPin);
if(last!=current)
{
delay(5);
current=digitalRead(switchPin);
}
return current;
}
void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
currentButton=debounce(lastButton);
if(lastButton==HIGH && currentButton ==LOW){
ledOn=!ledOn;}
lastButton=currentButton;
digitalWrite(Blue,ledOn);
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
if((hour==21)&&(minute==58)&&(second==50)){
digitalWrite(Blue,LOW);
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis1) >= relaydelay){
digitalWrite(Blue,HIGH);
previousMillis1 = currentMillis;}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis2 >= timedelay) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" ");
switch(dayOfWeek){
case 1:
lcd.print("Sun");
break;
case 2:
lcd.print("Mon");
break;
case 3:
lcd.print("Tue");
break;
case 4:
lcd.print("Wed");
break;
case 5:
lcd.print("Thu");
break;
case 6:
lcd.print("Fri");
break;
case 7:
lcd.print("Sat");
break;
}
const char* AMPM = 0;
if (hour > 12) {
hour -= 12;
AMPM = " PM";
}
else AMPM = " AM";
lcd.print(" ");
lcd.print(month, DEC);
lcd.print("/");
lcd.print(dayOfMonth, DEC);
lcd.print("/20");
lcd.print(year, DEC);
lcd.setCursor(0,1);
lcd.print(" ");
if(hour>12){
lcd.print(hour-12, DEC);
}
else{
lcd.print(hour);
}
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(AMPM);
previousMillis2 = currentMillis;}
}