nRF24L01 can't send a variable value (Sending string works)

Hi,
I’ve got 2 nRF24L01 connected to my PC.
I’ve downloaded the example code and everything is working fine.
However the example code transmits a string “Hello world”.
I want to replace the string with a variable that reads the state of a switch.

Say: When a switch is closed the transmitter board sends a “1”, when the switch is open the board transmits a “0”.

I spent all afternoon trying to have this working but to no avail.
However on the Serial post I can see the value of the switch.

Here is my code:

    #include <SPI.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN pins
    const byte address[6] = "00001";
int buttonPin = 2;
int buttonState = 0;
    
    void setup() {
Serial.begin(9600);      
      radio.begin();
      radio.openWritingPipe(address);
      radio.setPALevel(RF24_PA_MIN);
      radio.stopListening();

pinMode(buttonPin, INPUT);      
    }
    
    void loop() {
      buttonState = digitalRead(buttonPin);
const char text[] = (buttonState,1);
Serial.println(buttonState);
 radio.write(&text, sizeof(text));
 delay(500);
    }

I would really appreciate if somebody could help me into the right direction.
Thank you very much.

LRAT:
I've got 2 nRF24L01 connected to my PC.

I connect my NRFs to Arduinos or other microcontrollers, not to a PC.

LRAT:
I want to replace the string with a variable that reads the state of a switch.

But against your intention you send a string initialized by the state, strange.
Claiming that the resulting string will not be sent contradicts the "it is working with strings".

For sending the button state I would use

 radio.write(&buttonState, sizeof(buttonState));

BTW I would not use an int to hold values that can only be HIGH or LOW.

Thanks Whandall,

I’ve got it working now. I got rid of the code line “const char text = (buttonState,1));” and applied your recommendations. It’s now working fine.
The reason why I’ve got the two boards connected to my pc is because it’s a developing environment.
Both UNO-boards are connected to a different COM-port and I’ve got two Arduine IDE instances open.
Both boards are running indepently from one another.
I want to use the transmitter board to collect pulses from a TBRG (Tipping Bucket Rain Gauge). These tips will then be transmitted to my pc where I can record the data.

My next step will be to have a RTC generating a timestamp every time a tip on the TBRG occurs. This will then be send to my home pc where the tips will be added so that I can see the toatl of rainfall for the day.

Here is my modified code (Working):

   #include <SPI.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN pins
    const byte address[6] = "00001";
int buttonPin = 2;
int buttonState = 0;
    
    void setup() {
Serial.begin(9600);      
      radio.begin();
      radio.openWritingPipe(address);
      radio.setPALevel(RF24_PA_MIN);
      radio.stopListening();

pinMode(buttonPin, INPUT);      
    }
    
    void loop() {
      buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
 radio.write(&buttonState, sizeof(buttonState));
 delay(500);
    }

Many thanks for your help!
Cheers,

Luc

See if this wurkz, untested.

    #include <SPI.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN pins
    const byte address[6] = "00001";
byte buttonPin = 2;
byte buttonState = 0;
   
    void setup() {
Serial.begin(9600);     
      radio.begin();
      radio.setRetries(15,15);
      radio.setPayloadSize(1);
      radio.openWritingPipe(address);
      radio.setPALevel(RF24_PA_MIN);
      radio.stopListening();

pinMode(buttonPin, INPUT);     
    }
   
    void loop() {
       buttonState = digitalRead(buttonPin);
      //const char text[] = (buttonState,1);
      Serial.println(buttonState);
      radio.write(&buttonState, sizeof(buttonState));
      delay(500);
    }

Howdy Outsider,

Your suggested code works as well.
I just can’t understand what that line of code actually does :-0
Cheers,

Luc

This Simple nRF24L01+ Tutorial may be of interest.

...R