Millis() Problem!

Hello everyone… :slight_smile:
I´m new to the forum and an arduino beginner.

I’ve use an arduino to control some lights using relays and sensors and works fine! but I would like to change a delay (used to maintain lights on during certain time) to millis…

With the purpose of understand the problem, I´ve simplified the code just for the part needed.

I’m using serial.read to trigger the light on (I find easier to debug that way)

I think I almost got it right… because when I use

while (mills()<=EndTime)

the light its on the right amount of time…

the problem is when I use if…

if (mills()>=EndTime) it never turns off…

I hope that anyone could point me in the right direction…
Thanks!

/*
 *  Simple test using millis() instead of delay for turning on a lamp. 
 *  Using serial input as trigger. 
 */


//Definiciones

const int RelayPin=6;

//Timing variables // Variables de tiempo

unsigned long LampTime;         // Trigger Lamp begins      el tiempo en el que inicia Lámpara
unsigned long LampDelay = 3000; // On moment                tiempo para mantenerse encendido 
unsigned long LampEnd;          // End Time                 tiempo Final de estar encendido

char val;                       //For serial input

void setup() { 
Serial.begin(9600);             
pinMode(RelayPin, OUTPUT);
  }
  

void loop() { 
 

if( Serial.available() ) {
val = Serial.read();

if( val == '0' )
{ 
 digitalWrite(RelayPin, HIGH);
 LampTime = millis();            
 LampEnd=(LampTime+LampDelay);

//Serial.prints para revisar datos //Debuging Prints
    
Serial.println();
Serial.print(" LampTime              "); Serial.print(LampTime); 
Serial.println();
Serial.print(" LampEnd               "); Serial.print(LampEnd);  
delay(50); 


if  (millis()>=LampEnd){          /* Check if the lamp has been On enough time.
                                     I´ve also used (millis()>=LampTime+LampDelay) with the same
                                    output
                                    
                                    Revisar si a estado encendida suficiente tiempo
                                    También he utilizado la forma de 
                                    (millis()>=LampTime+LampDelay) con el mismo resultado
                                 
                                   */
      
digitalWrite(RelayPin, LOW); 
Serial.println();
Serial.print("TERMINATED"); 

}//FIN if (millis)

     }//Fin val==0 

  }//Fin Serial.Available
      
}//Fin Loop

if (millis()>=LampEnd){

Once millis() is > LampEnd, when will it be < LampEnd?

Have you read: https://forum.arduino.cc/index.php?topic=503368.0

try something like:

if (currentLampTime - startLampTime <= lampDelay) {

Always use subtraction when working with millis() - for example

if (millis() - lastTimeSomethingHappened >= intervalBetweenHappenings) {

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

larryd:
if (millis()>=LampEnd){

Once millis() is > LampEnd, when will it be < LampEnd?

Hi, those were used in two different scenarios… (sketches versions)
the fist one:

while millis() <= LampEnd (witch have worked fine)

and the other (in other version)

if mills() >=LampEnd, Then… (that one didn’t work)

DH12043:
try something like:

if (currentLampTime - startLampTime <= lampDelay) {

Thanks, but that doesn’t work

I’ve find that the problem is that my currentTime and the startLampTime are the same… so any comparison will be wrong.

I did a test using switch commands and works fine…

I read the resources you have mentioned but can’t get it yet…

Anyway, thank you for your advices!

I've find that the problem is that my currentTime and the startLampTime are the same.

You don't have either of those variables, so the statement does not make sense.

The problem with the code you posted is that every pass through loop()

(1) you need to input a '0' on the serial monitor, then (2) LampTime is defined, then (3) LampEnd is defined (with LampEnd = LampTime + 3000UL), then (3) nothing else will happen until the NEXT TIME you input a '0', and so forth.

You will never get to the point that millis() > LampEnd, so the entire logic is wrong.

RaFunambule:
Hi, those were used in two different scenarios… (sketches versions)
the fist one:

while millis() <= LampEnd (witch have worked fine)

and the other (in other version)

if mills() >=LampEnd, Then… (that one didn’t work)

BOTH of those are the wrong way to use millis().

Have you carefully studied the links I gave you?

…R