How to Keep the LED...
 
Notifications
Clear all

How to Keep the LED light on untill button is pressed again ?

3 Posts
2 Users
0 Reactions
2,224 Views
Gulshan
(@gulshan)
Member
Joined: 5 years ago
Posts: 127
Topic starter  

Hello, I have this code by which I can turn the Led Light on by pushing the button and when release it the LED will get turned off and this happens at the receiver where the LED is connected and the button is connected at the transmitter and both the transmitter and the receiver are connected by the nrf24l01 modules.

I want to modify the code such that once at receiver when I pressed the button The LED should get turn on and then It should remain ON until again that same button is pressed again. Can Anyone Help me ?

Here is the Code:-

TRANSMITTER

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(8, 10); // CE, CSN         
const byte address[6] = "00001";     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.

int button_pin = 2;
boolean button_state = 0;

void setup() {
pinMode(button_pin, INPUT);
radio.begin();                  //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MAX);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening();          //This sets the module as transmitter
}

void loop()
{
button_state = digitalRead(button_pin);
if(button_state == LOW)
{
const char text[] = "Your Button State is HIGH";
radio.write(&text, sizeof(text));                  //Sending the message to receiver
}
else
{
const char text[] = "Your Button State is LOW";
radio.write(&text, sizeof(text));                  //Sending the message to receiver 
}
radio.write(&button_state, sizeof(button_state));  //Sending the message to receiver 
delay(1000);
}


and RECEIVER

#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(8, 10); // CE, CSN const byte address[6] = "00001"; boolean button_state = 0; int led_pin = 7; void setup() { pinMode(led_pin , OUTPUT); Serial.begin(9600); radio.begin(); radio.openReadingPipe(0, address);   //Setting the address at which we will receive the data radio.setPALevel(RF24_PA_MAX);       //You can set this as minimum or maximum depending on the distance between the transmitter and receiver. radio.startListening();              //This sets the module as receiver } void loop() { if (radio.available())              //Looking for the data. { char text[32] = "";                 //Saving the incoming data radio.read(&text, sizeof(text));    //Reading the data radio.read(&button_state, sizeof(button_state));    //Reading the data if(button_state == LOW) { digitalWrite(led_pin , HIGH); Serial.println(text); } else { digitalWrite(led_pin , LOW); Serial.println(text);} } delay(100); }
This topic was modified 5 years ago by Gulshan

   
Quote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 6 years ago
Posts: 1119
 

You just need to add a boolean variable that you "toggle" and invert its value each time you receive a signal.  So when the first signal comes in you toggle it HIGH, next time it gets toggled LOW. You use that boolean (instead of the incoming trigger) to control the LED state.

The easiest way to explain that would be to have you look at the article I wrote on using IR Remote controls. In one of the experiments I do exactly what you are asking, except with an IR remote.

?

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
Gulshan
(@gulshan)
Member
Joined: 5 years ago
Posts: 127
Topic starter  

@dronebot-workshop

Ok, Thank You I will try it. 


   
ReplyQuote