Sending new integer value to change value of integer at receiver usingNRF2401

I am trying to send a value of an integer to the other Arduino using the nrf2401 radio component, I was following a tutorial and it showed that the integer had to be made on both arduinos. I set them both as zero and after the parameters needed for the integer to change are met on the transmitter, the new value does not appear on the receiver serial monitor.

It might be useful to provide a link to that tutorial.

Further nobody will be able to help you if you don't post the code forvboth the transmitter and the receiver.

A photo of the wiring of each might be useful as well. Which Arduinos are you using?

I will take a SWAG and say you are powering one or more radios with the Arduino, that is a No-No. Sometimes it might work but many times it just gives you problems. Use a separate power supply for the radio(s). Here is some information that may help:
Gil's Crispy Critter Rules, they apply to processor hardware:
Rule #1. A Power Supply the Arduino is NOT!
Rule #2. Never Connect Anything Inductive to an Arduino!
Rule #3 Don't connecting or disconnecting wires with power on.
Rule #4 Do not apply power to any pin unless you know what you are doing.
LaryD's Corollary's
Coro #1 when first starting out, add a 220R resistor in series with both Input and Output pins.
Coro #2 buy a DMM (Digital Multi-meter) to measure voltages, currents and resistance.
Violating these rules tends to make crispy critters out of Arduinos.
Hint: It is best to keep the wires under 25cm/10" for good performance.

transmitter

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

#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <stdint.h>

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
// these pins define the pins being used by the Arduino^^
#define TS_MINX 118
#define TS_MINY 106
#define TS_MAXX 950
#define TS_MAXY 933
#define YP A3
#define XM A2
#define YM 9
#define XP 8
// this code calibrates the screen
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define MINPRESSURE 10
#define MAXPRESSURE 1000

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// these define the pins used to activate the screen, i believe that the 300 is the Maximum resistence of the screen

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xf81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

//transmitter

RF24 radio(10, 7); // CE, CSN
const byte address [6] = ("TEXTC");

int currentscreen = 0;
bool Camera = 0;
bool tracker = false;
bool Homevar = false;
bool backbutton = 1;
bool welcome = true;
int button = 0;
//bool button = false;
// A touch variable that will switch depending on parameters for radio communication

void Home() {
  tft.fillScreen(RED);
  tft.setCursor(60, 100);
  tft.setTextSize(2);
  tft.print("CHOOSE APPLICATION");
  tft.fillRect(10, 10, 75, 75, BLUE);
  tft.fillRect(125, 10, 75, 75, BLUE);
  tft.fillRect(240, 10, 75, 75, BLUE);
  // creating buttons for applications, first test out using LED's
}
void Welcome() {
  tft.fillScreen(WHITE);                //WHat to fill the screen colour with- colours stated above
  tft.drawRect(0, 0, 319, 240, WHITE);  //the rectangularshape that the screen fills
  tft.setCursor(5, 5);                  //set cursor is where text will begin on the screen, top right corner of the text
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("WELCOME LKAT");
  //add parameters
  tft.fillRect(50, 180, 210, 40, BLACK);
  tft.drawRect(50, 180, 210, 40, GREEN);
  tft.setCursor(60, 190);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
}

void LEDB() {
  tft.setCursor(5, 5);
  tft.fillScreen(BLUE);
 
  tft.print("HELLO");
}

void LEDR() {
  tft.setCursor(5, 5);
  tft.fillScreen(RED);
  tft.print("HELLO");
}

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);//00001
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(3);  //changing this value between 1 and 0 will change the layout of the text on screen. landscape portarait
  currentscreen = 0;

  Welcome();
}

bool buttonPressed(const int x, const int y, const int buttonX, const int buttonY, const int buttonWidth, const int buttonHeight) {
  return ( (x >= buttonX) && (x <= buttonX + buttonWidth) && (y >= buttonY) && (y <= buttonY + buttonHeight) );
}

