I’ve been breaking my head over this for the last hour. I want to use the DS1307 RTC module in a home automation project. To do certain time based things. So I thought lets try to rewrite the “blink without delay” example that comes with the Arduino IDE and use the data from the DS1307 instead of the millis function. To get the data from the DS1307 I used the code that I found on this site. But I got very lost trying to figure out how make this simple sketch. Thankfully there are a lot of friendly helpful people on the Arduino forum who showed me how to do this. It’s actually a lot simpler then I thought.
So here it is, blink without delay and millis but with the DS1307 🙂
/* Blink without delay using a DS1307 Led blinks once every second. */ #include <Wire.h> const int DS1307_I2C_ADDRESS = 0x68; const int led = 13; // the number of the light pin int ledState = LOW; int lastTime = -1; byte second, minute, hour; // Convert binary coded decimal to normal decimal numbers byte bcdToDec(byte val) { return ( (val/16*10) + (val%16) ); } void getDateDs1307() { // Reset the register pointer Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0x00); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 1); second = bcdToDec(Wire.receive() & 0x7f); } void setup() { Wire.begin(); pinMode(led, OUTPUT); } void loop(){ getDateDs1307(); int time1 = second; if (abs(time1 - lastTime) > 1) { if (ledState == LOW) ledState = HIGH; else ledState = LOW; // set the LED with the ledState of the variable: digitalWrite(led, ledState); lastTime = time1; } }