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

Im trying to simplify my project to understand better how to send and receive state changes of multiple buttons to multiple Led's. Iv'e tried to give each button a different state to send and receive in my project. Example: Red button, blue button, pause button and Resume button. The local side of the button changes work, but sending and receiving the state changes don't work.

Press Red Button on master= Master Red Led turns on, sends to Node_01 and Red Led turns on node_01
Blue Turns on both Blue led's on both modules and turns red Led's off
ect
How do I code it so that the blue led turns on with the state of the blue from master to node_01 changes?
Then add more Buttons to correspond to the other buttons. I've been using if statements and just need some guidance or better code on how to program multiple buttons and send to multiple Led's.
This is the project that I started with, just need some state change and how to add more buttons and leds to this sketch. So far in this sketch all 4 buttons turn on the Red led only.
Thanks for the help

Receiving Node

//Receiving Node

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

RF24 NRF24L01 (9, 10);//create object called NRF24L01. specifying the CE and CSN pins to be used on the Arduino
byte address[] [6] = {"00001", "00002", "00003"};//set addresses of the 2 pipes for read and write

int button_stateR;
int button_stateB;
int button_stateP;
int button_stateRB;

const int RedLed = 4;
const int BlueLed = 6;
const int PauseLed = A0;
const int Neutral_Led = 7;

void setup() {

  NRF24L01.begin();
  //open the pipes to read and write from board 1
  NRF24L01.openWritingPipe(address[0]);//open writing pipe to address pipe 1
  NRF24L01.openReadingPipe(1, address[1]);//open reading pipe from address pipe 2

  NRF24L01.setPALevel(RF24_PA_MIN);//set RF power output to minimum, RF24_PA_MIN (change to RF24_PA_MAX if required)
  NRF24L01.setDataRate(RF24_250KBPS);//set data rate to 250kbps
  // NRF24L01.setChannel(110);//set frequency to channel 110


  pinMode(RedLed, OUTPUT);
  pinMode(BlueLed, OUTPUT);
  pinMode(PauseLed, OUTPUT);
  pinMode(Neutral_Led, OUTPUT);
}

void loop() {
  ////////////////////////////Receive button change from the other Arduino//////////////////////////


  NRF24L01.startListening();
  if (NRF24L01.available())//do we have transmission from other Arduino board
  {

    NRF24L01.read(&button_stateR, sizeof(button_stateR));//update the variable with new state

    if (button_stateR == HIGH);//test the other Arduino's button state
    digitalWrite(RedLed, HIGH);
    digitalWrite(BlueLed, LOW);
    digitalWrite(PauseLed, LOW);
    digitalWrite(Neutral_Led, LOW);
  }


}

Master

//Master

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

