nRF24l01 LED; Momentary Switch State/Brightness

What I’m attempting to do is create an LED light that is on the receiving end of a radio frequency circuit. On the transmitting side is a momentary switch. The light is off to start and every time the switch is pushed the LED will increase in brightness until it has reached its maximum of 255, then the next button action will turn it off. 0, 75, 150, 255, back to off.

Would anyone tell me why this isn’t working, or if there’s a better way to approach this with the same components?

Tx Side…

Arduino Pro Mini 3.3v
nRF24l01
Momentary Switch

Rx Side…

Arduino Uno
nRF24l01
White LED

Tx Code…

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

#define BUTTON A1 //assign pin 7 to pushbutton

int val=0; //val used to check current status of pushbutton later in the program
int old_val=0; //old_val to check previous status
int state=0; //state to check button presses
int dataTransmitted;
int msg;

RF24 myRadio (9, 10);
byte addresses[][6] = {"1Node"};  

void setup()
{

  pinMode(BUTTON,INPUT);

  dataTransmitted = 100; 
  myRadio.begin(); 
  myRadio.setChannel(108) ;
  myRadio. setPALevel(RF24_PA_MAX); 

  myRadio.openWritingPipe( addresses[0]); 

  delay(5); 
  
}

void loop()
{
  val=analogRead(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;
    
  }
  else if(state==2)
  {msg = 75;
    
  }
  else if(state==3)
  {msg = 150;
    
  }
  else if(state==4)
  {msg = 255;
    
  }
myRadio.write( &msg, sizeof (msg) );

}

Rx Code…

#include <SPI.h>   // Comes with Arduino IDE
#include "RF24.h"  // Download and Install (See above)


/*-----( Declare Constants and Pin Numbers )-----*/
#define LED 5 //White LED



/*-----( Declare objects )-----*/
// (Create an instance of a radio, specifying the CE and CS pins. )
RF24 myRadio (7, 8); // "myRadio" is the identifier you will use in following methods
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int dataReceived;  // Data that will be received from the transmitter

void setup()   /****** SETUP: RUNS ONCE ******/
{
 
  myRadio.begin();  // Start up the physical nRF24L01 Radio
  myRadio.setChannel(108);  // Above most Wifi Channels
  // 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_MAX);  // Uncomment for more power

  myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
  myRadio.startListening();

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{

  if ( myRadio.available()) // Check for incoming data from transmitter
  {
    while (myRadio.available())  // While there is data ready
    {
      myRadio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)
    }
    // DO something with the data, like print it
   analogWrite (LED, dataReceived);
  } //END Radio available

}

I appreciate any help I can get. Hopefully I provided enough information.

Have a look at this Simple nRF24L01+ Tutorial

I presume you can get the correct LED behaviour when you have the LED and the switch on the same Arduino. If not, start with that.

...R

Robin2:
Have a look at this Simple nRF24L01+ Tutorial

I presume you can get the correct LED behaviour when you have the LED and the switch on the same Arduino. If not, start with that.

...R

I've gotten it to work on a single Arduino without RF..

I've gone over the tutorial, but nothing. I did manage to change the CE and CSN pins on the uno to 9,10 from 7, 8.. I caught that in the tutorial. It still wont light. Sometimes the led I have connected to the uno will light without the other Arduino even plugged in.

ChristopherBishop:
I've gone over the tutorial, but nothing.

Have you managed to get one of my examples working without any changes? That's where you should start. When the wireless communication works reliably you can add other features.

If you can't get any of my examples to work then post the code that YOU have uploaded to YOUR Arduinos and tell us exactly what each Arduino does.

...R

"val=analogRead(BUTTON); //check status of pushbutton"

Is not how you read a digital input.

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.

You cannot "evolve" code that does not work. You're making needless and unnecessary changes at this point.

Hint:

Your original RX sketch works. The TX sketch radio code will work but there are simple logic errors preventing it.

Go back to your original code and debug your transmit logic. Add a Serial.begin(9600); block in setup() and then use Serial.println commands to echo values to the console to figure out what you're transmitting and you'll see some of your mistakes right away. Print all of the values of interest like this:

    Serial.print("val= ");
    Serial.print(val);
    Serial.print("  old_val= ");
    Serial.print(old_val);
    Serial.print(" state= ");
    Serial.println(state);

Among the errors is constant transmission. You only need to transmit a message when "state" changes. Fixing that will simplify the logic and you may see where you've gone wrong.

Follow-up. After reloading the original code, I find that the digitalRead instead of the incorrect analogRead is all that is required. The logic works, I thought I saw nesting errors with all the unnecessary braces. Auto-reformat provided the needed clarity.

Perhaps your pushbutton isn't wired correctly, that should be apparent from the serial output.

avr_fred:
You cannot "evolve" code that does not work. You're making needless and unnecessary changes at this point.

Hint:

Your original RX sketch works. The TX sketch radio code will work but there are simple logic errors preventing it.

Go back to your original code and debug your transmit logic. Add a Serial.begin(9600); block in setup() and then use Serial.println commands to echo values to the console to figure out what you're transmitting and you'll see some of your mistakes right away. Print all of the values of interest like this:

    Serial.print("val= ");

Serial.print(val);
   Serial.print("  old_val= ");
   Serial.print(old_val);
   Serial.print(" state= ");
   Serial.println(state);




Among the errors is constant transmission. You only need to transmit a message when "state" changes. Fixing that will simplify the logic and you may see where you've gone wrong.

Okay. So I've done this. It's interesting to say the least.

When I press the button, nothing happens. Not on the monitor or physically.

However when I press the reset button on the Arduino, the serial monitor displays "0 old_val= 0 state= 0"

So clearly the Arduino isn't even registering the fact that I'm pressing the button. old_val will never change without a new val to replace it. But state should be 1 as default.

But state should be 1 as default.

Why? Nothing in the code makes it so.

You should also initialize "state" to 1, not 0. 0 is an invalid value per your logic.

Sorry, I forgot to mention that of course I changed the tx code object invocation to the usual RF24 myRadio (7,8);

FWIW, I'm testing with two UNOs.

avr_fred:
Sorry, I forgot to mention that of course I changed the tx code object invocation to the usual RF24 myRadio (7,8);

FWIW, I'm testing with two UNOs.

And you can get it to work with two UNOs?

ChristopherBishop:
I feel as if I'm spiraling.

Did you do what I suggested in Reply #3.

That's how I approach all my projects and I generally get them to work.

,,,R