when using the code below or a simular code when you set the time, is there a way to store what time you just set it at as an interger so that you can use that time later in the program? if so how? I am trying to make sort of a intermatic timer, so that a user can use the push buttons on the LCD to set 1,2,or 3 diffrent times and when one of those times = the main time it will then run a step motor.
any help will be great, thanks
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
int a=0;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// define custom characters for up/down/left/right arrow
byte ua[8] = { B00100, B01110, B11111, B10101, B00100, B00100, B00100, B00100};
byte da[8] = {B00100, B00100, B00100, B00100, B10101,B11111,B01110, B00100};
byte la[8] = {B00011, B00110, B01100, B11111, B11111, B01100, B00110, B00011};
byte ra[8] = {B11000, B01100, B00110, B11111, B11111, B00110, B01100, B11000 };
void setup()
{
lcd.begin(16, 2);
Wire.begin();
lcd.createChar(0,ua);
lcd.createChar(1,da);
lcd.createChar(2,la);
lcd.createChar(3,ra);
}
// 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) );
}
int readButtons()
// returns the button number pressed, or zero for none pressed
{
int b,c = 0;
c=analogRead(0); // get the analog value
if (c>1000)
{
b=0; // buttons have not been pressed
}
else
if (c>470 && c<485)
{
b=1; // button 1 pressed
}
else
if (c<135 && c>120)
{
b=2; // button 2 pressed
}
else
if (c>295 && c<315)
{
b=3; // button 3 pressed
}
else
if (c<10)
{
b=4; // button 4 pressed
}
else
if (c<730 && c>715)
{
b=5; // button 5 pressed
}
return b;
}
// Gets the date and time from the ds1307
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.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}
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.send(0);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.send(00010000); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}
void showTime()
{
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print(" ");
lcd.print(hour, DEC);
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
}
void setTime()
{
int escape=0;
int h=0;
int m=0;
do
{
escape=readButtons();
delay(100); // for debounce
if (escape==1)
{
--m;
if (m<0)
{
m=59;
}
} else
if (escape==2)
{
h++;
if (h>23)
{
h=0;
}
} else
if (escape==3)
{
--h;
if (h<0)
{
h=23;
}
} else
if (escape==4)
{
m++;
if (m>59)
{
m=0;
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.write(0);
lcd.write(1);
lcd.print(":h ");
lcd.write(2);
lcd.write(3);
lcd.print(":m Sel=OK");
lcd.setCursor(5,1);
if (h<10)
{
lcd.print("0");
}
lcd.print(h);
lcd.print(":");
if (m<10)
{
lcd.print("0");
}
lcd.print(m);
} while (escape<5);
setDateDs1307(0, m, h, dayOfWeek, dayOfMonth, month, year);
}
void loop()
{
lcd.clear();
showTime();
a=readButtons();
if (a==5) // someone pressed 'select'
{
delay(200); // for debounce
setTime();
}
delay(75); // delay to stop LCD flicker
}