RF24 NRF24L01(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002", "00003"};

int button_stateR;
int button_stateB;
int button_stateP;
int button_stateRB;

const int RedLed = 4;       //Led for this Uno
const int BlueLed = 6;      //Led for this Uno
const int Neutral_Led = 7;  //Led for this Uno
const int PauseLed = A0;    //Led for this Uno

const int RedButton = 2;      //This Boards Button
const int BlueButton = 3;     //This Boards Button
const int PauseButton = 8;    //This Boards Button
const int ResumeButton = A1;  //This Boards Button

void setup() {

  NRF24L01.begin();
  NRF24L01.openReadingPipe(1, address[0]);//open reading pipe from address pipe 1
  NRF24L01.openWritingPipe(address[1]);//open writing pipe to address pipe 2
  NRF24L01.setPALevel(RF24_PA_MIN);//set RF power output to minimum RF24_PA_MIN (change to RF24_PA_MAX if required)
  NRF24L01.setDataRate(RF24_250KBPS);//set datarate to 250kbps
  // NRF24L01.setRetries(15, 15);//Added later, not sure if I need this or not SA
  // NRF24L01.setChannel(110);//set frequency to channel 110


  pinMode(RedLed, OUTPUT);
  pinMode(BlueLed, OUTPUT);
  pinMode(PauseLed, OUTPUT);
  pinMode(Neutral_Led, OUTPUT);

  pinMode(RedButton, INPUT_PULLUP);
  pinMode(BlueButton, INPUT_PULLUP);
  pinMode(PauseButton, INPUT_PULLUP);
  pinMode(ResumeButton, INPUT_PULLUP);

} //End SETUP

void loop() {
  //#############################################################
  /////////////////////////////////////Transmit button change to the other Arduino
  delay(10);
  NRF24L01.stopListening();
  //#############################################################

  button_stateR = digitalRead(RedButton);//test for button press on this board

  if (button_stateR == LOW)//button is pulled up so test for LOW
  {
    NRF24L01.write(&button_stateR, sizeof(button_stateR));//send LOW state to other Arduino board
    digitalWrite(RedLed, HIGH);
    digitalWrite(BlueLed, LOW);
    digitalWrite(PauseLed, LOW);
    digitalWrite(Neutral_Led, LOW);
  }

  button_stateB = digitalRead(BlueButton);//test for button press on this board

  if (button_stateB == LOW)//button is pulled up so test for LOW
  {
    NRF24L01.write(&button_stateB, sizeof(button_stateB));//send LOW state to other Arduino board
    digitalWrite(BlueLed, HIGH);
    digitalWrite(RedLed, LOW);
    digitalWrite(PauseLed, LOW);
    digitalWrite(Neutral_Led, LOW);
    {
      // button_stateB = HIGH;//reset the button state variable
    }
  }
  button_stateP = digitalRead(PauseButton);//test for button press on this board

  if (button_stateP == LOW)//button is pulled up so test for LOW
  {
    NRF24L01.write(&button_stateP, sizeof(button_stateP));//send LOW state to other Arduino board
    //  digitalWrite(BlueLed, LOW);
    // digitalWrite(RedLed, LOW);
    digitalWrite(PauseLed, HIGH);
    digitalWrite(Neutral_Led, LOW);
  }
  {
    //  button_stateP = HIGH;//reset the button state variable
  }
  button_stateRB = digitalRead(ResumeButton);//test for button press on this board
  if (button_stateRB == LOW)//button is pulled up so test for LOW
  {
    NRF24L01.write(&button_stateRB, sizeof(button_stateRB));//send LOW state to other Arduino board
    digitalWrite(BlueLed, LOW);
    digitalWrite(RedLed, LOW);
    digitalWrite(PauseLed, LOW);
    digitalWrite(Neutral_Led, HIGH);
  }
  {
    // button_stateRB = HIGH;//reset the button state variable
  }


}

Do not use multiple writes to send the data. Put the switch (button) states into a bool array or struct and send the array or struct.

Ok I’ll figure out what that all means and give it a try thanks.

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

Thank you so much for the examples. I’ve been looking so long for good examples of what I need in my project. Implementing them is the hard part. I will work with these and try to get my head around them. I appreciate your knowledge. I hope this will help me get a good foothold on the project I’m working on. Thanks again.

A friend of mine once described structures as: "Arrays for adults"

Econjack

| groundFungus
February 7 |

  • | - |

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.

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.

I've been trying to get this to work, but of no avail. Here's what I have, maybe you can see my mistakes.

Master that Receives and sends

// Material declaration (form)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

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

RF24 NRF24L01(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002", "00003"};


//boolean buttonState1 = false;//used for both transmission and receive
//boolean buttonState = false;//used for both transmission and receive

struct SwitchStates
{
  bool button_stateR_A;
  bool button_stateB_A;
  bool button_stateP_A;
  bool button_stateRB_A;
} SwitchStates;

bool newData = false;

//////////////////////For the Flash led//////
//int ledState = LOW;
//unsigned long previousMillis = 0;
//const long interval = 500;
//unsigned long currentMillis = millis();

/////////////////Master Led's and Buttons (this Module)//////////
const int PauseLed_00 = A0;
const int Neutral_Led_00 = 7;
const int BlueLed_00 = 6 ;
const int RedLed_00 = 4;

const int PauseButton_00 = 8;
const int ResumeButton_00 = A1;
const int buzz_Pin_00 = 5;

/////////////////Module A led's//////////

const int RedLed_01 = 4;
const int Neutral_Led_01 = 7;
const int BlueLed_01 = 6;



/////////////////Module B led's//////////
//const int RedLed_02 = ;
//const int Neutral_Led_02 = ;
//const int BlueLed_02 = ;

/////////////////Module C led's//////////
//const int RedLed_03 = ;
//const int Neutral_Led_03 = ;
//const int BlueLed_03 = ;

/////////////////Module D led's//////////
//const int RedLed_04 = ;
//const int Neutral_Led_04 = ;
//const int BlueLed_04 = ;



//////////////////Airsoft Bomb Input////////////

//const int ArmPin = ;


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



//bool newData = false;//Not sure what this does yet

void setup() {

  NRF24L01.begin();
  NRF24L01.openWritingPipe(address[0]);//open writing pipe to address pipe 1
  NRF24L01.openReadingPipe(1, address[1]);//open reading pipe from address pipe 2
  NRF24L01.setPALevel(RF24_PA_MIN);//set RF power output to minimum RF24_PA_MIN (change to RF24_PA_MAX if required)
  NRF24L01.setDataRate(RF24_250KBPS);//set datarate to 250kbps
  // NRF24L01.setRetries(15, 15);//Added later, not sure if I need this or not SA
  // NRF24L01.setChannel(110);//set frequency to channel 110

  lcd.init(); // initialize the lcd
  lcd.backlight();

  pinMode(PauseLed_00, OUTPUT);
  pinMode(Neutral_Led_00, OUTPUT);
  pinMode(BlueLed_00, OUTPUT);
  pinMode(RedLed_00, OUTPUT);
  pinMode(PauseButton_00, INPUT_PULLUP);
  pinMode(ResumeButton_00, INPUT_PULLUP);
  pinMode(buzz_Pin_00, OUTPUT);

  // pinMode(RedLed_01, OUTPUT);
  // pinMode(Neutral_Led_01, OUTPUT);//Module A
  // pinMode(BlueLed_01, OUTPUT);

  //  pinMode(RedLed_02, OUTPUT);
  //  pinMode(Neutral_Led_02, OUTPUT);//Module B
  // pinMode(BlueLed_02, OUTPUT);

  // pinMode(RedLed_03, OUTPUT);
  // pinMode(Neutral_Led_03, OUTPUT);//Module C
  // pinMode(BlueLed_03, OUTPUT);

  // pinMode(RedLed_04, OUTPUT);
  // pinMode(Neutral_Led_04, OUTPUT);//Module D
  // pinMode(BlueLed_04, OUTPUT);


}

void loop() {

  lcd.setCursor(4, 0 ); lcd.print("Master_00");
  lcd.setCursor(5, 1 ); lcd.print("Console");



  receive();
  showData();
  {
    if (digitalRead(PauseButton_00) == LOW)
    {
      Pause();
      send();
     // delay(150);
    }
    // else
    // {

    // }

  }
  {
    if (digitalRead(ResumeButton_00) == LOW)
    {
      Resume();
      send();
     // delay(150);
      // }
      // else
      // {
    }

  }

}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("LDR 1 = ");
    //  Serial.print(SwitchStates);
     
      newData = false;
   }
}

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

  digitalWrite(BlueLed_00, HIGH);//to show that the send function works
  delay(2000);
  digitalWrite(BlueLed_00, LOW);
}

