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