I am using the RF24.h library to program one Arduino Uno to talk to another. One Uno is the remote controller and transmits only, while the other Uno just listens. I'm testing the radio connection before proceeding further with my remote controlled hovercraft project. My receiver is saying 'Connection Made' on the serial monitor, but the data being received is all zeros. I have looked at the data transmitted to make sure that I'm not sending zeros.
Pin assignments for both nRF24L01s to both Unos:
V+ -> 3.3V
GND -> GND
CSN -> pin 10
CE -> pin 9
MOSI -> pin 11
SCK -> pin 13
IRQ -> pin 2 (may use later if I put in interrupts)
MISO -> pin 12
Transmitter (remote controller) code
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <Wire.h>
#include <SPI.h>
// Define the digital inputs
//#define jB1 1 // Joystick button 1
//#define jB2 0 // Joystick button 2
#define Crane3left 7 // Toggle switch 1
#define Crane3right 8 // Toggle switch 1
#define Crane1left 3 // Button 1
#define Crane1right 4 // Button 2
#define Crane2left 5 // Button 3
#define Crane2right 6 // Button 4
float elapsedTime, currentTime, previousTime;
int c = 0;
RF24 radio(9, 10); // nRF24L01 (CE, CSN)
const byte address[6] = "00001"; // Address
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
// byte j1Button;
byte liftPot;
byte drivePot;
// byte j2Button;
// byte pot1;
// byte pot2;
// byte tSwitch1;
// byte tSwitch2;
byte C1L;
byte C1R;
byte C2L;
byte C2R;
byte C3L;
byte C3R;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
// Define the radio communication
radio.begin();
radio.openWritingPipe(address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
// Activate the Arduino internal pull-up resistors
// pinMode(jB1, INPUT_PULLUP);
// pinMode(t1, INPUT_PULLUP);
pinMode(Crane1left, INPUT_PULLUP);
pinMode(Crane1right, INPUT_PULLUP);
pinMode(Crane2left, INPUT_PULLUP);
pinMode(Crane2right, INPUT_PULLUP);
pinMode(Crane3left, INPUT_PULLUP);
pinMode(Crane3right, INPUT_PULLUP);
// Set initial default values
data.j1PotX = 127; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
data.j1PotY = 127;
data.liftPot = 127;
data.drivePot = 127;
// data.j1Button = 1;
// data.j2Button = 1;
// data.pot1 = 1;
// data.pot2 = 1;
// data.tSwitch1 = 1;
// data.tSwitch2 = 1;
data.C1L = 1;
data.C1R = 1;
data.C2L = 1;
data.C2R = 1;
data.C3L = 1;
data.C3R = 1;
}
void loop() {
// Read all analog inputs and map them to one Byte value
// data.j1PotX = 127; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
// data.j1PotY = 127;
// data.liftPot = 127;
// data.drivePot = 127;
data.j1PotX = map(analogRead(A1), 0, 1023, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255
data.j1PotY = map(analogRead(A0), 0, 1023, 0, 255);
data.liftPot = map(analogRead(A2), 0, 1023, 0, 255);
data.drivePot = map(analogRead(A3), 0, 1023, 0, 255);
//data.pot1 = map(analogRead(A7), 0, 1023, 0, 255);
//data.pot2 = map(analogRead(A6), 0, 1023, 0, 255);
// Read all digital inputs
// data.j1Button = digitalRead(jB1);
// data.j2Button = digitalRead(jB2);
// data.tSwitch2 = digitalRead(t2);
data.C1L = digitalRead(Crane1left);
data.C1R = digitalRead(Crane1right);
data.C2L = digitalRead(Crane2left);
data.C2R = digitalRead(Crane2right);
data.C3L = digitalRead(Crane3left);
data.C3R = digitalRead(Crane3right);
Serial.print("\n J1 X axis:");
Serial.print(data.j1PotX);
Serial.print("\n J1 Y axis: ");
Serial.print(data.j1PotY);
Serial.print("\n Lift Pot: ");
Serial.print(data.liftPot);
Serial.print("\n Drive Pot: ");
Serial.print(data.drivePot);
Serial.print("\n Crane 1 left: ");
Serial.println(data.C1L);
Serial.print("\n Crane 1 right: ");
Serial.println(data.C1R);
Serial.print("\n Crane 2 left: ");
Serial.println(data.C2L);
Serial.print("\n Crane 2 right: ");
Serial.println(data.C2R);
Serial.print("\n Crane 3 left: ");
Serial.println(data.C3L);
Serial.print("\n Crane 3 right: ");
Serial.println(data.C3R);
radio.write(&data, sizeof(Data_Package));
}
Receiver (vehicle) code
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <Wire.h>
#include <SPI.h>
/*
DIY Arduino based RC Transmitter Project
== Receiver Code ==
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
RF24 radio(9, 10); // nRF24L01 (CE, CSN)
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
// byte j1Button;
byte liftPot;
byte drivePot;
// byte j2Button;
// byte pot1;
// byte pot2;
// byte tSwitch1;
// byte tSwitch2;
byte C1L;
byte C1R;
byte C2L;
byte C2R;
byte C3L;
byte C3R;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening(); // Set the module as receiver
resetData();
}
void loop() {
// Check whether there is data to be received
if (radio.available()) {
// Serial.print("\n Connection made!!");
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
lastReceiveTime = millis(); // At this moment we have received the data
}
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone has a throttle up and we lose connection, it can keep flying unless we reset the values
}
// Print the data in the Serial Monitor
Serial.print("\n J1 X axis: ");
Serial.print(data.j1PotX);
Serial.print("\n J1 Y axis: ");
Serial.print(data.j1PotY);
Serial.print("\n Lift Pot: ");
Serial.print(data.liftPot);
Serial.print("\n Drive Pot: ");
Serial.print(data.drivePot);
Serial.print("\n Crane 1 left: ");
Serial.println(data.C1L);
Serial.print("\n Crane 1 right: ");
Serial.println(data.C1R);
Serial.print("\n Crane 2 left: ");
Serial.println(data.C2L);
Serial.print("\n Crane 2 right: ");
Serial.println(data.C2R);
Serial.print("\n Crane 3 left: ");
Serial.println(data.C3L);
Serial.print("\n Crane 3 right: ");
Serial.println(data.C3R);
}
void resetData() {
Serial.print("Connection not made!");
// Reset the values when there is no radio connection - Set initial default values
data.j1PotX = 69;
data.j1PotY = 69;
data.liftPot = 99;
data.drivePot = 99;
data.C1L = 1;
data.C1R = 1;
data.C2L = 1;
data.C2R = 1;
data.C3L = 1;
data.C3R = 1;
}
Serial Monitor output from transmitter (remote control):
J1 X axis:127
J1 Y axis: 125
Lift Pot: 254
Drive Pot: 102
Crane 1 left: 1Crane 1 right: 1
Crane 2 left: 1
Crane 2 right: 1
Crane 3 left: 1
Crane 3 right: 1
Serial Monitor output from receiver(vehicle):
J1 X axis: 0
J1 Y axis: 0
Lift Pot: 0
Drive Pot: 0
Crane 1 left: 0Crane 1 right: 0
Crane 2 left: 0
Crane 2 right: 0
Crane 3 left: 0
Crane 3 right: 0