Hey, I am trying to build a RC submarine. For this project I need 2 servos and 1 DC motor to be controlled wireless. I got stuck at trying to control 2 servos at the same time with 2 potentiometers wireless. I read the advice at another post to group the message in an array. I am now able to move both servos with one potentiometer or both servos with both potentiometers. What I need is potentiometer1 to correspond with servo1 and potentiometer2 to correspond with servo2. Could anyone help me figure out what I am doing wrong?
transmitter code
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem
int msg_Pin=analogRead(A0); // connects to analoge pin A0
int msgTwo_Pin=analogRead(A1); // connects to analoge pin A1
int msg[2]; // creates an array of 2, holding data of both potmeters
RF24 radio(5,10); //5 and 10 are a digital pin numbers to which signals CE and CSN are connected.
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino.
void setup(void){
radio.begin(); //it activates the modem.
radio.openWritingPipe(pipe); //sets the address of the receiver to which the program will send data.
}
void loop(void){
// writes data in the array &maps input over 180 degrees
msg[0] = map (analogRead(msg_Pin), 0, 1023, 0, 180);
msg[1] = map (analogRead(msgTwo_Pin), 0, 1023, 0, 180);
radio.write(&msg, sizeof(msg));
}
receiver code
#include <Servo.h> //the library which helps us to control the servo motor
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem
Servo myServo; //define the servo name
Servo myServoTwo; //define the servo name
RF24 radio(5, 10); /*This object represents a modem connected to the Arduino.
Arguments 5 and 10 are a digital pin numbers to which signals
CE and CSN are connected.*/
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.
int msg[2];
void setup() {
myServo.attach(3); //3 is a digital pin to which servo signal connected
myServoTwo.attach(6); //6 is a digital pin to which servo signal connected
radio.begin(); //it activates the modem.
radio.openReadingPipe(1, pipe); //determines the address of our modem which receive data.
radio.startListening(); //enable receiving data via modem
}
void loop(){
if(radio.available()){ //checks whether any data have arrived at the address of the modem
bool done = false; //returns a “true” value if we received some data, or “false” if no data.
while (!done) {
done = radio.read(&msg, sizeof(msg));
myServo.write(msg[0]);
myServoTwo.write(msg[1]);
}
}
}