void receive()
{
  // digitalWrite(BlueLed_00, HIGH); test to see if receive function called (works)
  if (NRF24L01.available())
  {
    NRF24L01.read(&SwitchStates, sizeof(SwitchStates));
    digitalWrite(BlueLed_00, HIGH);//test for recieve locally
    newData = true;
  }
}

////////////////////////////////////////////////////////////Resume Button//////////////////////////////////////

void Resume() {


  //digitalWrite(RedLed, LOW);//So that nobody thinks its running when it's not
  // digitalWrite(BlueLed, LOW);//So that nobody thinks its running when it's not
  tone(buzz_Pin_00, 2000, 800);
  delay(400);
  tone(buzz_Pin_00, 3000, 800);

  digitalWrite(PauseLed_00, LOW);
  digitalWrite(Neutral_Led_00, HIGH);//sets the states to neutral so it can be started again
  delay(2000);
  digitalWrite(Neutral_Led_00, LOW);


}


////////////////////////////////////////////////////////////Pause Button//////////////////////////////////////

void Pause() {// only have the button on the master so nobody can just neutralize from other and start their timer

  //  buttonState = digitalRead(PauseButton_00);
  //  digitalRead(buttonState == LOW); //sets state to 10 for pause menu
  // if (buttonState == LOW)
  // {

  //Boom();// not used
  digitalWrite(PauseLed_00, HIGH);
  digitalWrite(Neutral_Led_00, LOW);
  tone(buzz_Pin_00, 500, 200);
  delay(200);
  tone(buzz_Pin_00, 1200, 200);
  delay(200);
  tone(buzz_Pin_00, 500, 200);
  delay(200);
  tone(buzz_Pin_00, 1200, 200);
  //delay(500);
  //receive();

  //  newData = true;
  // }
  // buttonState = HIGH;//reset the button state variable
  // if (buzzer)tone (buzz_Pin, 1000, 5000);
  // tone(buzzer, 750, 200);
  //    delay(400);
  //  tone(buzz_Pin, 4000, 200);
  //  delay(400);
}
void Flash() {


  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    digitalWrite(PauseLed_00, ledState);

  }

}

