So I am writing code to control a servo with an RC radio. I am using an Arduino Mega. I get this error whenever I compile. I am not that experienced with Arduino so I am probably not doing something right. Thanks for the help!
Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
C:\Users\ethan\Desktop\Periferals_control\Periferals_control.ino: In function 'void loop()':
Periferals_control:46: error: expected ';' before '{' token
{
^
exit status 1 expected ';' before '{' token
This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.
#include <Servo.h>
Servo servo1;
Servo servo2;
//the following are all ~PWM capable ports
int servoElevator_PwmIn = 6; //Elevator Servo
int servoGrabber_PwmIn = 7; //Grabber Servo
//Channels on AT9
int leftStickVertical = 2; //Channel 3
int rightStickVertical = 3; //Channel 7
int elevatorServoStop = 50;
void setup() {
//pinMode(servoGrabber_PwmIn, OUTPUT);
//pinMode(servoElevator_PwmIn, OUTPUT);
pinMode(leftStickVertical, INPUT);
pinMode(rightStickVertical, INPUT);
Serial.begin(9600);
servo1.attach(6);
servo2.attach(servoGrabber_PwmIn);
}
void loop() {
int leftStickVerticalPwm = pulseIn(leftStickVertical, HIGH, 25000);
int rightStickVerticalPwm = pulseIn(rightStickVertical, HIGH, 25000);
Serial.print(leftStickVerticalPwm);
Serial.print("\t");
Serial.print(rightStickVerticalPwm);
Serial.print("\n");
if (rightStickVerticalPwm == 0)
{
servo1.write(elevatorServoStop);
}
else if (rightStickVerticalPwm > 1500)
{
servo1.write(100);
}
else (rightStickVerticalPwm < 1400)
{
servo1.write(30);
}
}
2 Answers 2
if (rightStickVerticalPwm == 0)
{
servo1.write(elevatorServoStop);
}
else if (rightStickVerticalPwm > 1500)
{
servo1.write(100);
}
else (rightStickVerticalPwm < 1400)
{
servo1.write(30);
}
You cannot have another comparison in an else
statement. You might have meant else if
in the last clause.
Clarification if you're not well familiar with C/C++: If you have an if
statement, it can be followed by arbitrary many else if
clauses and at maximum one else
clause which will execute when all other conditions fail. For example:
int a = 10;
if( a == 9 ) {
//Executed if a = 9
} else if( a < 20 ) {
//If above not executed, this will be executed if a < 20
} else {
//This is executed if none of the conditions above hold.
//A pure "else" does not have a condition and only one may
//exist in an `if` statement.
}
you should not provide conditions for " else " statement because it focuses on what action should happen if all " if and else if " conditions are not fulfiiled.
Explore related questions
See similar questions with these tags.