# Set time on DS1307 with a keypad.

I am getting too side tracked on the LED clock that I am making by wanting to add more stuff to it. Newest purchase is a eBay find at \$12. A vandal resistant keypad, Storm Industries series 1000. Its clipped into 3 shields and propped up with a Sparkfun box. A very heavy and sturdy keypad around 2-3 pounds.

I have seen a video on YouTube that shows a guy setting the time on a DS1307 with a keypad. It is very vague in description and used 4 buttons. Has anyone tired doing something like this? I am not finding much info on how to go about this.

Idea from this also.
http://blog.makezine.com/archive/2007/10/alarm_clockcountdown_time_1.html

Anybody know how to combine the two?

Wanting to use part of the code used in this.

``````//
// Maurice Ribble
// 4-17-2008
// http://www.glacialwanderer.com/hobbyrobotics

// This code tests the DS1307 Real Time clock on the Arduino board.
// The ds1307 works in binary coded decimal or BCD.  You can look up
// bcd in google if you aren't familior with it.  There can output
// a square wave, but I don't expose that in this code.  See the
// ds1307 for it's full capabilities.

#include "Wire.h"

// 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) );
}

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
Wire.send(0);
Wire.send(0x80);
Wire.endTransmission();
}*/

// 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.send(0);
Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}

// 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.send(0);
Wire.endTransmission();

// A few of these need masks because certain bits are control bits
*hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
}

void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
Serial.begin(9600);

// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 45;
minute = 3;
hour = 7;
dayOfWeek = 5;
dayOfMonth = 17;
month = 4;
year = 8;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print("  ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print("  Day_of_week:");
Serial.println(dayOfWeek, DEC);

delay(1000);
}
``````

Too use the Keypad to set this section?

``````  // Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 45;
minute = 3;
hour = 7;
dayOfWeek = 5;
dayOfMonth = 17;
month = 4;
year = 8;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
``````

In loop, you'd read a switch that, when pressed, begins the process of collecting the new time.

The 4 buttons that you referred to are for moving left and right, up and down. Left and right select which value is being changed (hours, minutes, seconds). The up and down buttons increment or decrement the values.

The original switch that began the process, when pressed again, would be the signal to commit the time.