I’ve been working on a robotics project and have tested the platform without using the wireless system and it works great but for some reason when the RF modules are hooked up and the transmitting Arduino is powered on the code breaks. What is weird is that resetting the transmitter causes it to work without any issues. As a side note both the transmitter and receiver use UNO boards and the RF modules are nRF24L01
Here is the code (both the transmitter and receiver in one)
#include <SPI.h>
#include "RF24.h"
#define MODE_TRANSMITTER 1
#define MODE_RECEIVER 0
// Edit here to change mode
#define MODE MODE_TRANSMITTER
#define DEBUG_ENABLE 1
RF24 radio(7, 8);
byte addresses[][6] = {"1Node", "2Node"};
void setup() {
#if DEBUG_ENABLE
Serial.begin(115200);
#endif
radio.begin();
radio.setPALevel(RF24_PA_HIGH);
// Open Pipes according to radio mode
#if MODE
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
#else
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
#endif
radio.startListening();
}
void loop() {
#if MODE
// Transmitter Role
radio.stopListening();
// Joystick Mixing
int throttle, direction = 0;
int leftMotor, rightMotor = 0;
throttle = constrain((512 - analogRead(A0)) / 2, -255, 255);
direction = constrain(-(512 - analogRead(A1)) / 2, -255, 255);
leftMotor = constrain(throttle + direction, -255, 255);
rightMotor = constrain(throttle - direction, -255, 255);
#if DEBUG_ENABLE
// Debugging Info
Serial.print("Throttle: ");
Serial.println(throttle, DEC);
Serial.print("Direction: ");
Serial.println(direction, DEC);
Serial.print("LeftVal: ");
Serial.println(leftMotor, DEC);
Serial.print("RightVal: ");
Serial.println(rightMotor, DEC);
#endif
// Splitting into channels
uint8_t LF, LR, RF, RR = 0;
if (leftMotor >= 0 && rightMotor >= 0) {
LF = leftMotor;
RF = rightMotor;
LR, RR = 0;
}
else if (leftMotor >= 0 && rightMotor < 0) {
LF = leftMotor;
RR = abs(rightMotor);
LR, RF = 0;
}
else if (leftMotor < 0 && rightMotor >= 0) {
LR = abs(leftMotor);
RF = rightMotor;
RR, LF = 0;
}
else {
LR = abs(leftMotor);
RR = abs(rightMotor);
LF, RF = 0;
}
// Radio Transmission
uint8_t data[] = {LF, LR, RF, RR};
#if DEBUG_ENABLE
if (!radio.write(&data, sizeof(uint32_t))) {
Serial.println("Error Sending Data");
}
else {
Serial.print("Send OK, Data: ");
Serial.println(data[0], DEC);
Serial.println(data[1], DEC);
Serial.println(data[2], DEC);
Serial.println(data[3], DEC);
}
#else
radio.write(&data, sizeof(uint32_t));
#endif
delay(15);
#else
// Receiver Role
// Radio Receive
uint32_t data;
if (radio.available()) {
while (radio.available()) {
radio.read( &data, sizeof(uint32_t));
}
// Debug
Serial.print("Data Received: ");
Serial.println(data, DEC);
// Write to channels
uint8_t *chunk = (uint8_t*)&data;
analogWrite(5, chunk[0]);
analogWrite(6, chunk[1]);
analogWrite(9, chunk[2]);
analogWrite(10, chunk[3]);
#if DEBUG_ENABLE
// Debug
Serial.println("Chunks:");
Serial.println(chunk[0], HEX);
Serial.println(chunk[1], HEX);
Serial.println(chunk[2], HEX);
Serial.println(chunk[3], HEX);
#endif
}
#endif
}
I suspect the issue is at the if block in the transmitter section. Because of the way it is set up only one R/L of LF,LR,RF,RR should be able to be greater than zero at a time, yet in practice three of them end up greater than zero depending on the position of the joystick.
Example of debug output:
Throttle: -147
Direction: 40
LeftVal: -107
RightVal: -187
Send OK, Data: 17
107
0
187
The data is 17, 107, 0, 187 which shouldn’t be possible but it does this every time the transmitter is powered up and then works fine if it is reset. This obviously isn’t being caused by a sync error between the transceivers because the debug info is taken from the transmit side.
Id like to know what is causing this problem since in the final design pressing the reset button wont be an option. Also, if anyone has suggestions on making the if block more elegant or replacing it entirely would be greatly appreciated.