void loop() {
  
  TSPoint p = ts.getPoint();
      pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
  const char txt []= "Connection made";
  radio.write(&txt, sizeof(txt));
  delay(1000);

   if (p.z > ts.pressureThreshhold) {
    
    Serial.print("\tPressure = "); Serial.println(p.z);
    int y = map(p.x, TS_MINX, TS_MAXX, 0, 240);
    int x = map(p.y, TS_MINY, TS_MAXY, 0, 320);
    button = 1;
  radio.write(&button, sizeof(button));
  
 // delay(1000);

if (welcome == true) {
      if (currentscreen == 0 ) {
        if (buttonPressed(x, y, 50, 180, 210, 40)) {// x and y map variables and then the starting x,y coordinate and then width and then hieght
          currentscreen = 1;
          welcome = false;
          Homevar = true;
          Home();
          
        }
      }
    }
if (currentscreen == 1) {
  if( Homevar == true && welcome == false){
      if (buttonPressed(x, y, 10, 10, 75, 75)) {// starting xy coord and then width and height
       LEDB();
       currentscreen=2;
       
      //if(currentscreen==2 && buttonPressed(x,y,0,0,319,240)){//tft.darwRect(0,0,319,240);
        
       
        
      }
   else if (buttonPressed(x, y, 125, 10, 75, 75)) {
   LEDR();
   currentscreen=3;

   }
      else if (buttonPressed(x, y, 240, 10, 75, 75)) {
        tft.fillScreen(GREEN);
        currentscreen=4;
        if(currentscreen=4 ){
        

        }
  }
  }
}

   }
}

RECIEVER

#include <SPI.h>
 #include <nRF24L01.h>
#include <RF24.h>
#define  led 10

RF24 radio(7, 8); // CE, CSN
const byte address [6] = ("TEXTC");

int button = 0 ;
 
void setup() {
 Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0,address);//00001
   radio.setPALevel(RF24_PA_MIN);
    //radio.startListening();
  // tells the module to stop listening out for code, this sets it up as a transmitter
 pinMode(led, OUTPUT);
}

