Hello!
I've been looking for some help with my problem on older topics but I cant find any, so here it goes.
I'm also not a english native speaker so sorry in advance for any errors.
I'm working on a RC car powered with a gas engine completely homemade, and the car itself is almost done, so I started on the actual remote control itself. The car has 2 servos (throttle and steering) and I want to control them using 2 arduinos (transmiter and reciever).
I already have a code for each arduino but I cant get the servos to work properly. If I make the code for only 1 servo, this same servo will work perfect but as soon as I try to use 2 at the same time they just kinda shake and dont turn at full speed, so I gess my code as something wrong.
Transmiter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
void setup() {
radio.begin();
radio.openWritingPipe(addresses[1]); // 00001
radio.openReadingPipe(1, addresses[0]); // 00002
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
int potValue = analogRead(A0);
int pot2Value = analogRead(A1);
int angleValue = map(potValue, 0, 1023, 0, 180);
int angle2Value = map(pot2Value, 0, 1023, 0, 180);
radio.stopListening();
radio.write(&angleValue, sizeof(angleValue));
radio.write(&angle2Value, sizeof(angle2Value));
delay(5);
radio.startListening();
}
Reciever code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
Servo myServo;
Servo myServo2;
void setup() {
myServo.attach(5);
myServo2.attach(6);
radio.begin();
radio.openWritingPipe(addresses[0]); // 00002
radio.openReadingPipe(1, addresses[1]); // 00001
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
delay(5);
radio.startListening();
if ( radio.available()) {
while (radio.available()) {
int angleValue = 0;
int angle2Value = 0;
radio.read(&angleValue, sizeof(angleValue));
radio.read(&angle2Value, sizeof(angle2Value));
myServo.write(angleValue);
myServo2.write(angle2Value);
}
}}
I'm not very good at programming, this is just altered version of a code I found online.
I'm using 10k potenciometers for now just to make the code work, I'll probably use a joystick pot later on.