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
}
}