void loop() {
  radio.startListening();
       while (!radio.available());
    char txt[32] = "" ;
    radio.read(&txt, sizeof(txt));
   radio.read(&button,sizeof(button));
    Serial.println(button);
//digitalWrite(led, HIGH);
   
   if (button==1){
    Serial.println(txt);
   
   }```

i am trying ot send an variable when the tft lcd is pressed and then have the other arduino read it. whne the variable changes the reciver should turn on an led

i have been powering them with the arduinos and it has worked, the only issue is how to write the code so it reads a variable

Works fine for the LCD gives you weird problems with the radio. Sometimes it may work, depending on the luck of the draw. It is your choice but you asked for help and my suggestion is to power the radios with an external power supply.

got it, thanks for the suggestion will try it out.

1 Like

Move the radio.startListening(); call to setup.

Don't block your sketch for no reason,

       while (!radio.available());

switch to

  if (radio.available()) [
    // process packet  
  }

Don't read two packets if you received one. This could be the source of the malfunction.

    radio.read(&txt, sizeof(txt));
    radio.read(&button,sizeof(button));

Put both data into a struct, and send them as a single packet.
Keep in mind, that packets have a maximum length of 32.

i got this error "initializer for flexible array member 'const char' "

struct Data_P{
  bool touchvar = false;
  const char txt []= "Connection made";
} ;

Give the array a fixed length.
I would not use char arrays, I think codes are shorter and simpler to process.

RF24 radio(10, 7); // CE, CSN
const byte address [6] = ("TEXTC");
const char txt []= "Connection made";

struct Data_P {
  bool touchvar = false;
 char text[sizeof(txt)];
/////////
void loop() {
  
  TSPoint p = ts.getPoint();
      pinMode(XM, OUTPUT) ;
    pinMode(YP, OUTPUT) ;
  
  radio.write(&Data_P, sizeof(Data_P))
  delay(1000);

   if (p.z > ts.pressureThreshhold) {
    touchvar =!touchvar 
} ;

i get this error after the if statement and the radio write command

"expected primary-expression before ' ' token"

    Serial.println(touchvar);
//digitalWrite(led, HIGH);
    radio.write(&Data_P, sizeof(Data_P));
   if (touchvar == HIGH ){
    Serial.println(txt);
   
   }

Then it says that touchvar was not declared even though i created it as a bool in the struct

Hmm, maybe you want to close your struct definition with a }? You might have more errors like that.

  1. Tools / Autoformat in the IDE might help you to identify this type of problems (it will not solve them).
  2. In future, please post complete code, not snippets. The error might not always be where you think it is.
  3. In future, please post complete error messages verbatim (preferably using code tags).

for an example of sending data (int, float, etc)in a structure have a look at esp-32 with NRF24l01
change the CE and CSN setting to suit the microcontroller you are using, e.g. the ESP32 is

RF24 radio(4, 5);  //CE and CSN

e.g. a RPi pico rp2040 is

RF24 radio(20, 17);  // CE, CSN

sorry my mistake

#include <SPI.h>
 #include <nRF24L01.h>
#include <RF24.h>
#define  led 10

RF24 radio(7, 8); // CE, CSN
const byte address [6] = ("TEXTC");

const char txt []= "Connection made";

struct Data_P {
  bool touchvar = false;
 char text[sizeof(txt)];
} ;

 Data_P data ;
void setup() {
 Serial.begin(9600);
  radio.begin();
   radio.startListening();
  radio.openReadingPipe(0,address);//00001
   radio.setPALevel(RF24_PA_MIN);
    //radio.startListening();
  // tells the module to stop listening out for code, this sets it up as a transmitter
 pinMode(led, OUTPUT);
}

void loop() {
       if (radio.available()){
    char txt[32] = "" ;
  
    Serial.println(touchvar);
//digitalWrite(led, HIGH);
    radio.read(&data, sizeof(Data_P));
   if (touchvar == HIGH ){
    Serial.println(txt);
   
   }

Compilation error: 'touchvar' was not declared in this scope

toutchvar is a member of the Data_P structure
you access it so

    Serial.println(data.touchvar);

I believe it works now, I am recieving a starting value of 0 ( or 1, not sure why) and when i press the TFT LCD touchscreen it changes the value of of touchvar. However i can't see the 'txt' variable displayed in the serial monitor when the touchvar value is 1 and the LED doesn't turn on when the value of touchvar is 1.


is there an error in the code causing the message to not appear and the LED to not turn on

#include <SPI.h>
 #include <nRF24L01.h>
#include <RF24.h>
#define  led 10

RF24 radio(7, 8); // CE, CSN
const byte address [6] = ("TEXTC");

const char txt []= "Connection made";

struct Data_P {
  bool touchvar = false;
 char text[sizeof(txt)];
} ;

 Data_P data ;
void setup() {
 Serial.begin(9600);
  radio.begin();
   radio.startListening();
  radio.openReadingPipe(0,address);//00001
   radio.setPALevel(RF24_PA_MIN);
  //   radio.startListening();
  // tells the module to stop listening out for code, this sets it up as a transmitter
 pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(led, LOW);
       if (radio.available()){
            radio.read(&data, sizeof(Data_P));
    char txt[32] = "" ;
    Serial.println("touchvar variable =" );
    Serial.println(data.touchvar);
//digitalWrite(led, HIGH);

   if (data.touchvar == HIGH ){
    Serial.println(data.text);
   digitalWrite(led, HIGH);
   }
 // Se

not sure why data.text does not show anything - could you upload the latest transmitter code
the LED is set high in the if() but on entry to loop() you set it low - probably on for a few microseconds - possibly add a delay??

void loop() {
  digitalWrite(led, LOW);    // <<<<<<<<<<<<<<< set low
       if (radio.available()){
            radio.read(&data, sizeof(Data_P));
    char txt[32] = "" ;
    Serial.println("touchvar variable =" );
    Serial.println(data.touchvar);
//digitalWrite(led, HIGH);

   if (data.touchvar == HIGH ){
    Serial.println(data.text);
   digitalWrite(led, HIGH);         //  <<<<<<<<<< set high 
delay(1000);                               // <<<<<<<<<<<  try a delay???
   }

or use millis() to keep it on for a time