RTC program help with LED

Here is my code for my RTC, which is a DS3231

i want my LED to flash every 30 seconds by the time of day. HELP

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include “RTClib.h”

RTC_PCF8523 rtc;

char daysOfTheWeek[7][12] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};

void setup () {

while (!Serial) {
delay(1);
}

Serial.begin(57600);
if (! rtc.begin()) {
Serial.println(“Couldn’t find RTC”);
while (1);
}

if (! rtc.initialized()) {
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));
}
}

void loop () {
DateTime now = rtc.now();

Serial.print(now.year(), DEC);
Serial.print(’/’);
Serial.print(now.month(), DEC);
Serial.print(’/’);
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(’:’);
Serial.print(now.minute(), DEC);
Serial.print(’:’);
Serial.print(now.second(), DEC);
Serial.println();

Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println(“d”);

// calculate a date which is 7 days, 12 hours and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));

Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print(’/’);
Serial.print(future.month(), DEC);
Serial.print(’/’);
Serial.print(future.day(), DEC);
Serial.print(’ ‘);
Serial.print(future.hour(), DEC);
Serial.print(’:’);
Serial.print(future.minute(), DEC);
Serial.print(’:’);
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();
delay(3000);
}
This code works on my RTC. the time of day comes up on the serial monitor. now i need to add a LED

“i want my LED to flash every 30 seconds by the time of day. ”

What does flash mean?
Did you mean toggle state?

When seconds change, then when seconds is 0 or 30 you can do your stuff.

delay(3000); This is not a good thing to do as it freezes normal program execution for that amount of time.

i just mean i want it to turn on and off every 30 seconds at first by the time of day

Yes but how long is the LED to be ON.

time = 0 LED goes ON
time = 0 + 30sec LED goes OFF
time = 0 + 60sec LED goes ON
etc.

OR

time = 0 LED goes ON
time = 0 + 1sec LED goes OFF
time = 0 + 30sec LED goes ON
time = 0 + 31sec LED goes OFF
etc.

LED only needs to be on for 5 seconds total. then off for 30 then on for 5

Does that make sense i need help adding the LED in my program for my RTC DS3231. i just need the LED to come on every 30 seconds for 5 seconds.

Take what you need from this example:

#include <DS3232RTC.h>    //http://github.com/JChristensen/DS3232RTC
#include <Time.h>         //http://www.arduino.cc/playground/Code/Time  
#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)

byte lastSecond;
byte currentSecond;
const byte LEDpin = 13;

//***************************************************************
void setup(void)
{
  Serial.begin(9600);

  pinMode(LEDpin, OUTPUT);

  setSyncProvider(RTC.get);   
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");
} //END of setup()

//***************************************************************
void loop(void)
{
  currentSecond = second();

  if (lastSecond != currentSecond)
  {
    lastSecond = currentSecond;
    processLED();
    Serial.println(second());
  }

} //END of loop()

//***************************************************************
void processLED()
{
  switch (currentSecond)
  {
    case 0:
      digitalWrite(LEDpin, HIGH); //turn on LED
      break;
      
    case 5:
      digitalWrite(LEDpin, LOW); //turn off LED
      break;      
    case 30:
      digitalWrite(LEDpin, HIGH); //turn on LED
      break;
   
    case 35:
      digitalWrite(LEDpin, LOW); //turn off LED
      break;
  }

} //END of processLED()

im not sure at all what you mean by take what i need? can you be more specific? i just want to add on my program, im really a weak programmer. i have my LED hooked to pin 13, i just dont know what code i need to make it work.

I am only using 2 libraries in conjunction with my RTC which are, #include <Wire.h>
#include “RTClib.h” i dont want to add or change these. i just want to add the LED which i dont know how to do.

Add these lines to your original sketch:

void loop ()
{

  //your old loop() code goes here

  //delay(3000) //remove this line in your old code 



  //  Add the following lines of code to your original sketch
  
  //************************************
  currentSecond = now.second();
  if (lastSecond != currentSecond)
  {
    lastSecond = currentSecond;
    processLED();
    
  }

} //END of loop()

//***************************************************************
void processLED()
{
  switch (currentSecond)
  {
    case 0:
      digitalWrite(LEDpin, HIGH); //turn on LED
      break;

    case 5:
      digitalWrite(LEDpin, LOW); //turn off LED
      break;
    case 30:
      digitalWrite(LEDpin, HIGH); //turn on LED
      break;

    case 35:
      digitalWrite(LEDpin, LOW); //turn off LED
      break;
  }

} //END of processLED()

my first error is now 'current second' does not name a type

You have to define your variables.

Add these 3 lines in-front of the setup() function.
This will make them global variables.

byte lastSecond;
byte currentSecond;
const byte LEDpin = 13;

Next you will have to add this line inside the setup() function.

