Please help. I put together some code to try and create a latching button for a project of mine. End goal I want to use the code to move motors one way with 1 click, and reverse that direction with another button click. For now I am just trying to get my boards to communicate a latching command to an led. I've tried the below code but it just wont work. Any help at all would be greatly appreciated
TRANSMITTER
#include "nRF24L01.h" //NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"
#define SwitchPin 2
int SentMessage[1] = {000};
RF24 radio(9,10); // NRF24L01 used SPI pins + Pin 9 and 10 on the NANO
const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01
void setup()
{
pinMode(SwitchPin, INPUT_PULLUP);
digitalWrite(SwitchPin,HIGH);
radio.begin(); // Start the NRF24L01
radio.openWritingPipe(pipe); // Get NRF24L01 ready to transmit
}
void loop()
{
if (digitalRead(SwitchPin) == LOW) // If switch is pressed
{
SentMessage[0] = 111;
radio.write(SentMessage, 1); // Send pressed data to NRF24L01
}
else
{
SentMessage[0] = 000;
radio.write(SentMessage, 1); // Send idle data to NRF24L01
}
}
RECEIVER
#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"
#define LED_PIN 2
int flag= 1; //set flag to 1
int ReceivedMessage[1] = {000}; // Used to store value received by the NRF24L01
RF24 radio(9,10); // NRF24L01 SPI pins. Pin 9 and 10 on the Nano
const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01
void setup(void)
{
radio.begin(); // Start the NRF24L01
radio.openReadingPipe(1,pipe); // Get NRF24L01 ready to receive
radio.startListening(); // Listen to see if information received
pinMode(LED_PIN, OUTPUT);
}
void loop(void)
{
while (radio.available())
{
radio.read(ReceivedMessage, 1); // Read information from the NRF24L01
if (ReceivedMessage[0] == 111) // Indicates switch is pressed
{
if (flag ==1) {
digitalWrite(LED_PIN, HIGH);
}
else {
digitalWrite(LED_PIN, LOW);
}
flag= -flag; // inverts flag
}
delay(2);
}
}
That is not a very detailed description of the problem. You should describe what the code actually does and how that differs from what you want.
I tried your code on a couple of Unos on mine. It kind of works. When I press the button the LED sometimes toggles but sometimes not. It is not consistent. Since you send every time through loop the receiver is getting spammed. I suggest not sending quite so often.
If you don't mind, here is how I would do it. This uses the state change detection method and example code from Robin2's simple rf24 tutorial. The push button toggles the mode and the mode is sent to the receiver. The transmitter code only sends when the mode changes. The blink without delay method is used for timing the button reads.
Transmitter with button:
// transmit the toggled button state
// by c goulding aka groundfungus
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte buttonPin = 2;
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int dataToSend = 0;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup()
{
Serial.begin(115200);
Serial.println("SimpleTx Starting");
pinMode(buttonPin, INPUT_PULLUP);
radio.begin();
//RF24_1MBPS, RF24_2MBPS, RF24_250KBPS
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3, 5); // delay, count
//radio.setChannel(10);
//RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(slaveAddress);
}
//====================
void loop()
{
static bool mode = 0;
static bool lastButtonState = HIGH;
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();
bool buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
mode = !mode;
dataToSend = mode;
send();
}
lastButtonState = buttonState;
}
}
}
//====================
void send()
{
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt)
{
Serial.println(" Acknowledge received");
}
else
{
Serial.println(" Tx failed");
}
}
int SentMessage[1] = {000};
SentMessage[0] = 111;
radio.write(SentMessage, 1); // Send pressed data to NRF24L01
int ReceivedMessage[1] = {000}; // Used to store value received by the NRF24L01
radio.read(ReceivedMessage, 1); // Read information from the NRF24L01
if (ReceivedMessage[0] == 111) // Indicates switch is pressed
The 'message' is an array of one 'int' but the code seems to be sending and receiving only one byte of the message.
I tried the new code and when I press the button on the transmitter nothing happens to the led in the receiver. Of note when plugged into my computer power the LED on the receiver constantly flickers but stays on
the original code is flawed because a button press on the transmitter can result in a group of multiple consecutive transmissions of the value 111. However, the receiver treats each received individual 111 as an instruction to toggle the state.
Instead, the receiver should watch for the end of a series of transmissions of 111 then toggle the state.
Hi groundFungus
I dont know if the message was meant for myself I was repling to a post by another forum member I appreciate all help and advice from all members as I would not be able to achieve the basics without the contribution of forum members.
Thanks again
oldguyjn