Forced shutdown

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 ");
  }
}
unsigned long timestamp=millis();

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 ");
    }
  }
}

Thank you, but the problem remains infinitely sends a command to LOW, while bluetooth is not connected anymore.

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.

exface:
Thank you, but the problem remains infinitely sends a command to LOW, while bluetooth is not connected anymore.

With the code you have and changes I give;

The pin starts out LOW.

pinMode(led, OUTPUT); // default state is LOW

It will only be HIGH if serial reads 'H' and then only stay HIGH until time is up.

One other thing to change:

#define timelimit (60000)

change to

unsigned long timelimit = 60000UL;

Thank you, I understand the principle of operation.

One more fix. Changed to work on UNO with serial monitor at 115200 baud.

const char led = 13;
char val;
unsigned long timestamp = millis();
const unsigned long timelimit = 60000UL; 

void setup()
{
  Serial.begin( 115200 );
  pinMode( led, OUTPUT );
}

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 " );
      timestamp = 0;
    }
  }
}