Nrf24L01 Trying to add more buttons and Leds using states Help Please

Here is some example code using a struct to make a payload to send all four switch states in one write. This struct contains only one data type so could easily be an array instead. I like struct because one can include any data types in them so you could mix bool, byte, int, long and strings in the same struct. You can't do that with an array. This code is adapted from the examples in the simple rf24 tutorial.

Sender


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


const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

struct LdrValues
{
  int ldr_1;
  int ldr_2; 
}ldrValues;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

const byte LDR1 = A0;
const byte LDR2 = A1;

void setup()
{

   Serial.begin(115200);
   Serial.println("SimpleTx Starting");
   pinMode(LDR1, INPUT_PULLUP);
   pinMode(LDR2, INPUT_PULLUP);
   
   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   radio.openWritingPipe(slaveAddress);
}

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {
      send();
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      prevMillis = millis();
   }
}

//====================

void send()
{
   ldrValues.ldr_1 = analogRead(LDR1);
   ldrValues.ldr_2 = analogRead(LDR2);
   radio.write( &ldrValues, sizeof(ldrValues) );
}

Receiver



// SimpleRx - the slave or the receiver

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct LdrValues
{
   int ldr_1;
   int ldr_2;
} ldrValues;

bool newData = false;

//===========

void setup()
{

   Serial.begin(115200);

   Serial.println("SimpleRx Starting");

   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

//=============

void loop()
{
   getData();
   showData();
}

//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &ldrValues, sizeof(ldrValues) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      newData = false;
   }
}

Your struct could look like:

struct SwitchStates
{
   bool button_stateR;
   bool button_stateB;
   bool button_stateP;
   bool button_stateRB;
} switchStates;

Read the switches:

void readSwitchStates()
{
   switchStates.button_stateR = analogRead(RedLed);
   switchStates.button_stateB = analogRead(BlueLed);
   switchStates.button_stateP = analogRead(PauseLed );
   switchStates.button_stateRB = analogRead(Neutral_Led );
}

Send the data:

void send()
{
   NRF24L01.write(&switchStates, sizeof(switchStates));  // one write for all four switch states.
}

The receiving program has the same struct declared global scope.

Receive with:

void receive()
{
   if (NRF24L01.available())
   {
      NRF24L01.read(&switchStates, sizeof(switchStates));
   }
}

Your data will be in the switchStates struct variables.

switchStates.button_stateR
switchStates.button_stateB
switchStates.button_stateP
switchStates.button_stateRB