RTC DS1307 LED with days of the week

Im making a clock project that has 7 LEDs. DS1307 RTC connected via I2C and Wire lib.
The first one turns on on Monday, second on Tuesday, third on Wednesday, and so on.

Please help with code: turn the correct LED on and the previous one on.

Hi. Welcome.
how do you read the date/time from RTC?
How are LEDs to the Board connected?

hi and welcome to the community!

It would be easy to do, but I am betting it would be better to turn off the previous day's LED.

When you turn on an LED number X, you only need to turn off LED number X - 1, and if X - 1 is less than zero, it means LED 6 was the one you need to turn off.

You could use the modulo operator '%'

Turn on LED X.
Turn off LED (X - 1) % 7.

Or use logic

Turn on LED X.
If X is 0, turn off LED 6, otherwise turn off LED X -1

Both assume you are numbering 0 to 6 the 7 "day" LEDs. Like a good C programmer. :expressionless:

The RTC uses 1 to 7, so there's that little detail to take into account...

HTH

a7

Do you know just how to read the date and time from the DS1307? If you do, then I'd say you're 80% of the way done with your project.

Try running this sketch, with your Serial monitor set to 115200 baud.
What do you see on the Serial monitor?

#include <Wire.h>

byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;
 
void setup()
{
  Wire.begin();
  Serial.begin(115200);
 
  // clear /EOSC bit
  // Sometimes necessary to ensure that the clock
  // keeps running on just battery power. Once set,
  // it shouldn't need to be reset but it's a good
  // idea to make sure.
//  Wire.beginTransmission(0x68); // address DS3231
//  Wire.write(0x0E); // select register
//  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
//  Wire.endTransmission();
}
 
void loop()
{
  // ask RTC for the time
  // send request to receive data starting at register 0
  Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
  Wire.write((byte)0); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
  // check for a reply from the RTC, and use it if we can
  if (Wire.available() >= 7) { 
    // if we're here, we got a reply and it is long enough
    // so now we read the time
    ss = bcd2bin(Wire.read()); // get seconds
    mi = bcd2bin(Wire.read()); // get minutes
    hh = bcd2bin(Wire.read()); // get hours
    wd = bcd2bin(Wire.read());
    dd = bcd2bin(Wire.read());
    mo = bcd2bin(Wire.read());
    yy = bcd2bin(Wire.read());
    // show that we successfully got the time
    Serial.print("Got the time: ");
    printTime();
  }
  else {
    // if we're here, that means we were unable to read the time
    Serial.println("Unable to read time from RTC"); 
  }
  delay(500);
}

byte bcd2bin(byte x) {
  // converts from binary-coded decimal to a "regular" binary number
  return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}

void printTime() {
  // just like it says on the tin
  Serial.print ("\'");
  if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
  if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
  if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print("(");
  switch (wd) {
    case 1: Serial.print("Mon"); break;
    case 2: Serial.print("Tue"); break; 
    case 3: Serial.print("Wed"); break; 
    case 4: Serial.print("Thu"); break; 
    case 5: Serial.print("Fri"); break; 
    case 6: Serial.print("Sat"); break; 
    case 7: Serial.print("Sun"); break;
    default: Serial.print("Bad");  
  }
  Serial.print(") ");
  if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
  if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
  if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.