RTC program with leds

Hi,
I'm trying to get a program running on Arduino Uno, my goal is to have 3 led (one red one blue and one green) that will light up only at certain times on the days using a RTC module.
The red one should be on between 20:00 and 6:29 then it should be off
The blue one should be on between 6:30 and 7:29 and then should turn off
Finally the green one should be on between 7:30 and 8:59
After that they should all be off during the rest of the day and start again at 20:00.

I've managed to do the wiring and the RTC seems to work but the leds just won't do what I want and I can't find my mistake.
Thank you for any help you could get me, here's what I've got so far:


#include <virtuabotixRTC.h>

virtuabotixRTC myRTC(6, 7, 8);
const int ledPinRed =  13; 
const int ledPinBlue =  12;
const int ledPinGreen =  11; 


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

  myRTC.setDS1302Time(30, 29, 15, 7, 5, 6, 2022); // seconds, minutes, hours, day of the week, day of the month, month, year

  pinMode(ledPinRed, OUTPUT);     
  pinMode(ledPinBlue, OUTPUT);   
  pinMode(ledPinGreen, OUTPUT); 
  
  pinMode(A0, INPUT);
  pinMode(10, OUTPUT);

}

void loop() 
{
   myRTC.updateTime();

  Serial.print("Current Date / Time: ");
  Serial.print(myRTC.dayofmonth);
  Serial.print("/");
  Serial.print(myRTC.month);
  Serial.print("/");
  Serial.print(myRTC.year);
  Serial.print("  ");
  Serial.print(myRTC.hours);
  Serial.print(":");
  Serial.print(myRTC.minutes);
  Serial.print(":");
  Serial.println(myRTC.seconds);
 
  
  delay(500); // Delay so the program doesn't print non-stop
  
if (myRTC.hours>=20 or myRTC.hours<6)
{
  digitalWrite(ledPinRed, HIGH);
  digitalWrite(ledPinBlue, LOW);
  digitalWrite(ledPinGreen, LOW);
}

if (myRTC.hours=6 and myRTC.minutes<30)
{
  digitalWrite(ledPinRed, HIGH);
  digitalWrite(ledPinBlue, LOW);
  digitalWrite(ledPinGreen, LOW);
}

if (myRTC.hours=6 or myRTC.hours>=30)
{
  digitalWrite(ledPinRed, LOW);
  digitalWrite(ledPinBlue, HIGH);
  digitalWrite(ledPinGreen, LOW);
}
  
if (myRTC.hours=7 and myRTC.minutes<30)
{
  digitalWrite(ledPinRed, LOW);
  digitalWrite(ledPinBlue, HIGH);
  digitalWrite(ledPinGreen, LOW);
}
if (myRTC.hours=7 and myRTC.minutes>=30)
{
  digitalWrite(ledPinRed, LOW);
  digitalWrite(ledPinBlue, LOW);
  digitalWrite(ledPinGreen, HIGH);
}
 
if (myRTC.hours=8)
{
  digitalWrite(ledPinRed, LOW);
  digitalWrite(ledPinBlue, LOW);
  digitalWrite(ledPinGreen, HIGH);
}
if (myRTC.hours>=9 and myRTC.hours <20)
{
  digitalWrite(ledPinRed, LOW);
  digitalWrite(ledPinBlue, LOW);
  digitalWrite(ledPinGreen, LOW);
}

}

mouellou

myRTC.hours = 7 is an assignment. For equality checks, you have to use the "==" operator (two equality signs).

This mistake is in multiple places in your code.

Thanks, I'm always doing that, my bad

In the IDE if you go to File, Preferences and enable compiler warnings using = instead of == in a compare will usually cause a warning.

... \AppData\Local\Temp\arduino_modified_sketch_41135\sketch_jun05b.ino: In function 'void loop()':
sketch_jun05b:48:38: error: lvalue required as left operand of assignment
if (digitalRead(valorSensorPIR) = 0)
^
exit status 1
lvalue required as left operand of assignment

1 Like

Nice, thanks!

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