Hello all! I am very new to coding and am trying an early project. I am trying to make an RC car controlled by a FlySky controller. I have been having a problem with what I believe is the order of operations. I can have the motors create a forward, reverse, and clockwise rotation, but it does not respond to the counterclockwise rotation.
I think it is the order of the "if" and "else if" statements, but I have not been able to solve the problem. Some code from the DroneBot workshop has been used for the initial information gathered from the receiver.
Any help is greatly appreciated and constructive criticism is very welcome!! My goal is to start learning with smaller projects and scale up with input from other sensors.
// Define Input Connections
#define CH1 A3
#define CH2 A1
#define CH3 A2
#define CH4 A0
#define CH5 10
#define CH6 11
#define motorSpeedPinLeft 11 //analogs
#define motorSpeedPinRight 10
#define motorDirectionLeft1A 13 //digitals
#define motorDirectionLeft1B 12
#define motorDirectionRight1A 8
#define motorDirectionRight1B 7
// Integers to represent values from sticks and pots
int ch1Value;
int ch2Value;
int ch3Value;
int ch4Value;
int ch5Value;
int dt=50;
float motorSpeedLeft;
float motorSpeedRight;
// Boolean to represent switch value
bool ch6Value;
// Read the number of a specified channel and convert to the range provided.
// If the channel is off, return the default value
int readChannel(int channelInput, int minLimit, int maxLimit, int defaultValue){
int ch = pulseIn(channelInput, HIGH, 30000);
if (ch < 100) return defaultValue;
return map(ch, 1000, 2000, minLimit, maxLimit);
}
// Read the switch channel and return a boolean value
bool readSwitch(byte channelInput, bool defaultValue){
int intDefaultValue = (defaultValue)? 100: 0;
int ch = readChannel(channelInput, 0, 100, intDefaultValue);
return (ch > 50);
}
void setup(){
// Set up serial monitor
Serial.begin(115200);
//Channel inputs
pinMode(CH1, INPUT);
pinMode(CH2, INPUT);
pinMode(CH3, INPUT);
pinMode(CH4, INPUT);
pinMode(CH5, INPUT);
pinMode(CH6, INPUT);
//Motor outputs
pinMode(motorSpeedPinLeft, OUTPUT);
pinMode(motorSpeedPinRight, OUTPUT);
pinMode(motorDirectionLeft1A, OUTPUT);
pinMode(motorDirectionLeft1B, OUTPUT);
pinMode(motorDirectionRight1A, OUTPUT);
pinMode(motorDirectionRight1B, OUTPUT);
}
void loop() {
// Get values for each channel
ch1Value = readChannel(CH1, -100, 100, 0);
ch2Value = readChannel(CH2, -100, 100, 0);
ch3Value = readChannel(CH3, -100, 100, -100);
ch4Value = readChannel(CH4, -100, 100, 0);
ch5Value = readChannel(CH5, -100, 100, 0);
ch6Value = readSwitch(CH6, false);
// Print to Serial Monitor
Serial.print("Ch1: ");
Serial.print(ch1Value);
Serial.print(" | Ch2: ");
Serial.print(ch2Value);
Serial.print(" | Ch3: ");
Serial.print(ch3Value);
Serial.print(" | Ch4: ");
Serial.print(ch4Value);
Serial.print(" | Ch5: ");
Serial.print(ch5Value);
Serial.print(" | Ch6: ");
Serial.println(ch6Value);
//Turning/////////////////////////////////////////////////////////////////////////////
if (ch1Value>0) { //clockwise
motorSpeedLeft= 2.55*abs(ch1Value);
motorSpeedRight= 2.55*abs(ch1Value);
digitalWrite(motorDirectionRight1A, LOW);
digitalWrite(motorDirectionRight1B, HIGH);
digitalWrite(motorDirectionLeft1A, HIGH);
digitalWrite(motorDirectionLeft1B, LOW);
} else if (ch1Value<0) { //counterclockwise
motorSpeedLeft= 2.55*abs(ch1Value);
motorSpeedRight= 2.55*abs(ch1Value);
digitalWrite(motorDirectionLeft1A, LOW);
digitalWrite(motorDirectionLeft1B, HIGH);
digitalWrite(motorDirectionRight1A, HIGH);
digitalWrite(motorDirectionRight1B, LOW);
if (ch2Value>=0) { //forward
motorSpeedLeft= 2.55*abs(ch2Value);
motorSpeedRight= 2.55*abs(ch2Value);
digitalWrite(motorDirectionRight1A, HIGH);
digitalWrite(motorDirectionRight1B, LOW);
digitalWrite(motorDirectionLeft1A, HIGH);
digitalWrite(motorDirectionLeft1B, LOW);
} else if (ch2Value<0) { //reverse
motorSpeedLeft= 2.55*abs(ch2Value);
motorSpeedRight= 2.55*abs(ch2Value);
digitalWrite(motorDirectionLeft1A, LOW);
digitalWrite(motorDirectionLeft1B, HIGH);
digitalWrite(motorDirectionRight1A, LOW);
digitalWrite(motorDirectionRight1B, HIGH);
} }
motorSpeedLeft=constrain(motorSpeedLeft, 0, 255);
motorSpeedRight=constrain(motorSpeedRight, 0, 255);
analogWrite(motorSpeedPinLeft, motorSpeedLeft);
analogWrite(motorSpeedPinRight, motorSpeedRight);
delay(dt);
}