void FlashLed()
{
  for (int i = 0; i < 5; i++)
  {
    digitalWrite(PauseLed_00, HIGH);
    delay(200);
    digitalWrite(PauseLed_00, LOW);
    delay(200);
    digitalWrite(PauseLed_00, HIGH);
  }

}

void Boom() {///////////////When the game is finished sounds//////////////////////////////Doesn't stop ,need buttons//////
  lcd.clear();
  while (1) {
    lcd.setCursor(0, 0); lcd.print("!!!!!!BOOM!!!!!!");


    digitalWrite(PauseLed_00, HIGH);
    digitalWrite(Neutral_Led_00, HIGH);

    for (int i = 0; i < 5; i++) {             //CHANGED THIS AND MAY NEED TO CHANGE BACK
      for (int j = 2000; j < 4000; j++) {
        if (buzz_Pin_00) {
          tone(buzz_Pin_00, j);
          delay(1);
        }
        //   }
        //   noTone(buzz_Pin_00);
        //   for (int j = 4000; j > 2000; j--) {
        //   if (buzz_Pin_00) {
        //    tone(buzz_Pin_00, j);
        //   delay(1);
        //  }
      }
    } // End For i
    noTone(buzz_Pin_00);
    //  break;
  } // End while
  while (1);
  // End void  Boom

}

The Game that sends the majority of the button presses to the Master

// Material declaration (form)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

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

