Hello everyone... ![]()
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