nrf24l01 causing arduino code not to run

hey yall
so ive been working on a rc tank project. the issue is that the radio.write for the nrf24l01 causes the code not to run. if i remove the radio.write the code runs fine except i cant transmit. ive double and triple checked connections and have tried 5-6 different modules and same result. even a generic hello world code doesnt run. cant check if radio.read has same issue since cant send data.

ive verified that the 2 switches and the pot are being read via serial monitor, but as soon as i load code with radio.write monitor doesnt display anything even if doing a serial print before the radio write

controller code

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

RF24 radio(9, 8);

byte address[6] = "00001";

byte pos1 = 2;
byte pos2 = 3;
byte pos3 = 4;
byte pos4 = 5;
int pot = A0;

void setup() {
  pinMode(pos1, INPUT_PULLUP);
  pinMode(pos2, INPUT_PULLUP);
  pinMode(pos3, INPUT_PULLUP);
  pinMode(pos4, INPUT_PULLUP);
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
  delay(100);
}

void loop() {
  int spd = analogRead(pot);
  spd = map(spd, 0, 1023, 0, 254);
  byte state1 = digitalRead(pos1);
  byte state2 = digitalRead(pos2);
  byte state3 = digitalRead(pos3);
  byte state4 = digitalRead(pos4);

  radio.write(&spd, sizeof(spd));
  radio.write(&state1, sizeof(state1));
  radio.write(&state2, sizeof(state2));
  radio.write(&state3, sizeof(state3));
  radio.write(&state4, sizeof(state4));

  Serial.println(spd);
  Serial.println(state4);
  Serial.println(state3);
  Serial.println(state2);
  Serial.println(state1);
}

tank code

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

RF24 radio(4, 5);
byte address[6] = "00001";

int m1 = 6;  // motor 1 enable
int in1 = 8;  // motor 1 input 1
int in2 = 7;  // motor  1 input 2
int m2 = 5;  // motor 2 enable
int in3 = 6;  // motor 2 input 1
int in4 = 12;  // motor 2 input 2

void setup() {
  pinMode(m1, OUTPUT);  // set pin as output
  pinMode(in1, OUTPUT);  // set pin as output
  pinMode(in2, OUTPUT);  // set as output
  pinMode(m2, OUTPUT);  // set as output
  pinMode(in3, OUTPUT);  // set as output
  pinMode(in4, OUTPUT);  // SET AS output
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
  delay(100);
}

void loop() {

  byte state1;
  byte state2;
  byte state3;
  byte state4;
  int spd;

  if (radio.available()) {
    radio.read(&state1, sizeof(state1));
    radio.read(&state2, sizeof(state2));
    radio.read(&spd, sizeof(spd));
    radio.read(&state3, sizeof(state3));
    radio.read(&state4, sizeof(state4));
    Serial.println(spd);
  }
  if (state1 == LOW && state2 == HIGH) {  // if swith in forward postion run motor forward a set speed
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    analogWrite(m1, spd);  //  set motor 1
  }

  if (state2 == LOW && state1 == HIGH) {  // if switch in rear position run motor backwards at set speed
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    analogWrite(m1, spd);
  }
  if (state1 == HIGH && state2 == HIGH) {
    analogWrite(m1, 0);
  }
    if (state3 == LOW && state4 == HIGH){
      analogWrite(m2, spd);
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH);
    }
    if (state3 == HIGH && state4 == LOW){
      analogWrite(m2, spd);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW);
    }
if (state3 == HIGH && state4 == HIGH){
  analogWrite(m2, 0);
}
}

You’ve have two fundamental errors there, in addition to calls to delay(100). Just delete those.

Transmit mistake: You need to encode the state number into each message.

Receive mistake: when radio.available is true, there is only one message available. You’re reading five messages in succession which will not work. You read the message, decode the state, take the appropriate action and loop, waiting for the next message.

i am a fellow n00b maybe a few steps ahead, i found out how to use struct for the data and that means you just write/read ONCE. (the nRF24 can send packets of up to 32 bytes, so you lump it all together instead of repeating it for each variable.)

have a look at the code on this screen;

the whole video is rather long, half an hour - but very useful for any beginner as the code walk-thru is really easy to follow.

