Problem with Alarm with RTC

Hi Guys ,

I’m developing a program (with helping in Forum) to alarm two led with RTC from (min:second) to (min:second) with Serial Monitor but I succeeded only to do that for one led.

when I write the code for twice, the first led turns off and the other receive the command …

briefly … Two led don’t set together

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

RTC_DS1307 RTC;
int start_minute;
int start_second;
int end_minute;
int end_second;
int type;


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);
  int second_of_the_hour = now.minute() * 60 + now.second();
  //The time is set as 24 hours
  //Pin 4 get high at 6pm and low at 6am
  if (Serial.available() > 0)
  {
    type = Serial.parseInt
    start_minute = Serial.parseInt();
    start_second = Serial.parseInt();
    end_minute = Serial.parseInt();
    end_second = Serial.parseInt();
  }

  int start_time = start_minute * 60 + start_second;
  int end_time =  end_minute * 60 + end_second;
  if (type =5 && second_of_the_hour >= start_time &&  second_of_the_hour < end_time)
  {
    if (!digitalRead(12))
    {
      digitalWrite(12, HIGH);
      Serial.println("ON");
    }
  }
  else if (digitalRead(12))
  {
    digitalWrite(12, LOW);
    Serial.println("OFF");
  }
    if (type =6 && second_of_the_hour >= start_time &&  second_of_the_hour < end_time)
  {
    if (!digitalRead(8))
    {
      digitalWrite(8, HIGH);
      Serial.println("ON");
    }
  }
  else if (digitalRead(8))
  {
    digitalWrite(8, LOW);
    Serial.println("OFF");
  }
}

Thanks

  //Any pin. I have used Pin 4
  pinMode(12, OUTPUT);

4 != 12.

If you expect to have 2 LEDs connected, you need 2 output pins.

  if (type =5

Why are you assigning 5 to type in an if statement?

PaulS:

  //Any pin. I have used Pin 4

pinMode(12, OUTPUT);



4 != 12.

If you expect to have 2 LEDs connected, you need 2 output pins.

sorry that is small mistake .. I write it well in code.



if (type =5



Why are you assigning 5 to type in an if statement?

I typed it to set led 12 ON
example : 5,43,12,45,6 .. turn on led 12 from (43:12) to (45:6)

and 6 for led 8 ..

The man said “assigning”, not “testing”… :slight_smile:

You need to read up on the various operators. The = operator and the == operator do very different things, and are used in very different ways.

Thanks for Replaying ...

I did that but the Result is the same ...

Led 12 set perfectly .. If I set led 8 from Serial Monitor ..led 12 turns off and led 8 set ...

It's time to post your updated code with all the changes.

my last code :frowning:

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

RTC_DS1307 RTC;
int start_minute;
int start_second;
int end_minute;
int end_second;
int type;


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);
  pinMode (8, 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);
  int second_of_the_hour = now.minute() * 60 + now.second();
  //The time is set as 24 hours
  //Pin 4 get high at 6pm and low at 6am
  if (Serial.available() > 0)
  {
    type = Serial.parseInt();
    start_minute = Serial.parseInt();
    start_second = Serial.parseInt();
    end_minute = Serial.parseInt();
    end_second = Serial.parseInt();
  }

  int start_time = start_minute * 60 + start_second;
  int end_time =  end_minute * 60 + end_second;
  if (type == 5 && second_of_the_hour >= start_time &&  second_of_the_hour < end_time)
  {
    if (!digitalRead(12))
    {
      digitalWrite(12, HIGH);
      Serial.println("ON");
    }
  }
  else if (digitalRead(12))
  {
    digitalWrite(12, LOW);
    Serial.println("OFF");
  }
  if (type == 6 && second_of_the_hour >= start_time &&  second_of_the_hour < end_time)
  {
    if (!digitalRead(8))
    {
      digitalWrite(8, HIGH);
      Serial.println("ON");
    }
  }
  else if (digitalRead(8))
  {
    digitalWrite(8, LOW);
    Serial.println("OFF");
  }
}

What are you calculating for start_time and end_time? Are the values reasonable?

What IS in type?

Without all the time handling stuff, can you turn the LEDs off and on properly?

  //Any pin. I have used Pin 4
  pinMode(12, OUTPUT);
  pinMode (8, OUTPUT);

Still with the stupid comment, I see. Why? What value does the comment add?

PaulS:
What are you calculating for start_time and end_time? Are the values reasonable?

start_time and end_time the time which the led turn on and off in seconds .

What IS in type?

type is the led type (12 or 8 pin)

Without all the time handling stuff, can you turn the LEDs off and on properly?

I can with one led only .. not twice together ..

  //Any pin. I have used Pin 4

pinMode(12, OUTPUT);
  pinMode (8, OUTPUT);



Still with the stupid comment, I see. Why? What value does the comment add?

it's a copy from other code .. sorry ..don't give attention to it ..

Thanks for your helping and patience .

That what I mean …

It works only for led 12 when I type “5”

and works only for led 8 when I type “6”

I can’t make them work together (every led) with its time …

I can't make them work together (every led) with its time ..

You have not added any more Serial.print() statements, to show that type, start_minute, start_second, end_minute, and end_second contain valid values. Why not?

You haven't proven that start_time and end_time contain valid values. Why not?

I want just ... two if statements work together

I want just ... two if statements work together

No. You just want to whine.

Put the required Serial.print() statements in your code, or do NOT reply again.