#include <LiquidCrystal.h>
#define DS1307_I2C_ADDRESS 0x68
#include "Wire.h"
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int menuState = 0;
int lastState = -1;
int state;
int b_light_value = 0;
int w_light_value = 0;
const int w_pin = 11;
const int b_pin = 3;
int b_output = 0;
int w_output = 0;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void showMenus() {
switch (menuState){ //Blues Select
case 0:
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Blues");
break;
case 1:
lcd.clear();
//White Select
lcd.setCursor(0,1);
lcd.print("Whites");
break;
case 2:
lcd.clear();
//Time Set Menu
lcd.setCursor(0,1);
lcd.print("Time Set");
break;
case 3:
lcd.clear();
//Ramp-up/down Timer
lcd.setCursor(0,1);
lcd.print("Sunrise/Set");
break;
case 7:
//Set Blues
lcd.setCursor(0,1);
lcd.print("Blues ");
lcd.print(b_light_value);
break;
case 8:
//Set Whites
lcd.setCursor(0,1);
lcd.print("Whites ");
lcd.print(w_light_value);
break;
}
}
void processButtons(){
int state = 0;
int x = analogRead(0);
//Check analog values from LCD Keypad Shield
if (x < 50) {
state = 5;
//Right
} else if (x < 200) {
//Up
state = 1;
} else if (x < 400){
//Down
state = 2;
} else if (x < 600){
state = 6;
//Left
} else if (x < 800){
//Select
state = 4;
}
delay(250); //delay button read
switch (menuState){
case 0: //Blues
switch (state){
case 1:
menuState = 3;
showMenus();
break;
case 2:
menuState = 1;
showMenus();
break;
case 4: //Enter
menuState = 7;
showMenus();
}
break;
case 1: //Whites
switch (state){
case 1:
menuState = 0;
showMenus();
break;
case 2:
menuState = 2;
showMenus();
break;
case 4:
menuState = 8;
showMenus();
break;
}
break;
case 2: //Time Set
switch (state){
case 1:
menuState = 1;
showMenus();
break;
case 2:
menuState = 3;
showMenus();
break;
case 4:
menuState = 2;
showMenus();
break;
}
break;
case 3: //Ramp-up/down set
switch (state){
case 1:
menuState = 2;
showMenus();
break;
case 2:
menuState = 0;
showMenus();
break;
case 4:
menuState = 3;
showMenus();
break;
}
break;
case 7: //Adjust Blues
switch (state){
case 5: //If Right
b_light_value = b_light_value + 5;
b_output = b_output + 12.75;
if (b_light_value > 100){
b_light_value = 0;
}
if (b_output > 255){
b_output = 0;
}
analogWrite(b_pin, b_output);
showMenus();
break;
case 6: //If Left
b_light_value = b_light_value - 5;
b_output = b_output - 12.75;
if (b_light_value < 0){
b_light_value = 0;
}
if (b_output < 0){
b_output = 0;
}
analogWrite(b_pin, b_output);
showMenus();
break;
case 4: //If Select
menuState = 0;
showMenus();
break;
}
break;
case 8: //Adjust Whites
switch (state){
case 5: //If Right
w_light_value = w_light_value + 5;
w_output = w_output + 12.75;
if (w_light_value > 100){
w_light_value = 0;
}
if (w_output > 255){
w_output = 0;
}
analogWrite(w_pin, w_output);
showMenus();
break;
case 6: //If Left
w_light_value = w_light_value - 5;
w_output = w_output - 12.75;
if (w_light_value < 0){
w_light_value = 0;
}
if (w_output < 0){
w_output = 0;
}
analogWrite(w_pin, w_output);
showMenus();
break;
case 4: //If Select
menuState = 1;
showMenus();
break;
}
break;
}
}
void loop(){
processButtons();
showMenus();
DisplayTime();
delay(1000);
}
void setup(){
setDS1307time(30,42,21,3,8,4,17);
lcd.begin(16,2);
}
void DisplayTime() {
lcd.setCursor(4,0);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS1307
readDS1307time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute<10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second<10)
{
Serial.print("0");
}
Serial.print(second, DEC);
lcd.setCursor(4,0);
lcd.print(hour, DEC);
lcd.print(":");
lcd.print(minute, DEC);
lcd.print(":");
lcd.print(second, DEC);
Serial.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day of week: ");
switch(dayOfWeek){
case 1:
Serial.println("Sunday");
break;
case 2:
Serial.println("Monday");
break;
case 3:
Serial.println("Tuesday");
break;
case 4:
Serial.println("Wednesday");
break;
case 5:
Serial.println("Thursday");
break;
case 6:
Serial.println("Friday");
break;
case 7:
Serial.println("Saturday");
break;
}
}
void setDS1307time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS1307
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS1307time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 3);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
Here's what I have going on. The RTC stuff is basically copy and pasted from another program I made that displays correctly. When I run this, it displays the time as 45:165:85. I'm sure it has somethign to with the way the menu system works but I cant figure out what.