RF24 NRF24L01(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002", "00003"};


struct SwitchStates
{
  bool button_stateR_A;
  bool button_stateB_A;
  bool button_stateP_A;
  bool button_stateRB_A;
} SwitchStates;

bool newData = false;

const int PauseLed = A0;
const int Neutral_Led = 7;
const int BlueLed = 6 ;
const int RedLed = 4;

const int RedButton = 2;      //This Boards Button
const int BlueButton = 3;     //This Boards Button
const int PauseButton = 8;    //This Boards Button
const int ResumeButton = A1;  //This Boards Button

const int buzz_Pin_00 = 5;


void setup() {
  lcd.init(); // initialize the lcd
  lcd.backlight();
  lcd.clear();

  NRF24L01.begin();
  NRF24L01.openReadingPipe(1, address[0]);//open reading pipe from address pipe 1
  NRF24L01.openWritingPipe(address[1]);//open writing pipe to address pipe 2
  NRF24L01.setPALevel(RF24_PA_MIN);//set RF power output to minimum RF24_PA_MIN (change to RF24_PA_MAX if required)
  NRF24L01.setDataRate(RF24_250KBPS);//set datarate to 250kbps
  // NRF24L01.setRetries(15, 15);//Added later, not sure if I need this or not SA
  // NRF24L01.setChannel(110);//set frequency to channel 110





  // Declaration of I/O
  pinMode(RedLed, OUTPUT);
  pinMode(RedButton, INPUT_PULLUP);
  pinMode(BlueLed, OUTPUT);
  pinMode(Neutral_Led, OUTPUT);
  pinMode(PauseLed, OUTPUT);
  pinMode(BlueButton, INPUT_PULLUP);
  pinMode(PauseButton, INPUT_PULLUP);
  pinMode(ResumeButton, INPUT_PULLUP);
  //  pinMode(buzz_Pin, OUTPUT);
}


void loop() {
 // Resume();
 
  readSwitchStates();
  send();
}

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

  }

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

void readSwitchStates()
{
  SwitchStates.button_stateR_A = analogRead(RedLed);
  SwitchStates.button_stateB_A = analogRead(BlueLed);
  SwitchStates.button_stateP_A = analogRead(PauseLed );
  SwitchStates.button_stateRB_A = analogRead(Neutral_Led );
  send();
}

void Resume() {
  digitalRead(ResumeButton);//sets state to 10 for the print of "Paused"


  digitalWrite(Neutral_Led, HIGH);//sets the states to neutral so it can be started again
  digitalWrite(PauseLed, LOW);
  digitalWrite(RedLed, LOW);//So that nobody thinks its running when it's not
  digitalWrite(BlueLed, LOW);//So that nobody thinks its running when it's not
  //  tone(buzz_Pin, 2000, 800);
  delay(400);
  //  tone(buzz_Pin, 3000, 800);
  // delay(600);
  // delay(10);
}

Why are you reading the LED outputs?
What was I thinking? Sorry, my bad mistake. That was in the code that I provided. I can't believe that i did that.

This is how to read the buttons. Try it and let me know if this works better. Sorry again.

  SwitchStates.button_stateR_A = digitalRead(RedButton);
  SwitchStates.button_stateB_A = digitalRead(BlueButton);
  SwitchStates.button_stateP_A = digitalRead(PauseButton );
  SwitchStates.button_stateRB_A = digitalRead(ResumeButton );

No Worries, Thanks for looking at it again for me. My project will be a huge challenge with all the things I have to do for it. I'll work on it as I can and see what I can do. Might take a day or two, but I'll let you know. Thanks

Ok so here is what I have so far, I'm a little unclear on the "radio.stoplistening function and where it needs to be. I have 2 buttons on the master that will send a pause and a Resume to the other modules, but just Module A for now. So I need to be able to read the buttons and send the states from module A to the master and just the Pause and resume to the Module_A. I may need some if statements in the send and receive functions, but I'm not sure yet. Please have a look to see if there is anything out of place with the radio and the Struct functions. Heres the codes:

Master:


/////////////////////////////////Master Module//////////////////////////////

//There are elements from 5.0 that can be added to this once this Master is figured out more///////

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

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

