Im making a project that has 7 LEDs. The first one turns on on Monday, second on Tuesday, third on Wednesday, and so on. I already have some RTC code up and running. On the serial monitor, it shows the day of the week. How do I use this to make my LED light up at the right time?
Here is my code Im currently using to find day of the week, date, and time:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <Wire.h> #include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
while (!Serial); // for Leonardo/Micro/Zero
Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(F(DATE), F(TIME)));
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled //rtc.adjust(DateTime(F(DATE), F(TIME)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
Grumpy_Mike:
You first turn all of them off. Then you immediately turn on the one that is given to you by the daysOfTheWeek[now.dayOfTheWeek()] variable.
but how will the Arduino know which LED is for Sunday,
You tell it in your code.
The now.dayOfTheWeek() returns a number, print it out and see. The daysOfTheWeek is a string array that returns the day of the week. Which is why daysOfTheWeek[now.dayOfTheWeek()] returns a string to print out.
So you make an array that has the LED pins you need to turn on for each day of the week and index it with your now.dayOfTheWeek() variable to turn on the right LED.
Or if that is too advanced just use a bunch of if statements on your day of the week number.
Grumpy_Mike:
You tell it in your code.
The now.dayOfTheWeek() returns a number, print it out and see. The daysOfTheWeek is a string array that returns the day of the week. Which is why daysOfTheWeek[now.dayOfTheWeek()] returns a string to print out.
So you make an array that has the LED pins you need to turn on for each day of the week and index it with your now.dayOfTheWeek() variable to turn on the right LED.
Or if that is too advanced just use a bunch of if statements on your day of the week number.
I will probably have to use the if statements. But Im afraid that might take up the memory space. Is it possible for you to explain what you mean by indexing the variable into the array or give me some resources to figure it out?
You make an array with the PIN numbers of each of the seven LEDs you want to light up in turn.
Let’s say you have done that and called it imaginatively LEDs. Then you simply do
digitalWrite(LEDs[now.dayOfTheWeek()], HIGH);
For the correct LED to be turned on.
Try it and post what you have done if it doesn’t work.
Thank you! I will try to test this weekend. I will post the code anyways if you still have suggestions. I also need a buzzer to ring at a certain time everyday so, if that is in the code, thats why.