pinMode(LEDpin, OUTPUT);

i did add the variables and it still says the same thing about the currentSecond part

Show us what you have done.

#include <Wire.h>
#include “RTClib.h”
byte lastSecond;

byte currentSecond;

const byte LEDpin = 7;

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};

void setup ()
{
pinMode(LEDpin, OUTPUT);
Serial.begin(9600);
delay(3000); // wait for console opening

if (! rtc.begin()) {
Serial.println(“Couldn’t find RTC”);
while (1);
}

if (rtc.lostPower()) {
Serial.println(“RTC lost power, lets set the time!”);

// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(DATE), F(TIME)));

// Following line sets the RTC with an explicit date & time
// for example to set January 27 2017 at 12:56 you would call:
// rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
}
}

void loop ()
{

DateTime now = rtc.now();

Serial.print(now.year(), DEC);
Serial.print(’/’);
Serial.print(now.month(), DEC);
Serial.print(’/’);
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(’:’);
Serial.print(now.minute(), DEC);
Serial.print(’:’);
Serial.print(now.second(), DEC);
Serial.println();

Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println(“d”);

// calculate a date which is 7 days, 12 hours and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));

Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print(’/’);
Serial.print(future.month(), DEC);
Serial.print(’/’);
Serial.print(future.day(), DEC);
Serial.print(’ ‘);
Serial.print(future.hour(), DEC);
Serial.print(’:’);
Serial.print(future.minute(), DEC);
Serial.print(’:’);
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();

// Add the following lines of code to your original sketch

//************************************
currentSecond = now.second();
if (lastSecond != currentSecond)
{
lastSecond = currentSecond;
processLED();

}

} //END of loop()

//***************************************************************
void processLED()
{
switch (currentSecond)
{
case 0:
digitalWrite(LEDpin, HIGH); //turn on LED
break;

case 5:
digitalWrite(LEDpin, LOW); //turn off LED
break;
case 30:
digitalWrite(LEDpin, HIGH); //turn on LED
break;

case 35:
digitalWrite(LEDpin, LOW); //turn off LED
break;
}

} //END of processLED()

I redid the program and it will now run. But, without the delay i cant even read whats on the serial monitor screen. and i am hooking up a LED with a breadboard. when i use this program my board has no delay the TX light stays lit up. I need to see the time delay and i also need to be able to read the serial monitor screen

i see now what im thinking wrong but i still need the delay for the serial monitor screen but when i add it i have an error. anyway to fix this?

Your RTC library is different from the one I have but this should work:

Note: a HIGH on the LED anode turns it on.

#include <Wire.h>
#include "RTClib.h"

//RTC_DS1307 rtc;
RTC_DS3231 rtc;

byte lastSecond;
byte currentSecond;
const byte LEDpin = 7;

void setup ()
{
  //Wire.begin();
  //rtc.begin();

  pinMode(LEDpin, OUTPUT);

  Serial.begin(9600);
  delay(3000); // wait for console opening

  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower())
  {
    Serial.println("RTC lost power, lets set the time!");

    // Comment out below lines once you set the date & time.
    // Following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

    // Following line sets the RTC with an explicit date & time
    // for example to set January 27 2017 at 12:56 you would call:
    // rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
  }

}


void loop ()
{

  DateTime now = rtc.now();

  //************************************
  currentSecond = now.second();
  if (lastSecond != currentSecond)
  {
    lastSecond = currentSecond;
    processLED();
    Serial.println(now.second());
  }

} //END of loop()

//***************************************************************
void processLED()
{
  switch (currentSecond)
  {
    case 0:
      digitalWrite(LEDpin, HIGH); //turn on LED
      break;

    case 5:
      digitalWrite(LEDpin, LOW); //turn off LED
      break;
    case 30:
      digitalWrite(LEDpin, HIGH); //turn on LED
      break;

    case 35:
      digitalWrite(LEDpin, LOW); //turn off LED
      break;
  }


} //END of processLED()

also can you tell me what these lines all represent? so i can change them as i want and i know what im changing.
void processLED()
{
switch (currentSecond)
{
case 0:
digitalWrite(LEDpin, HIGH); //turn on LED
break;

case 5:
digitalWrite(LEDpin, LOW); //turn off LED
break;
case 30:
digitalWrite(LEDpin, HIGH); //turn on LED
break;

case 35:
digitalWrite(LEDpin, LOW); //turn off LED
break;
}

#include <Wire.h>
#include “RTClib.h”
byte lastSecond;
byte currentSecond;
const byte LEDpin = 7;

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};

