Control led with RTC

Hi guys ,,

please I want a method to control a led with RTC from second 10 to second 25 as an example .

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

RTC_DS1307 RTC;
int val;
int val2;

void setup() {

  Serial.begin(9600);
  Wire.begin();

  RTC.begin();

  if (! RTC.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  //Any pin. I have used Pin 4
  pinMode(12, OUTPUT);


}

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(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  Serial.println();
  delay(1000);

  //The time is set as 24 hours
  //Pin 4 get high at 6pm and low at 6am
  if (Serial.available() > 0)
  {
    val = Serial.parseInt();
  }
    if (val == now.second())
    {
      digitalWrite(12, HIGH);
      Serial.println("YES");
    }
  }

I can't input two numbers in Serial Monitor ...

I could only input the second 10 in Serial monitor but 25 I couldn't !!

Thanx

You could require a character before the number to specify which number you are sending. Like:

S10
E25

Read characters until you get an S for Start or an E for End. Then read the number that follows that and store it as the 'start' or 'end' value.

See if this works like you want, enter 2 numbers separated by a comma, then [ENTER]. ( 10,25[ENTER])
UNTESTED

 if (Serial.available() > 0)
  {
    val = Serial.parseInt();
    val2 = Serial.parseInt();
  }

  if (val > 0 && now.second >= val && now.second <= val2)
  {
    if (!digitalRead(12))
    {
      digitalWrite(12, HIGH);
      Serial.println("YES");
    }
  }
  else if (digitalRead(12))
       {
         digitalWrite(12, LOW);
         Serial.println("NO");
       }

johnwasser:
You could require a character before the number to specify which number you are sending. Like:Read characters until you get an S for Start or an E for End. Then read the number that follows that and store it as the 'start' or 'end' value.

Thanx johnwasser

edgemoron:
See if this works like you want, enter 2 numbers separated by a comma, then [ENTER]. ( 10,25[ENTER])
UNTESTED

 if (Serial.available() > 0)

{
    val = Serial.parseInt();
    val2 = Serial.parseInt();
  }

if (val > 0 && now.second >= val && now.second <= val2)
  {
    if (!digitalRead(12))
    {
      digitalWrite(12, HIGH);
      Serial.println("YES");
    }
  }
  else if (digitalRead(12))
      {
        digitalWrite(12, LOW);
        Serial.println("NO");
      }

Thanks man ,, U r the best ....It works

But I'm too confused to do it for minutes too ...

I do it like the previous code like that:

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

RTC_DS1307 RTC;
int val;
int val2;
int val3;
int val4;

void setup() {

  Serial.begin(9600);
  Wire.begin();

  RTC.begin();

  if (! RTC.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  //Any pin. I have used Pin 4
  pinMode(12, OUTPUT);


}

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(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  Serial.println();
  delay(1000);

  //The time is set as 24 hours
  //Pin 4 get high at 6pm and low at 6am
  if (Serial.available() > 0)
  {
    val = Serial.parseInt();
    val2 = Serial.parseInt();
    val3 = Serial.parseInt();
    val4 = Serial.parseInt();
  }

  if (val > 0 && now.minute() >= val && now.minute() < val2 && now.second() >= val3 && now.second() <= val4)
  {
    if (!digitalRead(12))
    {
      digitalWrite(12, HIGH);
      Serial.println("YES");
    }
  }
  else if (digitalRead(12))
  {
    digitalWrite(12, LOW);
    Serial.println("NO");
  }
}

but it works each minute I typed.... not continuously
please help ,,, and a lot of thanks

What did you type? What did you want it to do? What did it do that was different?

My guess is that you typed something like 5,7,15,30
You wanted it to turn on from X:05:15 to X:07:30 of every hour,
But it turned on from X:XX:15 to X:XX:30 of every minutes from minute 5 to minute 7.

johnwasser:
What did you type? What did you want it to do? What did it do that was different?

My guess is that you typed something like 5,7,15,30
You wanted it to turn on from X:05:15 to X:07:30 of every hour,
But it turned on from X:XX:15 to X:XX:30 of every minutes from minute 5 to minute 7.

Exactly .....

that what I want to do

Note: Please make your variable names more meaningful. 'val' and 'val2' are not meaningful. 'start_minute' and 'end_minute' are.

You wrote:

  if (val > 0 && 
      now.minute() >= val && 
      now.minute() < val2 && 
      now.second() >= val3 && 
      now.second() <= val4)
  {

You want:

  int start_time = start_minute * 60 + start_second;
  int end_time =  end_minute * 60 + end_second;
  int second_of_the_hour = now.minute() * 60 + now.second();
  if (second_of_the_hour >= start_time &&  second_of_the_hour < end_time) {

johnwasser:
Note: Please make your variable names more meaningful. 'val' and 'val2' are not meaningful. 'start_minute' and 'end_minute' are.

You wrote:

  if (val > 0 && 

now.minute() >= val &&
      now.minute() < val2 &&
      now.second() >= val3 &&
      now.second() <= val4)
  {




You want:


int start_time = start_minute * 60 + start_second;
  int end_time =  end_minute * 60 + end_second;
  int second_of_the_hour = now.minute() * 60 + now.second();
  if (second_of_the_hour >= start_time &&  second_of_the_hour < end_time) {

Wonderful !!!!

Thanks so much ... it works !!

Thanks for your tips .