So, my code is evolving due to picking up things from tutorials like the one @robin2 posted..
Now if I press the button, the LED goes on and stays on without the ability to change.
Transmit..
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
int BUTTON = 5;
int val;
int old_val=0;
int msg[1];
byte state = 0;
byte brightness[] = {0, 75, 150, 255};
RF24 myRadio (9,10); // CNS, CE
bool radioNumber = 1;
byte addresses[][6] = {"1Node","2Node"};
bool role = 1;
void setup() {
myRadio.begin();
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
myRadio.setPALevel(RF24_PA_LOW);
// Open a writing and reading pipe on each radio, with opposite addresses
myRadio.openWritingPipe(addresses[1]);
}
void loop() {
pinMode(BUTTON, INPUT);
val=digitalRead(BUTTON); //check status of pushbutton
if((val==HIGH)&&(old_val==LOW)) //button pressed
{
state++; //increment state
delay(10); //debounce consideration
if(state>4) //want only 4 brightness options
{
state=1;
delay(5);
}
}
old_val=val;
if(state==1)
{msg[0] = 0;
}
else if(state==2)
{msg[0] = 75;
}
else if(state==3)
{msg[0] = 150;
}
else if(state==4)
{msg[0] = 255;
myRadio.write(msg[1], 1);
}
}
Recieve...
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 myRadio (9, 10);
bool radioNumber = 0;
byte addresses[][6] = {"1Node","2Node"};
int msg[1];
int LED = 3;
bool role = 0;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
myRadio.begin();
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
myRadio.setPALevel(RF24_PA_LOW);
// Open a writing and reading pipe on each radio, with opposite addresses
myRadio.openReadingPipe(1, addresses[1]);
}
void loop() {
if (myRadio.available())
{
while (myRadio.available())
{
myRadio.read(msg, 2);
}
analogWrite (LED, msg[0]);
}
}
I feel as if I'm spiraling.