Hello!
Could you please help me with my issue?
I am using a PWM signal from a Radio controller and receiver that go into the arduino. The arduino then reads the code and tells the servo what to do. The servo jitters and is not smooth at all. I am thinking that it has something to do with the PWM waves not being perfectly consistent, but I don't honestly know.
Here is the Code, ignore some parts that are not related to the issue.
Also, general advice might be useful. I am trying to make a flying wing controlled by an arduino to hopefully eventually program it to be VTOL capable.
#include <Servo.h>
Servo ElavonLeft;
Servo ElavonRight;
Servo RightMotor;
Servo LeftMotor;
//ServoPositions
int ElavonLeftPos = 0;
int ElavonRightPos = 0;
//Interstage
int ElevatorInter = 0;
int AileronInter = 0;
int ThrottleInter = 0;
int RudderInter = 0;
//MotorPower
int LeftMotorPower = 0;
int RightMotorPower = 0;
//ServoPins
const int ElavonLeftPin = 12;
const int ElavonRightPin = 13;
const int LeftMotorPin = 2;
const int RightMotorPin = 7;
//PWM input from Rx
const int ElevatorPin = 6;
const int AileronPin = 9;
const int RudderPin = 5;
const int ThrottlePin = 11;
const int ModeSwitchPin = 3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ElavonLeft.attach(ElavonLeftPin);
ElavonRight.attach(ElavonRightPin);
LeftMotor.attach(LeftMotorPin);
RightMotor.attach(RightMotorPin);
}
void loop() {
// Read the Positions
int AuxStickPosition = pulseIn(ModeSwitchPin, HIGH, 25000);
if (AuxStickPosition < 1300) {
int ElevatorStickPosition = pulseIn(ElevatorPin, HIGH, 25000);
int RudderStickPosition = pulseIn(RudderPin, HIGH, 25000);
int AileronStickPosition = pulseIn(AileronPin, HIGH, 25000);
int ThrottleStickPosition = pulseIn(ThrottlePin, HIGH, 25000);
int AuxStickPosition = pulseIn(ModeSwitchPin, HIGH, 25000);
//Read and convert
RudderInter = map(RudderStickPosition, 1000, 2000, -100, 100);
AileronInter = map(AileronStickPosition, 1000, 2000, -100, 100);
ThrottleInter = map(ThrottleStickPosition, 1000, 2000, -100, 100);
ElevatorInter = map(ElevatorStickPosition, 1000, 2000, -100, 100);
//wut
ElavonLeftPos = map(AileronInter - ElevatorInter, -200, 200, 45, 135);
ElavonRightPos = map(AileronInter + ElevatorInter, -200, 200, 45, 135);
RightMotorPower = map(ThrottleInter + RudderInter, -200, 200, 0, 180);
LeftMotorPower = map(ThrottleInter - RudderInter, -200, 200, 0, 180);
//Constrain
ElavonLeftPos = constrain(ElavonLeftPos, 0, 180);
ElavonRightPos = constrain(ElavonLeftPos, 0, 180);
LeftMotorPower = constrain(LeftMotorPower, 0, 180);
RightMotorPower = constrain(RightMotorPower, 0, 180);
ElavonLeft.write(ElavonLeftPos);
ElavonRight.write(ElavonRightPos);
LeftMotor.write(LeftMotorPower);
RightMotor.write(RightMotorPower);
Serial.println(ElevatorStickPosition);
Serial.println(AileronStickPosition);
delay(5);
}
else {
int AuxStickPosition = pulseIn(ModeSwitchPin, HIGH, 25000);
LeftMotor.write(0);
RightMotor.write(0);
delay(1);
}
}