Turning on and off light

Hello everybody,
i just came across little problem. I have Arduino+ time module+relay with bulb. In code variable "Hod" and "Min" are for hours and minutes from real time module. Variable "hodiny" and "minuty" we set up and present time in hours and mins when Bulb should start light up. Variable "hodinyD" and "minutyD" represent time in hours and mins until the bulb should glow.

For example : we want to bulb light up from 09:15 to 09:35. So we put 09 in "hodiny" and 15 in "minuty" and 09 in "hodinyD" and 35 in "minutyD".

So i thought it might work like it is in code below. But it doesn't work and i don't know why. I put println inside and it's writing "turn on" but relay is not turning on.

And i want to do one thing : when is all variables (hodiny,minuty,hodinyD,minutyD) set to 0 it should be off for all time when is set to 0. In the end it should looks like : When is real time equal to hodiny,minuty turn on bulb and stay turn on, until the real time is equal to hodinyD,minutyD then turn off and stay turn off until real time is again equal to hodiny and minuty.... and repeat this until variables is set to 0 then stay turned off.

So thank you very much for your help.

t = rtc.getTime();//real time

  Hod = t.hour;

  Min = t.min;

if((Hod==hodiny)&&(Min==minuty)){
                        do
                            {      
                                   Serial.println("turn on");
                                  digitalWrite(36, HIGH); 
                                } while ((Hod!=hodinyD)&&(Min!=minutyD));
                        }
                       
                        else {digitalWrite(36, LOW);}

i just posting part of code becase it's larger project.

That's because you lock it in the do while. Hod and Min are not magically changing once you're in there. Drop the while and let the loop do the looping. (Preudo) code:

if((Hod == hodiny) && (Min == minuty) && !digitalRead(lightPin)){
  digitalWrite(lightPin, HIGH);
}
if((Hod == hodinyD) && (Min == minutyD) && digitalRead(lightPin)){
  digitalWrite(lightPin, LOW);
}[code]

PS, terrible variable names. If one is the on time and the other the off, it should be in the name. I doubt only adding a "D" will tell that whatever language you write in.

I found where i did mistake.... :slight_smile: i forgot something... pinMode(36, OUTPUT);

So it's working like i had it i just added one if for that function when everything it's set to 0

 if((hodiny!=0)&&(minuty!=0)&&(hodinyD!=0)&&(minutyD!=0)){
                      
                       if((Hod==hodiny)&&(Min==minuty)){
                        do
                            {      
                                  digitalWrite(36, HIGH); 
                                } while ((Hod!=hodinyD)&&(Min!=minutyD));
                        }
                       
                        else {digitalWrite(36, LOW);}
                              }

i know maybe it's not very good but it's working :smiley:

Thanks for your help

                        do
                            {     
                                  digitalWrite(36, HIGH);
                                } while ((Hod!=hodinyD)&&(Min!=minutyD));

Why do you feel the need to turn the pin on over and over? Are you afraid that it will somehow turn itself off if you don't keep reminding it to be on?

What will cause this loop to end? Nothing, nada, zip, zilch. You have an infinite loop that, if entered, will render the Arduino useless.

And what is up with the stupid indenting? Tools + Auto Format will make it look like you know what you are doing, and fool all of your friends.