NRF24l01 troubles

I have been working on making a 12 channel transmitter and receiver for a project and I can't seem to get my codes to work. it worked the other day but I made some tweaks and it seized to function. Unfortunately I can't remember the changes I made to the code so I'm stuck. Anny help would be much appreciated. Here are the codes.

Transmitter:

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

RF24 radio(9, 10);

struct MyData {
  int state0;
  int state1;
  int state2;
  int state3;
  int state4;
  int state5;
  int state6;
  int state7;
  int state8;
  int state9;
  int state10;
  int state11;
};

MyData data;

void resetData() {

  data.state0 = 0;
  data.state1 = 0;
  data.state2 = 0;
  data.state3 = 0;
  data.state4 = 0;
  data.state5 = 0;
  data.state6 = 0;
  data.state7 = 0;
  data.state8 = 0;
  data.state9 = 0;
  data.state10 = 0;
  data.state11 = 0;

}

const byte address[6] = "00001";

const int CH0 = A0;
const int CH1 = A1;
const int CH2 = A2;
const int CH3 = A3;
const int CH4 = A4;
const int CH5 = 2;
const int CH6 = 3;
const int CH7 = 4;
const int CH8 = 5;
const int CH9 = 6;
const int CH10 = 7;
const int CH11 = 8;

void setup() {

  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();

  pinMode(CH0, INPUT);
  pinMode(CH1, INPUT);
  pinMode(CH2, INPUT);
  pinMode(CH3, INPUT);
  pinMode(CH4, INPUT);
  pinMode(CH5, INPUT_PULLUP);
  pinMode(CH6, INPUT_PULLUP);
  pinMode(CH7, INPUT_PULLUP);
  pinMode(CH8, INPUT_PULLUP);
  pinMode(CH9, INPUT_PULLUP);
  pinMode(CH10, INPUT_PULLUP);
  pinMode(CH11, INPUT_PULLUP);

}

void loop() {

  data.state0 = analogRead(CH0);
  data.state1 = analogRead(CH1);
  data.state2 = analogRead(CH2);
  data.state3 = analogRead(CH3);
  data.state4 = analogRead(CH4);
  data.state5 = digitalRead(CH5);
  data.state6 = digitalRead(CH6);
  data.state7 = digitalRead(CH7);
  data.state8 = digitalRead(CH8);
  data.state9 = digitalRead(CH9);
  data.state10 = digitalRead(CH10);
  data.state11 = digitalRead(CH11);
  
  radio.write (&data, sizeof(MyData));
  delay(1);

}

Receiver:

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

Servo CH0;
Servo CH1;
Servo CH2;
Servo CH3;
Servo CH4;

const int CH5 = A0;
const int CH6 = 2;
const int CH7 = 4;

RF24 radio(7, 8); //(CE, CSN)

const byte address[6] = "00001";

struct MyData {
  int state0;
  int state1;
  int state2;
  int state3;
  int state4;
  int state5;
  int state6;
  int state7;
  int state8;
  int state9;
  int state10;
  int state11;
};

MyData data;

void resetData() {

  data.state0 = 0;
  data.state1 = 0;
  data.state2 = 0;
  data.state3 = 0;
  data.state4 = 0;
  data.state5 = 0;
  data.state6 = 0;
  data.state7 = 0;
  data.state8 = 0;
  data.state9 = 0;
  data.state10 = 0;
  data.state11 = 0;

}

void setup() {

  radio.begin();
  radio.openReadingPipe(0, address[6]);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  Serial.begin(9600);

  CH0.attach(3);
  CH1.attach(5);
  CH2.attach(6);
  CH3.attach(9);
  CH4.attach(10);

  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);

}

void loop() {

  if (radio.available()) {
    radio.read(&data, sizeof(MyData));
  }

  int CH0val = map(data.state0, 0, 1023, 0, 180);
  int CH1val = map(data.state1, 0, 1023, 0, 180);
  int CH2val = map(data.state2, 0, 1023, 0, 180);
  int CH3val = map(data.state3, 0, 1023, 0, 180);
  int CH4val = map(data.state4, 0, 1023, 0, 180);
  int CH5val = data.state5;
  int CH6val = data.state6;
  int CH7val = data.state7;
  int CH8val = data.state8;
  int CH9val = data.state9;
  int CH10val = data.state10;
  int CH11val = data.state11;

  CH0.write(CH0val);
  CH1.write(CH1val);
  CH2.write(CH2val);
  CH3.write(CH3val);
  CH4.write(CH4val);

  if (CH5val == 0) {
    digitalWrite(2, HIGH);
  }

  else {
    digitalWrite(2, LOW);
  }

  if (CH6val == 0) {
    digitalWrite(4, HIGH);
  }

  else {
    digitalWrite(4, LOW);
  }

  if (CH7val == 0) {
    digitalWrite(A0, HIGH);
  }

  else {
    digitalWrite(A0, LOW);
  }

  if (CH8val == 0) {
    digitalWrite(A1, HIGH);
  }

  else {
    digitalWrite(A1, LOW);
  }

  if (CH9val == 0) {
    digitalWrite(A2, HIGH);
  }

  else {
    digitalWrite(A2, LOW);
  }

  if (CH10val == 0) {
    digitalWrite(A3, HIGH);
  }

  else {
    digitalWrite(A3, LOW);
  }

  if (CH11val == 0) {
    digitalWrite(A4, HIGH);
  }

  else {
    digitalWrite(A4, LOW);
  }
}

I can't remember the changes, either. So, best solution is to start over, now that you have more experience.
Paul

And I might add, TEST after each change in code.

I did try starting over. It didn't work out very well. I had a bunch more problems than simply not receiving proper states. Thanks for the advice though :slight_smile:

And did you solve each of the problems, one at a time?
Paul

Only one. I had accidentally deleted the line Radio.begin();

I suspect that there is something wrong with my logic.

"It doesn't work any more" conveys no useful information. What does the code actually do? How does that differ from what you want the code to do?

Insert some serial prints to see what is sent and what is received. Serial prints are your best troubleshooting tool.

Provide a sample of what is sent and a sample of what is received.

I have tried adding serial prints. they didn't reveal anything that would help unfortunately. And it sorry about it "it doesn't work anymore" to be more specific, the values on the receiver end refuse to change as the ones on the transmitter end do. I'm getting a strong connection and all the hardware is working properly I tested it with simpler one channel code and all was well.

Problem solved. thanks everyone for your input. the problem was that the address on the receiver end got messed up.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.