Need to turned off led after specific time

I have made the code for remote controlled circuit using One push button I/P (Noramly LOW) ,Single LED O/P,Nano and NRF24L01.
After pressing the button, the led in RX circuit will turn on, and the subsequent press will turn it off.
Here is my RX side code and its working fine

//RX CODE
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN);
boolean state5 = false;
boolean state3 = LOW;

int joystick[1];  // 2 element array holding Joystick readings Xangle and 7 button state

int LadderLamp = 6; // digital out put
byte Ladder;
void setup() {

  Serial.begin(9600);
  pinMode(LadderLamp, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();;
}

void loop() {
  radio.startListening();
  if ( radio.available() )
  { Serial.print ("Lamp ON");
    radio.read( &joystick, sizeof(joystick));
    Ladder = joystick[0];
    if (Ladder == HIGH) {
      state3 = !state3;
      digitalWrite(LadderLamp, state3);
    }
    delay(50);

  }
}

Now i want to add some more functions!!
If there is no input from the TX for 5 seconds after the led is turned on, the led will turn off automatically. This is a function I want to add to the code. Please assist me in doing the same.

Save the value of millis() when the LED is turned on as startTime

In loop() test whether the current value of millis() - startTime is greater than the required period and if so turn off the LED. It does not matter if the LED is already turned off

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN);
boolean state5 = false;
boolean state3 = LOW;

int joystick[1];  // 2 element array holding Joystick readings Xangle and 7 button state

int LadderLamp = 6; // digital out put
byte Ladder;
void setup() {

  Serial.begin(9600);
  pinMode(LadderLamp, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();;
}

void loop() {
  radio.startListening();
  unsigned long currentMillis= millis();
  if ( radio.available() )
  { 
    radio.read( &joystick, sizeof(joystick));
    Ladder = joystick[0];
    if (Ladder == HIGH) {
      state3 = !state3;
     unsigned long LedStartTime=currentMillis;
      digitalWrite(LadderLamp, state3);
      Serial.print ("LedStartTime : ");
      Serial.println (LedStartTime);
      Serial.print ("currentMillis : ");
      Serial.println (currentMillis);
      Serial.print("Differens : ");
      Serial.println (currentMillis-LedStartTime);
    }
    delay(50);

  }
 
}
Serial Monitor o/p


LedStartTime : 3320
currentMillis : 3320
Differens : 0
LedStartTime : 19786
currentMillis : 19786
Differens : 0


its not working.

i think stored led start time also varies! :woozy_face:

Read what I wrote carefully

then

Note that this test should be in loop(), not inside a test for the value of Ladder

Note too that your LedStartTime variable is local to the if code block and will not be available outside of it. The easy solution is to declare it as a global so that it is available throughout the sketch

Though I can see some logical operational issues with the following code, it may give the OP an idea or 2.

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN);
boolean state5 = false;
boolean state3 = LOW;

int joystick[1];  // 2 element array holding Joystick readings Xangle and 7 button state

int LadderLamp = 6; // digital out put
byte Ladder;
void setup() {

  Serial.begin(9600);
  pinMode(LadderLamp, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();;
}
unsigned long FiftyMillis = 50;
unsigned long Past50Time = millis();
unsigned long LEDonTime = 5000;
unsigned long LedStartTime = millis();
bool LEDon = false;
void loop() {
  //run once every 50mS
  if ( (millis() - Past50Time) >= Past50Time )
  {
    radio.startListening();
    //unsigned long currentMillis = millis();
    if ( radio.available() )
    {
      radio.read( &joystick, sizeof(joystick));
      Ladder = joystick[0];
      if (Ladder == HIGH) 
      {
        state3 = !state3;
        digitalWrite(LadderLamp, HIGH); <<<might need to swap a high for a low
        LedStartTime = millis();
        LEDon = true;
        //Serial.print ("LedStartTime : ");
        //Serial.println (LedStartTime);
        //Serial.print ("currentMillis : ");
        //Serial.println (currentMillis);
        //Serial.print("Differens : ");
        //Serial.println (currentMillis - LedStartTime);
Past50Time=millis();      
}
      //delay(50);not needed
    }
  }
///turn off led 5 seconds later
  if ( (LEDon) & ( millis() - LedStartTime ) >= LEDonTime )
  {
    LEDon = false;
    digitalWrite(LadderLamp, LOW); <<<might need to swap a low for a high
  }
}

an input cannot happen for 5 seconds or the LED will not turn off because LedStartTime will get updated.

Save the value of millis() when the LED is turned on as startTime

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN);
boolean state5 = false;
boolean state3 = LOW;
unsigned long LedStartTime;
unsigned long turnOffDelay = 5000; // turn off LED after this time
int joystick[1];  // 2 element array holding Joystick readings Xangle and 7 button state

int LadderLamp = 6; // digital out put
byte Ladder;
void setup() {

  Serial.begin(9600);
  pinMode(LadderLamp, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();;
}

void loop() {
  radio.startListening();
  unsigned long currentMillis= millis();
  if ( radio.available() )
  { 
    radio.read( &joystick, sizeof(joystick));
    Ladder = joystick[0];
    if (Ladder == HIGH) {
       state3 = !state3;
       digitalWrite(LadderLamp, state3);
       if (state3 == HIGH)
 {
       LedStartTime=currentMillis;
 }
    
    }
    delay(50);

  }
 Serial.print ("LedStartTime : ");
 Serial.println (LedStartTime);
}

s.m o/p

LedStartTime : 6944
LedStartTime : 6944
LedStartTime : 6944
LedStartTime : 6944
LedStartTime : 21643
LedStartTime : 21643
LedStartTime : 21643

is it correct?

Don't mix millis() and delay() as a matter of course.

noted

if ( (millis() - PastTime) >= Past50Time )

i think FiftyMillis in place of PastTime ?

I just wrote the code off the top of my head. I am sure there are errors with the code I wrote.

Note that this test should be in loop(), not inside a test for the value of Ladder

Note too that your LedStartTime variable is local to the if code block and will not be available outside of it. The easy solution is to declare it as a global so that it is available throughout the sketch

this is my new code and its working,pls let me know if anything done wrong

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN);
boolean state5 = false;
boolean state3 = LOW;
unsigned long LedStartTime;
unsigned long turnOffDelay = 5000; // turn off LED after this time
int joystick[1];  // 2 element array holding Joystick readings Xangle and 7 button state

int LadderLamp = 6; // digital out put
byte Ladder;
void setup() {

  Serial.begin(9600);
  pinMode(LadderLamp, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();;
}

void loop() {
  radio.startListening();
  unsigned long currentMillis = millis();
  if ( radio.available() )
  {
    radio.read( &joystick, sizeof(joystick));
    Ladder = joystick[0];
    if (Ladder == HIGH) {
      state3 = !state3;
      digitalWrite(LadderLamp, state3);
      if (state3 == HIGH)
      {
        LedStartTime = currentMillis;
      }

    }
}
 /* Serial.print ("LedStartTime : ");
  Serial.println (LedStartTime);
  Serial.print("Differens : ");
  Serial.print (currentMillis - LedStartTime);*/
  
if (state3 == HIGH && (unsigned long)(currentMillis - LedStartTime) >= turnOffDelay)
  {
    Serial.println("Differens : done ");
    state3 = !state3;
    digitalWrite(LadderLamp, state3);
  }
}



It looks OK

Does it work ?

yes its working

an input cannot happen for 5 seconds or the LED will not turn off because LedStartTime will get updated.

this problem existing. Led could not turn off, if I provide input right away after led switched on.

Does it work ?

ya its working fine