RF24 NRF24L01(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002", "00003"};

struct SwitchStates
{
  bool button_stateR_A;//Module A
  bool button_stateB_A;
  bool button_stateP_A;
  bool button_stateRB_A;
} SwitchStates;

bool newData = false;

/////////////////////////////////////Master Led's and Buttons (this Module)//////////
const int PauseLed_00 = A0;
const int Neutral_Led_00 = 7;
const int BlueLed_00 = 6 ;
const int RedLed_00 = 4;

const int PauseButton_00 = 8;
const int ResumeButton_00 = A1;
const int buzz_Pin_00 = 5;

/////////////////////////////Recieve From Module A Led's///////////////////////////////////////
const int RedLed_01 = 4;
const int Neutral_Led_01 = 7;
const int BlueLed_01 = 6;

/////////////////////////////Recieve From Module B Led's///////////////////////////////////////
//const int RedLed_02 = ;
//const int Neutral_Led_02 = ;
//const int BlueLed_02 = ;

/////////////////////////////Recieve From Module C Led's///////////////////////////////////////
//const int RedLed_03 = ;
//const int Neutral_Led_03 = ;
//const int BlueLed_03 = ;

/////////////////////////////Recieve From Module D Led's///////////////////////////////////////
//const int RedLed_04 = ;
//const int Neutral_Led_04 = ;
//const int BlueLed_04 = ;

/////////////////////////////Recieve From Airsoft Bomb Input///////////////////////////////////////
//const int ArmPin = ;//Recieve from the Airsoft Bomb when Armed

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;


void setup() {

  lcd.init(); // initialize the lcd
  lcd.backlight();
  lcd.clear();

  NRF24L01.begin();
  NRF24L01.openWritingPipe(address[0]);//open writing pipe to address pipe 1
  NRF24L01.openReadingPipe(1, address[1]);//open reading pipe from address pipe 2
  NRF24L01.setPALevel(RF24_PA_MIN);//set RF power output to minimum RF24_PA_MIN (change to RF24_PA_MAX if required)
  NRF24L01.setDataRate(RF24_250KBPS);//set datarate to 250kbps

  /////////////////////////////////////Master_00////////////////////////////
  pinMode(PauseLed_00, OUTPUT);
  pinMode(Neutral_Led_00, OUTPUT);
  pinMode(BlueLed_00, OUTPUT);
  pinMode(RedLed_00, OUTPUT);
  pinMode(PauseButton_00, INPUT_PULLUP);
  pinMode(ResumeButton_00, INPUT_PULLUP);
  pinMode(buzz_Pin_00, OUTPUT);

  /////////////////////////////////////Module A_01////////////////////////////
  // pinMode(RedLed_01, OUTPUT);
  // pinMode(Neutral_Led_01, OUTPUT);
  // pinMode(BlueLed_01, OUTPUT);

  /////////////////////////////////////Module B_02////////////////////////////
  //  pinMode(RedLed_02, OUTPUT);
  //  pinMode(Neutral_Led_02, OUTPUT);
  // pinMode(BlueLed_02, OUTPUT);

  /////////////////////////////////////Module C_03////////////////////////////
  // pinMode(RedLed_03, OUTPUT);
  // pinMode(Neutral_Led_03, OUTPUT);
  // pinMode(BlueLed_03, OUTPUT);

  /////////////////////////////////////Module D_04////////////////////////////
  // pinMode(RedLed_04, OUTPUT);
  // pinMode(Neutral_Led_04, OUTPUT);
  // pinMode(BlueLed_04, OUTPUT);

}

void loop() {

  lcd.setCursor(4, 0 ); lcd.print("Master_00");
  lcd.setCursor(5, 1 ); lcd.print("Module");


  Resume();
  Pause();
  receive();
  send();
}

void Resume() {

  buttonState = digitalRead(ResumeButton_00);//sets state to 10 for the print of "Paused"

  if (buttonState == LOW)
  {
    digitalWrite(Neutral_Led_00, HIGH);//sets the states to neutral so it can be started again
    digitalWrite(PauseLed_00, LOW);
    digitalWrite(RedLed_00, LOW);//So that nobody thinks its running when it's not
    digitalWrite(BlueLed_00, LOW);//So that nobody thinks its running when it's not
    tone(buzz_Pin_00, 2000, 800);
    delay(400);
    tone(buzz_Pin_00, 3000, 800);
    delay(600);
    delay(10);
  }
}

void Pause() {
  buttonState = digitalRead(PauseButton_00);
  if (buttonState == LOW)
  {
    digitalWrite(Neutral_Led_00, LOW);//sets the states to neutral so it can be started again
    digitalWrite(PauseLed_00, HIGH);
    digitalWrite(RedLed_00, LOW);//So that nobody thinks its running when it's not
    digitalWrite(BlueLed_00, LOW);//So that nobody thinks its running when it's not
    tone(buzz_Pin_00, 500, 200);
    delay(200);
    tone(buzz_Pin_00, 1200, 200);
    delay(200);
    tone(buzz_Pin_00, 500, 200);
    delay(200);
    tone(buzz_Pin_00, 1200, 200);

  }

}

void receive()
{
  if (NRF24L01.available())
  {
    NRF24L01.read(&SwitchStates, sizeof(SwitchStates));
    newData = false;
  }

}

void send()
{

}



Module_A


/////////////////////////////////Module_A/////////////////////////////

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

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

RF24 NRF24L01(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002", "00003"};

struct SwitchStates
{
  bool button_stateR_A;
  bool button_stateB_A;
  bool button_stateP_A;
  bool button_stateRB_A;
} SwitchStates;

bool newData = false;

const int PauseLed_01 = A0;
const int Neutral_Led_01 = 7;
const int BlueLed_01 = 6 ;
const int RedLed_01 = 4;

const int RedButton_01 = 2;      //This Boards Button
const int BlueButton_01 = 3;     //This Boards Button
const int PauseButton_01 = 8;    //This Boards Button
const int ResumeButton_01 = A1;  //This Boards Button

const int buzz_Pin_01 = 5;

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

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;


void setup() {
  lcd.init(); // initialize the lcd
  lcd.backlight();
  lcd.clear();

  NRF24L01.begin();
  NRF24L01.openReadingPipe(1, address[0]);//open reading pipe from address pipe 1
  NRF24L01.openWritingPipe(address[1]);//open writing pipe to address pipe 2
  NRF24L01.setPALevel(RF24_PA_MIN);//set RF power output to minimum RF24_PA_MIN (change to RF24_PA_MAX if required)
  NRF24L01.setDataRate(RF24_250KBPS);//set datarate to 250kbps
  NRF24L01.startListening();
  // NRF24L01.setRetries(15, 15);//Added later, not sure if I need this or not SA
  // NRF24L01.setChannel(110);//set frequency to channel 110

  // Declaration of I/O
  pinMode(RedLed_01, OUTPUT);
  pinMode(RedButton_01, INPUT_PULLUP);
  pinMode(BlueLed_01, OUTPUT);
  pinMode(Neutral_Led_01, OUTPUT);
  pinMode(PauseLed_01, OUTPUT);
  pinMode(BlueButton_01, INPUT_PULLUP);
  pinMode(buzz_Pin_01, OUTPUT);
}

void loop() {


  //  Resume();
  // Pause();
  readSwitchStates();

  send();

}


void receive()
{
  if (NRF24L01.available())
  {
    NRF24L01.read(&SwitchStates, sizeof(SwitchStates));
    newData = true;
  }

}

void send()
{
  if (NRF24L01.available())
  {
     NRF24L01.write(&SwitchStates, sizeof(SwitchStates));  // one write for all four switch states.
  }
  
  NRF24L01.stopListening();//??????
}


void readSwitchStates()
{
  SwitchStates.button_stateR_A = digitalRead(RedButton_01);
  SwitchStates.button_stateB_A = digitalRead(BlueButton_01);
  SwitchStates.button_stateP_A = digitalRead(PauseButton_01);
  SwitchStates.button_stateRB_A = digitalRead(ResumeButton_01);
}


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.