currently led remote just sends a button to On and the other time to Off
I want to modify the ledremote example in rf24 to allow the button to keep led on as long as button pressed only without sending low all the time across the transmitter to keep the led off. i havent been succesfull. setup as is works vey well.
i searched many places and its a challenge for me.
I want to modify the ledremote example in rf24 to allow the button to keep led on as long as button pressed only without sending low all the time
Send one value when the button on the Tx becomes pressed and a different one when the button on the Tx becomes released. Have the Rx determine whether to turn the LED on or off based on the value received
Look at the StateChangeDetection example in the IDE to see how to detect a state change.
i looked at change detection state on tx , but it needs to send once a low state and keep the RX low till next high till next high trigger from TX
do you have a small code to illustrate please?
tried this but not working as expected
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
// Constant Variables
RF24 radio(2, 3); // NRF24L01 used pins 2 and 3 on the ATTINY84
const uint64_t pipe = 0xE8E8F0F0E1LL;
const int buttonPin = 5; // Array that holds the pins used for each button
bool executed = false;
// Variable Decleration and initialization
void setup()
{
Serial.begin(115000);
//radio.begin(); // Start the NRF24L01
//radio.setPALevel(RF24_PA_HIGH);
//radio.setDataRate(RF24_1MBPS);
//radio.setCRCLength(RF24_CRC_16);
//radio.openWritingPipe(pipe); // Get NRF24L01 ready to transmit
// Define the arcade switch NANO pin as an Input using Internal Pullups
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
uint8_t button = digitalRead(buttonPin);
//Serial.println(button);
// Button has been pressed (HIGH state)
if (button == HIGH && executed == false)
{
radio.write(&buttonPin, sizeof(buttonPin));
executed = true;
}
if (button == LOW && executed == true)
{
Serial.println("Send once");
radio.write(&buttonPin, sizeof(buttonPin));
executed = false;
}
}
in this simple example the light stays on once button pressed even tough i tell it to go low , anything wrong i did?
trnasmitter
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 7;
void setup(void) {
pinMode(SW1,INPUT);
digitalWrite(SW1, LOW);
Serial.begin(115200);
radio.begin();
radio.openWritingPipe(pipe);
}
void loop(void) {
if (digitalRead(SW1) == HIGH)
{
msg[0] = 111;
radio.write(msg, 1);
}
}
receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
int msg[1];
RF24 radio(9, 10);
int LED1 = 2;
void setup(void) {
Serial.begin(115200);
radio.begin();
radio.openReadingPipe(1, pipe);
radio.startListening();
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);
}
void loop(void)
{
if (radio.available())
{
radio.read(msg, 1);
Serial.println(msg[0]);
if (msg[0] == 111)
{
digitalWrite(LED1, HIGH);
}
else{
digitalWrite(LED1, LOW);}
}
}
by changing this it fixed it - but this keeps firing a Low over the air - i need it sent once only i think?
void loop(void) {
if (digitalRead(SW1) == HIGH)
{
msg[0] = 1;
radio.write(msg, 1);
}
else if (digitalRead(SW1) == LOW)
{
radio.write(msg, 0);
}
}
Have a look at this Simple nRF24L01+ Tutorial.
Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.
The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work
...R
by changing this it fixed it - but this keeps firing a Low over the air - i need it sent once only i think?
As I said in my first reply you need to detect the change of state of the input then send a 1 when the input becomes HIGH (not is HIGH) and do nothing until the input becomes LOW (not is LOW) at which time you send a 0
On the Rx end turn the LED on when a 1 is received and off when a 0 is received
Robin2:
Have a look at this Simple nRF24L01+ Tutorial.
Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.
The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work
...R
the wireless is not the problem it works perfect with all example this is a case of customization.
UKHeliBob:
As I said in my first reply you need to detect the change of state of the input then send a 1 when the input becomes HIGH (not is HIGH) and do nothing until the input becomes LOW (not is LOW) at which time you send a 0
On the Rx end turn the LED on when a 1 is received and off when a 0 is received
will work on this to see how the code will look like. I am not the greatest at code that is why I am looking at someone to write me a code on how the detect should look like in my case.
I am looking at someone to write me a code on how the detect should look like in my case.
An example extracted from the StateChangeDetection example
const byte buttonPin = A1;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(115200);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
Serial.println("on");
}
else
{
Serial.println("off");
}
}
lastButtonState = buttonState;
}
UKHeliBob:
An example extracted from the StateChangeDetection example
const byte buttonPin = A1;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(115200);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
Serial.println("on");
}
else
{
Serial.println("off");
}
}
lastButtonState = buttonState;
}
TX is working as it should now but the RX both led are coming on at the same time .
it is not recognizing each button to each separate LED assignement.
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(9, 10);
uint8_t LedPins[] = { 2, 3, 4 };
const uint8_t num_Led_pins = sizeof(LedPins);
void setup()
{
for (int p = 0; p < 3; p++)
{
pinMode(LedPins[p], OUTPUT);
digitalWrite(LedPins[p], LOW);
}
Serial.begin(115200);
radio.begin();
radio.setPALevel(RF24_PA_HIGH);
radio.setCRCLength(RF24_CRC_16);
radio.setDataRate(RF24_1MBPS);
printf_begin();
radio.printDetails();
radio.openReadingPipe(1, pipe);
radio.startListening();
}
void loop()
{
if (radio.available())
{
radio.read(&LedPins, num_Led_pins);
//radio.read(msg, 1);
//Serial.println(msg[0]);
if (LedPins[0] = 1)
{
digitalWrite(2, HIGH);
}
if (LedPins[1] = 1)
{
digitalWrite(2, HIGH);
}
if (LedPins[2] = 1)
{
digitalWrite(3, HIGH);
}
else
{
digitalWrite(3, LOW);
}
}
}
tx code..
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(9, 10);
const uint8_t LightTrigger[] = { 2, 3, 4 };
const uint8_t num_button_pins = sizeof(LightTrigger);
uint8_t buttonState0 = 0; // current state of the button
uint8_t buttonState1 = 0; // current state of the button
uint8_t buttonState2 = 0; // current state of the button
uint8_t lastButtonState0 = 0; // previous state of the button
uint8_t lastButtonState1 = 0; // previous state of the button
uint8_t lastButtonState2 = 0; // previous state of the button
void setup()
{
for (int p = 0; p < 3; p++)
{
pinMode(LightTrigger[p], INPUT);
digitalWrite(LightTrigger[p], LOW);
}
Serial.begin(115200);
radio.begin();
radio.setPALevel(RF24_PA_HIGH);
radio.setCRCLength(RF24_CRC_16);
radio.setDataRate(RF24_1MBPS);
printf_begin();
radio.printDetails();
radio.openWritingPipe(pipe);
radio.stopListening();
}
void loop()
{
buttonState0 = digitalRead(LightTrigger[0]);
buttonState1 = digitalRead(LightTrigger[1]);
buttonState2 = digitalRead(LightTrigger[2]);
if (buttonState0 == HIGH)
{
radio.write(&buttonState0, num_button_pins);
Serial.println("button was pressed to HIGH0..");
}
if (buttonState0 != lastButtonState0)
{
if (buttonState0 == LOW)
{
Serial.println("Button was released0...");
radio.write(&buttonState0, num_button_pins);
}
// Delay a little bit to avoid bouncing
delay(5);
}
// save the current state as the last state, for next time through the loop
lastButtonState0 = buttonState0;
if (buttonState1 == HIGH)
{
radio.write(&buttonState1, num_button_pins);
Serial.println("button was pressed to HIGH1..");
}
if (buttonState1 != lastButtonState1)
{
if (buttonState1 == LOW)
{
Serial.println("Button was released1...");
radio.write(&buttonState1, num_button_pins);
}
// Delay a little bit to avoid bouncing
delay(5);
}
// save the current state as the last state, for next time through the loop
lastButtonState1 = buttonState1;
if (buttonState2 == HIGH)
{
radio.write(&buttonState2, num_button_pins);
Serial.println("button was pressed to HIGH1..");
}
if (buttonState2 != lastButtonState2)
{
if (buttonState2 == LOW)
{
Serial.println("Button was released1...");
radio.write(&buttonState2, num_button_pins);
}
// Delay a little bit to avoid bouncing
delay(5);
}
// save the current state as the last state, for next time through the loop
lastButtonState2 = buttonState2;
}
A couple of initial observations. There may be more later
In the Tx code
buttonState0 = digitalRead(LightTrigger[0]);
if (buttonState0 == HIGH)
{
radio.write(&buttonState0, num_button_pins);
Serial.println("button was pressed to HIGH0..");
}
Why are you continually sending the HIGH whilst the button is pressed ? The idea is to only send it when the input becomes HIGH
Why are you sending 3 bytes (num_button_pins) when there is only one byte of data ?
How are the inputs wired on the Tx ? Have you got pulldown resistors in place to keep the inputs LOW when the buttons are not pressed ?
UKHeliBob:
A couple of initial observations. There may be more later
In the Tx code
buttonState0 = digitalRead(LightTrigger[0]);
if (buttonState0 == HIGH)
{
radio.write(&buttonState0, num_button_pins);
Serial.println("button was pressed to HIGH0..");
}
Why are you continually sending the HIGH whilst the button is pressed ? The idea is to only send it when the input [u]becomes[/u] HIGH
Why are you sending 3 bytes (num_button_pins) when there is only one byte of data ?
How are the inputs wired on the Tx ? Have you got pulldown resistors in place to keep the inputs LOW when the buttons are not pressed ?
- the idea is to keep sending high while button pressed and only send once the input goes low as it it is default state. what is connected to thise pins are brake / left /right turn signal power inputs. hope that is clear.
2.I actually need to send 0(low)/ 1(high) for each input that is 6 bits.
if you can tell me what it should look like?
3.Yes, resistor to ground and button tighed to 5V to mimic incoming voltage.
The main problem seems to be that you are transmitting 3 bytes when the data is only 1 byte
radio.write(&buttonState0, num_button_pins);
radio.write(&buttonState1, num_button_pins);
radio.write(&buttonState2, num_button_pins);
but you are expecting to receive an array of 3 bytes
radio.read(&LedPins, num_Led_pins);
You can fix this by sending an array of 3 bytes after setting its elements to the appropriate 0 or 1 value based on reading the inputs.
I actually need to send 0(low)/ 1(high) for each input that is 6 bits.
Sorry, but I have no idea what that means
UKHeliBob:
The main problem seems to be that you are transmitting 3 bytes when the data is only 1 byte
but you are expecting to receive an array of 3 bytes
radio.read(&LedPins, num_Led_pins);
radio.write(&buttonState0, num_button_pins);
radio.write(&buttonState1, num_button_pins);
radio.write(&buttonState2, num_button_pins);
You can fix this by sending an array of 3 bytes after setting its elements to the appropriate 0 or 1 value based on reading the inputs.
Sorry, but I have no idea what that means
Can you show me how that would look like ?
So I need 3 extra button states?
the initial idea :
radio.write(&buttonState0, num_button_pins); high / LOW
radio.write(&buttonState1, num_button_pins); high / LOW
radio.write(&buttonState2, num_button_pins); high / LOW
So I need 3 extra button states?
No.
On the Tx side determine what the state of each LED need to be and put the values in an array of bytes. Transmit the array to the Rx side, read the array and set the LEDs to the values that it holds. For extra points don't use an array but instead use the bits of a single byte to hold the HIGH/LOW states and just transmit the single byte and decode the values on the Rx side.