Hello, I am currently working on a project that requires me to communicate two analog signals from a joystick (X and Y) that control either an electronic speed controller or a servo. I have done this successfully over a wired connection on a single board, with the code shown below:
// Hardwired Hovercraft Program Version
#include <Servo.h>
Servo ESC; // creates servo object to control the ESC
Servo servo; // creates servo object to control servo
int JoysX; // value from the analog pin 0
int JoysY; // value from the analog pin 1
void setup() {
// Attach the ESC on pin 9
ESC.attach(6,1000,2000); // (pin, min pulse width, max pulse width in microseconds to arm ESC)
servo.attach(5);
}
void loop() {
JoysX = analogRead(A0); // reads the value of the potentiometer (value between 0 and 1023)
JoysX = map(JoysX, 0, 1023, 0, 180); // scales it to use it with the servo library (value between 0 and 180)
ESC.write(JoysX); // Sends the signal to the ESC
JoysY = analogRead(A1); // reads the value of the potentiometer (value between 0 and 1023)
JoysY = map(JoysY, 0, 1023, 0, 180); // scales it to use it with the servo library (value between 0 and 180)
servo.write(JoysY); // Sends the signal to the Servo
}
Although this works, what I am attempting to complete is to have two Arduino boards, one a transmitter, and one a receiver, over two nrf24l01 modules, where the transmitter can communicate two analog signals at once to the receiver. I have attempted to program this myself, although it is proving quite difficult. The code for both the transmitter and receiver is shown below:
TX CODE:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
radio.begin();
radio.openWritingPipe(address); // 00001
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
radio.stopListening();
int JoysValueX = analogRead(A0);
int angleValueX = map(JoysValueX, 0, 1023, 0, 180);
radio.write(&angleValueX, sizeof(angleValueX));
int JoysValueY = analogRead(A1);
int angleValueY = map(JoysValueY, 0, 1023, 0, 180);
radio.write(&angleValueY, sizeof(angleValueY));
}
RX CODE:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
Servo SteeringSystem;
Servo ESC;
void setup() {
SteeringSystem.attach(5);
ESC.attach(6);
radio.begin();
radio.openReadingPipe(1, address); // 00001
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
delay(5);
radio.startListening();
if ( radio.available()) {
while (radio.available()) {
int angleVX = 0;
int angleVY = 0;
radio.read(&angleVX, sizeof(angleVX));
SteeringSystem.write(angleVX);
radio.read(&angleVY, sizeof(angleVY));
ESC.write(angleVY);
}
}
}
I don't know if communicating two analog signals at once through an nrf24l01 module is possible or plausible, so if anyone can help me out, it would be much appreciated.