Turn on a led at specific time

hello, i'm trying to make a led turn on at a certain time, but this is not working correctly, i 'm using lcd i2c and an rtc1302, can anyone help me?

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include <DS1302.h>
Time t;
DS1302 rtc(6, 7, 8); //RST,DAT,CLK Pins of the DS1302 Module 


//parâmetros de horário que serão atualizados
int horaAtual, minutoAtual;

//parâmetros primeira alimentação
int horaAlimentacao1, minutoAlimentacao1, demosComida1;

//parâmetros segunda alimentação
int horaAlimentacao2, minutoAlimentacao2, demosComida2;


void setup()
{

 //determina o horário da primeira alimentação
 horaAlimentacao1 = 9; 
 minutoAlimentacao1 = 9;

 //determina o horário da segunda alimentação
//  horaAlimentacao2 = 18; 
//  minutoAlimentacao2 = 30;

  pinMode(5, OUTPUT);

  rtc.halt(false);
  rtc.writeProtect(false);
  lcd.backlight();
  lcd.init();
  lcd.begin(16, 2);
  //comment this section later
  // rtc.setDOW(TUESDAY);      //name of the day of the week
  // rtc.setTime(11, 3, 00);  //Hour, Min, Sec 
  // rtc.setDate(30, 3, 2023); //Day, Month, Year
  digitalWrite(5, LOW);

}

void loop()
{
  //determina o horário atual
  t = rtc.getTime();
  horaAtual = t.hour;
  minutoAtual = t.min;
  lcd.setCursor(0,1);
  lcd.print("Hora:");
  lcd.setCursor(5, 1);
  lcd.print(rtc.getTimeStr());
  lcd.setCursor(0, 0);
  lcd.print("Data:");
  lcd.setCursor(5,0);
  lcd.print(rtc.getDateStr(FORMAT_SHORT, FORMAT_LITTLEENDIAN, '/'));
  //lcd.setCursor(13,1);
  //lcd.print(rtc.getDOWStr());
  if (horaAtual == horaAlimentacao1 && minutoAtual == minutoAlimentacao1)
  {
    digitalWrite(5, LOW);
    delay(20000);
    digitalWrite(5, HIGH);
  }
}

You have not said what is wrong. For instance

  • does it never turn on ?
  • does it turn on at the wrong time ?
1 Like

I think that you need to put extra brackets in the line above.

Try changing it to:

if ((horaAtual == horaAlimentacao1) && (minutoAtual == minutoAlimentacao1))

when I upload the code the led turns on immediately and when the entered time arrives it blinks and after 1 minute it turns off

add some serial prints to verify the values being tested. do horaAtual and minutAtual match the fixed values? do those values match the correct time

the LED should change after 20 sec

use the serial monitor and Serial commands

1 Like

Have you got your LOW and HIGH in the wrong order?

Does a HIGH on pin 5 turn the LED on?

if so you need

  {
    digitalWrite(5, HIGH);
    delay(20000);
    digitalWrite(5, LOW);
  }

Note that after the 20s delay the LED turns off, but the next time around loop() it will come back on as the hour and minute haven't changed yet.

I assume it will repeat your 20s delay three times until 'minutoAtual' changes

2 Likes

It would be polite, or clearer anyway, but unnecessary syntactically.

See

https://en.cppreference.com/w/c/language/operator_precedence

Even if you use lotsa parentheses, it is a good idea to be familiar with operator precedence in C/C++.

a7

2 Likes

@ruip31

The error is in the part of code which you did not post to the forum.
Please show your code in full.

1 Like

The way you describes it makes me think the LED is on when pin 5 is LOW.
First, for readability, you should give your pins a name and, since your LED is (probably) connected between 5V and the Arduino pin 5, you should give the pin states (HIGH and LOW) a name too :

#define LedPin  5
#define LedON  LOW
#define LedOFF HIGH
.
.
.
void setup()
{
.
.
.
  pinMode(LedPin, OUTPUT); // self explanatory
  // digitalWrite(5, LOW); // I think you're wrong here, should be HIGH !
  digitalWrite(LedPin, LedOFF); // Is it HIGH or LOW ? Who cares, it's OFF anyway
}

Now for the if (horaAtual == horaAlimentacao1 && minutoAtual == minutoAlimentacao1){} statement : this condition will remain true for one entire minute. As the statement requires 20 seconds to execute, it will be entered 3 times in a row. To enter it only once, you need a flag (or boolean) :

//parâmetros de horário que serão atualizados
int horaAtual, minutoAtual;
bool LedCylceDone = false; // <-- NEW
.
.
.
void loop()
{
.
.
.

  if(!LedCylceDone && horaAtual == horaAlimentacao1 && minutoAtual == minutoAlimentacao1)
  {
    digitalWrite((LedPin, LedON);
    delay(20000);
    digitalWrite((LedPin, LedOFF);
    LedCylceDone = true;
  }
  if(LedCylceDone && (horaAtual != horaAlimentacao1 || minutoAtual != minutoAlimentacao1))
  {
    LedCylceDone = false; // Reset the flag once the timing condition is over
  }
}

To be continued

It is already working but when the selected time arrives it works but while the led is on the clock freezes and only works again when you turn off the led

I made comments about the way I think the LED is connected. We need an answer about that. Plus your code correctly indented and posted with manually added cpp tag rather than default tag to add colors that help for readability.
Default tag :

```
[your code here]
```

cpp tag :

```cpp
[your code here]
```

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