void setup ()
{
pinMode(LEDpin, OUTPUT);

Serial.begin(9600);
delay(3000); // wait for console opening

if (! rtc.begin()) {
Serial.println(“Couldn’t find RTC”);
while (1);
}

if (rtc.lostPower()) {
Serial.println(“RTC lost power, lets set the time!”);

// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(DATE), F(TIME)));

// Following line sets the RTC with an explicit date & time
// for example to set January 27 2017 at 12:56 you would call:
// rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
}
}

void loop ()
{
DateTime now = rtc.now();

Serial.println(“Current Date & Time: “);
Serial.print(now.year(), DEC);
Serial.print(’/’);
Serial.print(now.month(), DEC);
Serial.print(’/’);
Serial.print(now.day(), DEC);
Serial.print(” (”);
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(’:’);
Serial.print(now.minute(), DEC);
Serial.print(’:’);
Serial.print(now.second(), DEC);
Serial.println();

Serial.println(“Unix Time: “);
Serial.print(“elapsed “);
Serial.print(now.unixtime());
Serial.print(” seconds/”);
Serial.print(now.unixtime() / 86400L);
Serial.println(” days since 1/1/1970”);

// calculate a date which is 7 days & 30 seconds into the future
DateTime future (now + TimeSpan(7,0,0,30));

Serial.println("Future Date & Time (Now + 7days & 30s): ");
Serial.print(future.year(), DEC);
Serial.print(’/’);
Serial.print(future.month(), DEC);
Serial.print(’/’);
Serial.print(future.day(), DEC);
Serial.print(’ ‘);
Serial.print(future.hour(), DEC);
Serial.print(’:’);
Serial.print(future.minute(), DEC);
Serial.print(’:’);
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();

//************************************
currentSecond = now.second();
if (lastSecond != currentSecond)
{
lastSecond = currentSecond;
processLED();
Serial.println(now.second());
}

} //END of loop()

//***************************************************************
void processLED()
{
switch (currentSecond)
{
case 0:
digitalWrite(LEDpin, HIGH); //turn on LED
break;

case 5:
digitalWrite(LEDpin, LOW); //turn off LED
break;
case 30:
digitalWrite(LEDpin, HIGH); //turn on LED
break;

case 35:
digitalWrite(LEDpin, LOW); //turn off LED
break;
}

} //END of processLED()

Using this code it still is so fast on the serial monitor that i cant read the time stamps.

Try

EDIT: Updated sketch

#include <Wire.h>
#include "RTClib.h"

byte lastSecond;
byte currentSecond;

const byte LEDpin = 7;

unsigned long lastMillis;

DateTime now;

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup ()
{
  pinMode(LEDpin, OUTPUT);

  Serial.begin(9600);
  delay(3000); // wait for console opening

  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower())
  {
    Serial.println("RTC lost power, lets set the time!");

    // Comment out below lines once you set the date & time.
    // Following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

    // Following line sets the RTC with an explicit date & time
    // for example to set January 27 2017 at 12:56 you would call:
    // rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
  }
}

void loop ()
{
  now = rtc.now();

  //has a second gone by?
  //************************************
  currentSecond = now.second();
  if (lastSecond != currentSecond)
  {
    lastSecond = currentSecond;
    processLED(currentSecond);
    //Serial.println(now.second());
  }

  //************************************
  //is it time to print to the serial monitor?
  if (millis() - lastMillis >= 3000)  //every 3 seconds
  {
    lastMillis = millis();

    printStuff();
  }

} //END of loop()


//This is run every 3 seconds
//***************************************************************
void printStuff()
{
  Serial.println("Current Date & Time: ");
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  Serial.println("Unix Time: ");
  Serial.print("elapsed ");
  Serial.print(now.unixtime());
  Serial.print(" seconds/");
  Serial.print(now.unixtime() / 86400L);
  Serial.println(" days since 1/1/1970");

  // calculate a date which is 7 days & 30 seconds into the future
  DateTime future (now + TimeSpan(7, 0, 0, 30));

  Serial.println("Future Date & Time (Now + 7days & 30s): ");
  Serial.print(future.year(), DEC);
  Serial.print('/');
  Serial.print(future.month(), DEC);
  Serial.print('/');
  Serial.print(future.day(), DEC);
  Serial.print(' ');
  Serial.print(future.hour(), DEC);
  Serial.print(':');
  Serial.print(future.minute(), DEC);
  Serial.print(':');
  Serial.print(future.second(), DEC);
  Serial.println();

  Serial.println();

} //END of printStuff()

//***************************************************************
void processLED(byte Second)
{
  switch (Second)
  {
    case 0:
      digitalWrite(LEDpin, HIGH); //turn on LED
      break;
    case 5:
      digitalWrite(LEDpin, LOW); //turn off LED
      break;
    case 30:
      digitalWrite(LEDpin, HIGH); //turn on LED
      break;
    case 35:
      digitalWrite(LEDpin, LOW); //turn off LED
      break;
  }

} //END of processLED()