What do you mean encode the state number in each message. I thought the radio.write(&state1...... Did that or is there something I'm missing

What bugs me the most is everything worked two nights ago. But the next morning when I hooked the other motor up suddenly nothing worked right. Fml

zjett23:
What bugs me the most is everything worked two nights ago.

what does "everything worked" mean ?

zjett23:
But the next morning when I hooked the other motor up suddenly nothing worked right. Fml

seems to me, the previous scenario was NOT really working but only seemed that way because 'something happened'.

Idk I had initially only hooked up one motor just to test everything. Tweaked it all for a few days and then 2 nights ago it worked. The controller was sending the proper commands and the tank motors were responding to all commands and it worked perfectly. That's why I have no clue. The next morning all I did was connect the wires for the second motor and add the if statements for motor 2. Nothing has worked since. For some reason as soon as I include radio.write the code will not work. Even the simplest thing, I've tried to run with radio.write won't work. Take it out and code runs fine.

zjett23:
...I did was connect the wires for the second motor and add the if statements for motor 2. Nothing has worked since. For some reason as soon as I include radio.write the code will not work. ...

so what does the code look like when it last worked with the radio.write ?

The same as it does now. Except it doesn't work. It won't even print the data to the monitor. I didnt change any code on the controller

zjett23:
The same as it does now. Except it doesn't work.
...
I didnt change any code on the controller

then it is a hardware issue.

or your code was NOT correct to begin with and "worked by coincidence".

have you read the post by WattsThat ?

Receive mistake: when radio.available is true, there is only one message available. You're reading five messages in succession which will not work.

i already suggested you use struct

Yes I did see his message though what's sure what he meant with the transmitter code. As far as working by coincidence. I didn't think you could get it to work by happenstance.

I've tried 5-6 nfr24l01 modules and same result. Even running a basic hello message code it doesn't work. It could be hardware I just think the odds of all of the first 6 I pulled out being bad are slim but idk

zjett23:
Yes I did see his message though what's sure what he meant with the transmitter code.

what he meant is you're doing it wrong.
i have never tried to radio.read five times in a row, so i don't know the correct way to do it - which is why i suggested to use struct instead, then you only write ONCE (for all your variables) and ALSO read only once.

zjett23:
As far as working by coincidence. I didn't think you could get it to work by happenstance.

i have experienced this often when learning with the nRF24 modules - it's not happenstance, it's just something happening that you think is working but is actually NOT.

sometimes i think i get "acknowledgment" (from bad code) when it's NOT because of something else more obviously wrong.

Oh I meant on the part about encoding the stage to the data I'm sending. I agree that a struc like you suggested is a much better way to send and read the info.

state NOT stage.

i think you should just re-write the code for JUST the data transmission, confirm you receive exactly what the controller is sending.

i just noticed you wrote speed first, but then read it third ?

just redo the whole thing and convert it to struct.

If you don’t understand how to implement the data struct, please say so. You always learn more when you firgure it out for yourself but when you’ve hit a wall, you stop absorbing new information. Don’t be afraid to say you’re stuck or as my pappy used to say, “I’m up a tree looking for wood”.

Ok so I'll redo the code to day and change the data to a structure. Gotta read up on it first since I've never used one yet. I'll let y'all know how that effects things.

ok so i went ahead and redid the code using a struct rather than just read/write 5-6 times.
this seems to have resolved the issue on the receiver end of thing since now it does print data to the serial monitor, however its only zeros so im guessing its not receiving anything from the transmitter. tried swapping out transceivers and same result. as well the controller doesnt print anything to the monitor. as before though if you remove the radio.write it does detect and print the proper switch states. btw i do prefer the struct better than the way it was so thanks on that regard.

/* MCAT CONTROLLER PROGRAM*/

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h> // include nfr24l01 library

RF24 radio(7, 6); // set ce and csn pins

byte address[6] = "00001";  // set address

byte pos1 = 2; // set pin for switch position
byte pos2 = 3; // set pin for switch position 2
int pot = A0; //set pot pin

struct cntrl {  // creat control structure
  byte state1;  // assign state1
  byte state2;  // "  " state2
  int spd;  // "  " spd
};
cntrl data;

void setup() {
  pinMode(pos1, INPUT_PULLUP); // turn on internal resistor
  pinMode(pos2, INPUT_PULLUP); // "      "
  radio.begin();  //  start radio
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);  // set power level
  radio.stopListening();  //  set to transmit
  Serial.begin(9600);
}

void loop() {
  data.spd = map(analogRead(pot), 0, 1023, 0, 254); // read pot assing to speed and map to pwm range
  data.state1 = digitalRead(pos1);
  data.state2 = digitalRead(pos2);

  radio.write(&data, sizeof(cntrl));

  Serial.println(data.spd);
  Serial.println(data.state1);
  Serial.println(data.state2);
}
/*MCAT VEHICLE PROGRAM*/

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

RF24 radio(8, 12);
byte address[6] = "00001";

byte m1 = 3;
byte in1 = 2;
byte in2 = 4;
byte m2 = 5;
byte in3 = 6;
byte in4 = 7;

struct cntrl {
  byte state1;
  byte state2;
  int spd;
};
cntrl data;

void setup() {
  pinMode( m1, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(&data, sizeof(cntrl));
  }
  Serial.println(data.spd);
  Serial.println(data.state1);
  Serial.println(data.state2);
}

On the transmit side, at the end of loop(), add a delay(100) or some other reasonable update rate, you’re just spewing packets at maximum warp drive.

On the receiver side, place the print statements inside the braces for the if (radio.available) so it only prints when data is received rather than every pass through loop().

The code you have should work with the minor changes I mentioned above.

But.

Your transmit side code isn’t validating the message sent, it just chucks it out into space and you don’t know if it was received or not. It’s easy to check that as the write method returns success or failure for the message. If you want to tweak it, have a look at the basic tutorials which are always a good starting place for most NRF24 applications.

Simple nRF24L01+ 2.4GHz transceiver demo

unfortunately still didnt work though after trying to upload code after a small update the mega2560 i was using for the controller times out during up load. avrdude: stk500v2_getsync() this is the error message, have tried different cables and usb ports to no avail.

im bout to move to Amish country and become a Luddite lol