2 button signals using nRF24L01

Hello Arduino forum
I am trying to get the nRF24L01 to transmit the button state for 2 different buttons.
What I want to happen in the program is, if I hold down the first button, the Yellow LED should turn on for 500 ms and then turn off, and when I hold down the second button the Red LED should turn on for 500 ms and then turn off.
What really happens.
When i press the first button, named with button_state the Yellow LED, does what it is supposed to do, a singlular time and then stays turned off. When i then press the second button, named button_state1 my Red LED does nothing, and it triggers the Yellow LED instead, and keeps triggering the code for the Yellow LED.
In the start of the Reciever code i have the program to radio read the different button states, and then serial.Print them to see if they are sent as HIGH or LOW, but when the second button is pressed it keeps sending 1’s as button_state1.

How do I fix my problem?
Thanks in advance

  • Redbridge

Transmitter code

[code]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte addresses[6] = {"89512"};  //Setting the two addresses. One for transmitting and one for receiving
int button_pin = 2; //Red button pin
int button_pin1 = 4;//Yellow button pin

boolean button_state = 0; //boolean to store value
boolean button_state1 = 0; //boolean to store value

void setup() {

  Serial.begin(9600);
  pinMode(button_pin, INPUT);
  pinMode(button_pin1, INPUT);
  radio.begin();                           //Starting the radio communication
  radio.openWritingPipe(addresses[1]);     //Setting the address at which we will send the data
  radio.setChannel(123);                   //Set channel to avoid 2.4ghz wifi
  radio.setPALevel(RF24_PA_MIN);           //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
}
void loop()
{
  delay(50);
  radio.stopListening();                             //This sets the module as transmitter
  if (digitalRead(button_pin) == HIGH) {             //if Button (Red) is HIGH do:
    delay(10);
    button_state = digitalRead(button_pin);            //assign button_state to HIGH
    radio.write(&button_state, sizeof(button_state));  //Sending the data
    Serial.write("Red");
  }

  delay(50);

  if (digitalRead(button_pin1) == HIGH) {              //if Button (Yellow) is HIGH do:
    delay(10);
    button_state1 = digitalRead(button_pin1);            //assign button_state1 to HIGH
    radio.write(&button_state1, sizeof(button_state1));  //Sending the data
    Serial.write("Yellow");
  }

}

[/code]

Reciever code

[code]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//Using an Arduino mega 2560
RF24 radio(32, 33); // CE, CSN
const byte addresses[6] = {"89512"};    //Setting the address for recieving
boolean button_state = 0;               //making a Boolean for our recieved button_state
boolean button_state1 = 0;              //making a Boolean for our recieved button_state1
int led_pin = 7;                        //Defining led_pin
int led_pin1 = 6;                       //Defining led_pin
void setup() {
  pinMode(led_pin, OUTPUT);             //Setting led_pin to OUTPUT
  pinMode(led_pin1, OUTPUT);            //Setting led_pin1 to OUTPUT
  Serial.begin(9600);                   //Starting a serial monitor
  radio.begin();                            //Starting the radio communication
  radio.setChannel(123);                    //Setting a channel for the nRF24L01
  radio.openReadingPipe(1, addresses[1]);   //Setting the address at which we will receive the data
  radio.setPALevel(RF24_PA_MIN);            //Setting the Reciever's strength to MIN, because we only need a short range and it takes less power.

}
void loop()
{
  radio.startListening();                    //This sets the module as receiver
  if (radio.available())                     //Looking for incoming data
  {
    radio.read(&button_state, sizeof(button_state));    //These lines are for checking we actually got some data when pushing the buttons
    Serial.println(button_state);                       //These lines are for checking we actually got some data when pushing the buttons
    radio.read(&button_state1, sizeof(button_state1));  //These lines are for checking we actually got some data when pushing the buttons
    Serial.println(button_state1);                      //These lines are for checking we actually got some data when pushing the buttons

    if (button_state == HIGH)                               //If the button_state is HIGH
    {
      digitalWrite(led_pin, HIGH);                          //Setting the LED (YELLOW) to blink
      delay(500);
      digitalWrite(led_pin, LOW);
      delay(100);
    }
    else if (button_state == LOW)                           //If the button_state is LOW
    {
      digitalWrite(led_pin, LOW);                           //Turn off LED
    }
    else if (button_state1 == HIGH)                         //If the button_state1 is HIGH
    {
      digitalWrite(led_pin1, HIGH);                         //Setting the LED1 (RED) to blink
      delay(500);
      digitalWrite(led_pin1, LOW);
      delay(100);
    }
    else if (button_state == LOW)                           //If the button_state1 is LOW
    {
      digitalWrite(led_pin1, LOW);                          //turn off LED1
    }
    delay(50);                                              //Delay for running the code again

[/code]

You should send the states of the two buttons in a single message. Also you should allow more time between transmissions. Sending 5 times per second is probably enough - and allows time for the receiver to act on the data.

You should not be using delay() in the receiving program.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

And have a look at how multiple items of data are sent in one message in this Simple nRF24L01+ Tutorial

…R