Hi, I am making a RC car using the nRF24l01 transceiver and the RF24 library. This is my code:
Controller:
//RADIO CODE FROM THE EXAMPLE IN RF24 LIBRARY: https://nrf24.github.io/RF24/examples_2GettingStarted_2GettingStarted_8ino-example.html
//RADIO
#include <RF24.h>
#include <RF24_config.h>
#include <nRF24L01.h>
#include <printf.h>
#define CE_PIN 7
#define CSN_PIN 8
RF24 radio(CE_PIN, CSN_PIN);
const uint8_t pipeOut = 0xE8E8F0F0E1LL;
bool radioNumber = 1;
bool role = true;
int payload = 0;
//CONTROLS
int SandDPot = A0; //Stop or Go, Forwards or Backwards, and Speed
int turnPot = A1; //Turn degrees
int order = 0; /* 0 = send stop,go,forwards&backwards
1 = send speed
2 = send turn degrees*/
int SandD; //see line 23
bool gORs; //go or stop
bool fORb; //forwards or backwards
int speed;
int turn;
void setup() {
//RADIO
if (!radio.begin()){
while (1) {}
}
radio.setPALevel(RF24_PA_LOW);
radio.setPayloadSize(sizeof(payload));
radio.openWritingPipe(pipeOut);
radio.stopListening();
//CONTROLS
pinMode(SandDPot, INPUT);
pinMode(turnPot, INPUT);
gORs = false; //stopped
fORb = true; //if on go, go forward
}
void loop() {
if (order != 2){ //not sending turn
SandD = analogRead(SandDPot) - 511.5; //get input make into range of 511 to -511
if (order == 0){ //sending stop,go,forwards&backwards
if (SandD != 0){ //0 = stopped
gORs = true; //moving
if (SandD > 0){ //if > 0 moving forward
fORb = true;
payload = 1;
} else {
fORb = false; //going backwards
payload = 2;
}
order++; //check speed
} else {
gORs = false; //stopped
payload = 0;
order = 0; //check if moving again
}
radio.write(&payload, sizeof(int));
} else { // sending speed
if (fORb == true){ //going forwards
speed = map(SandD, 0, 511, 0, 255); //convert to pwm
} else { //going backwards
speed = map(SandD, 0, -511, 0, 128); //half as fast
}
payload = speed + 3; //when recieving, if > 3 and =< 258, recieving speed
radio.write(&payload, sizeof(int));
order++; //check turn
}
} else { //order == 3, checking turn
turn = analogRead(turnPot); //get input from potentiometer
turn = map(turn, 0, 1023, 0, 180); //convert to 180 degrees for servo
payload = turn + 258; //when recieving if > 258, recieving turn
radio.write(&payload, sizeof(int));
}
}
Car: (I am using the serial monitor to test the receiving)
//RADIO CODE BASED ON THIS EXAMPLE IN LIBRARY: https://nrf24.github.io/RF24/examples_2GettingStarted_2GettingStarted_8ino-example.html
//RADIO
#include <RF24.h>
#include <RF24_config.h>
#include <nRF24L01.h>
#include <printf.h>
#define CE_PIN 7
#define CSN_PIN 8
RF24 radio(CE_PIN, CSN_PIN);
const uint8_t pipeIn = 0xE8E8F0F0E1LL;
bool radioNumber = 1;
bool role = false;
int commands; //commands go to here
//CAR
int turnDeg;
int speed;
void setup() {
//RADIO
if (!radio.begin()){
while (1) {}
}
radio.setPALevel(RF24_PA_LOW);
radio.openReadingPipe(1, pipeIn);
radio.startListening();
Serial.begin(9600);
}
void loop() {
uint8_t pipe;
uint8_t bytes;
//Get command
if (radio.available(&pipe)){
bytes = radio.getPayloadSize();
radio.read(&commands, bytes);
}
if (commands <= 3){ //if recieving go,stop,forwards,&backwards
if (commands == 0) { //stopping
Serial.println("stopped");
} else if (commands == 1) { //forwards
Serial.println("forward");
} else { //backwards
Serial.println("backwards");
}
} else if (commands > 3 && commands <= 258){ //if recieving speed
speed = commands - 3;
Serial.println("Speed: " + speed);
} else { // recieving turn
turnDeg = commands - 258;
Serial.println("Turn: " + turnDeg);
}
}
The problem is that when I run the code I get this in the serial monitor:
Thanks!

