Hello,
I'm new to this and I'm trying to do an RC airplane transmitter and receiver. The problem that I encounter is that both servos and the brushless motor jitter/chatter/vibrate.
In the transmitter, I use an Arduino UNO + HC12 + 1 joystick that controls the 2 servos + 1 potentiometer that controls the brushless motor + 1 switch to stop/start transmitting.
In the receiver, I use an Arduino Nano + HC12 + 2 Servos + 1 Brushless motor.
I was reading a lot about it and it could be caused by:
- Servo.h and SoftwareSerial.h library timers: I have changed the library Sevo.h for ServoTimer2.h
- Instability of the joystick, the values change too quick: I have included a code that the servo will only be moved if the new value change more than 20
- The voltage drops when the servos start: I have added a capacitor in the receiver
However, it is still vibrating and I have run out of ideas. Does anyone know how I can solve this problem?
This is the transmitter code and I also attach the sketch so you can see how I have connected everything:
#include <SoftwareSerial.h>
#define joyX1 A0
#define joyX2 A1
#define joyY1 A2
SoftwareSerial hc12(0,1); //TX,RX
const int switchpin=5;
const int greenled=8;
int buttonpushcounter=1;
int buttonstate=0;
int lastbuttonstate=0;
void setup() {
// put your setup code here, to run once:
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(switchpin,INPUT);
pinMode(greenled,OUTPUT);
digitalWrite(greenled,LOW);
Serial.begin(9600);
hc12.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate=digitalRead(switchpin);
if (buttonstate != lastbuttonstate){
if (buttonstate==HIGH){
buttonpushcounter++;
}
else{Serial.println("OFF");}
delay(100);
}
lastbuttonstate=buttonstate;
if (buttonpushcounter%2==0){
digitalWrite(greenled,HIGH);
int elevator=map(analogRead(A1),0,1023,800,2200);
int rudder=map(analogRead(A2),0,1023,800,2200);
int thrust=map(analogRead(A0),0,1023,800,2200);
hc12.print(elevator);
hc12.print(",");
hc12.print(rudder);
hc12.print(",");
hc12.print(thrust);
hc12.print(",");
hc12.println("");
Serial.print(elevator);
Serial.print(",");
Serial.print(rudder);
Serial.print(",");
Serial.print(thrust);
Serial.print(",");
Serial.println("");
delay(200);
}
else{digitalWrite(greenled,LOW);}
}
Even though in this code I am connecting the HC12 to the ports 0 and 1, I have also tried with 2,3 and 4,5.
This is the receiver code:
#include <SoftwareSerial.h>
#include <ServoTimer2.h>
ServoTimer2 servo1;
ServoTimer2 servo2;
ServoTimer2 motor;
//#include <Servo.h>
//
//Servo servo1;
//Servo servo2;
//Servo motor;
String input;
SoftwareSerial hc12(0,1); //TX,RX
int elevatorold=1504;
int rudderold=1493;
int thrustold=800;
int x;
int y;
int z;
const char coma=',';
void setup() {
// put your setup code here, to run once:
servo1.attach(9);
servo2.attach(10);
motor.attach(11);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
Serial.begin(9600);
hc12.begin(9600);
//motor.write(170);
//delay(2000);
//motor.write(90);
//delay(2000);
//motor.write(140);
//delay(2000);
//motor.write(90);
//delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
while (hc12.available()){
input=hc12.readStringUntil('\n');
if (input.length()>0){
Serial.println(input);
x=input.indexOf(coma);
int elevatornew=input.substring(0,x).toInt();
y=input.indexOf(coma,x+1);
int ruddernew=input.substring(x+1,y).toInt();
z=input.indexOf(coma,y+1);
int thrustnew=input.substring(y+1,z).toInt();
if (abs(elevatornew-elevatorold)>=20){
elevatorold=elevatornew;
servo1.write(elevatorold);
}
if (abs(ruddernew-rudderold)>=20){
rudderold=ruddernew;
servo2.write(rudderold);
}
if (abs(thrustnew-thrustold)>=20){
thrustold=thrustnew;
motor.write(thrustold);
}
delay(10);
}
}
}
If it is necessary, I can also attach a video.
Thanks in advance!