Hi, there is a standard code enables \ disables the diode function using the function val millis () to if given the command H, then it is forced to turn off after a specified time. The problem is that millis () sends the command, regardless of what has been instructed to LOW, and do so indefinitely. Help please, what could be the problem?
#define timelimit (60000)
int led = 10;
int val;
unsigned long timestamp=millis();
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop()
{
if (Serial.available()) {
val = Serial.read();
if (val == 'H') {
digitalWrite(led, HIGH);
Serial.println(" Led ON ");
}
if (val == 'L') {
digitalWrite(led, LOW);
Serial.println(" Led OFF ");
}
}
if(millis()-timestamp>=timelimit){
digitalWrite(led, LOW);
Serial.println(" Led OFF ");
}
}
The millis() don't work at compile time when that number is set.
Let it be the default zero.
void loop()
{
if (Serial.available()) {
val = Serial.read();
if (val == 'H') {
digitalWrite(led, HIGH);
Serial.println(" Led ON ");
timestamp = millis();
}
if (val == 'L') {
digitalWrite(led, LOW);
Serial.println(" Led OFF ");
timestamp = 0; // to keep the timer from printing upmteen Led OFF messages
}
}
if ( timestamp > 0 ){
if(millis()-timestamp>=timelimit){
digitalWrite(led, LOW);
Serial.println(" Led OFF ");
}
}
}
but the problem remains infinitely sends a command to LOW
The Arduino does not "send a command to LOW". Try again.
You need to use serial data only when you get serial data. The Arduino can not tell the difference between the bluetooth device receiving data and the bluetooth device being disconnected. It is up to YOU to make that distinction, based on time typically, as GoForSmoke's code does.