Hi all, thank you all so much for whatever help you can give me. I'm new to C++ and Arduino but have been learning as much as I can. I have a sketch that uses an if else statement to throw a servo and then once the servo is thrown to light an LED. Is there a way to do that. I can't complete the servo first then light the LED can I, because the only way I know how to do it is to use close and then open brackets, but then it won't recognize the else belonging to the if. I've scoured the web but can't figure out how to make this work. Here's my code - it reads a button and compares the servo's position and throws it the other way and then switches the color of an LED. It works fine if I get rid of the }{ between turn off and turn on LEDs, but I'd really like the LED to confirm the move is complete.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 90; // variable to store the servo position
int buttonApin = 8;
int LEDgreen = 2;
int LEDred = 3;
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonApin, INPUT_PULLUP);
pinMode(LEDgreen, OUTPUT);
pinMode(LEDred, OUTPUT);
}
void loop()
{
if (digitalRead(buttonApin) == LOW && pos < 110) // test to see if button pushed
for (pos != 110; pos <= 110; pos += 1) { // goes from 70 degrees to 110 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(LEDred, LOW); // turns off red LED
}
{
digitalWrite(LEDgreen, HIGH); // turns on green LED after move complete
}
else if (digitalRead(buttonApin) == LOW && pos >70)
for (pos != 70; pos >= 70; pos -= 1) { // goes from 110 degrees to 70 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(LEDgreen, LOW); // turns off green LED
}
{
digitalWrite(LEDred, HIGH); // turns on red LED after move complete
}
}
thanks for any help you can provide! FAS
Your parentheses are mis-matched and are incorrect. Load the code into a proper editor with formatting and see for yourself.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 90; // variable to store the servo position
int buttonApin = 8;
int LEDgreen = 2;
int LEDred = 3;
void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonApin, INPUT_PULLUP);
pinMode(LEDgreen, OUTPUT);
pinMode(LEDred, OUTPUT);
}
void loop()
{
if (digitalRead(buttonApin) == LOW && pos < 110) // test to see if button pushed
for (pos != 110; pos <= 110; pos += 1)
{ // goes from 70 degrees to 110 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(LEDred, LOW); // turns off red LED
}
{
digitalWrite(LEDgreen, HIGH); // turns on green LED after move complete
}
else if (digitalRead(buttonApin) == LOW && pos >70)
for (pos != 70; pos >= 70; pos -= 1)
{ // goes from 110 degrees to 70 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(LEDgreen, LOW); // turns off green LED
}
{
digitalWrite(LEDred, HIGH); // turns on red LED after move complete
}
}
Hmm, I did use a proper editor, I thought. I'm using the Arduino IDE on a Mac. As for mismatched, I traced it w/ the editor and they all have open and closed. It works fine if I delete the close and open before the turn the LED on. Maybe I'm asking the wrong question. I understand why it doesn't work this way - I'm wondering if there's a way to make it do two sequential actions within one if.
Thanks for looking at it for me! FAS
FrankAlan:
I'm using the Arduino IDE
Not.
Look more closely. You have encapsulated digitalWrite statements in bot your if and else clauses while there is no logical demarcation for the clause itself. To be honest, I'm not really sure how the compiler deals with it. If there are no errors/warnings, I'd be afraid of the assumption being made. Your test should look like this,
if (test_conditions)
{
// do everything after the starting brace and before the closing
// brace that you want to accomplish if test_condition is true
}
else //or else if
{
// do the 'otherwise' stuff here
}
See the difference?
OK, I'm staring at the IDE. Not sure how you say I'm not using it. Sorry, I probably should have said in the beginning the code wouldn't compile. There are warnings -
Arduino: 1.8.7 (Mac OS X), Board: "Arduino/Genuino Uno"
/Users/fschneider/Documents/Arduino/One_button_checked/One_button_checked.ino: In function 'void loop()':
One_button_checked:36:3: error: 'else' without a previous 'if'
else if (digitalRead(buttonApin) == LOW && pos >70)
^
exit status 1
'else' without a previous 'if'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
The else can't refer back to the if bc I have an additional {} statement. I get that. What I would like to know is if there is a way to do the following -
if (test_conditions)
{
//do one thing and then when it is complete do another - i.e. move a servo and then light an LED
}
else (test_conditions)
{
//do one thing and then when it is complete do another
}
If the servo and LED happen together it works just great. Thanks!
You do it exactly as your pseudo-code is written. It looks like the real thing wrong with your original code was that you didn't have any braces {} around the whole of the code following the if ().
An if() statement that isn't immediately followed by a { will only conditionally execute a single following statement. See the if...else reference
Steve
I'm staring at the IDE. Not sure how you say I'm not using it.
Whilst staring at the code did you try Auto formatting it ?
{
digitalWrite(LEDgreen, HIGH); // turns on green LED after move complete
}
What is this section of code dependent on ?
Thanks Steve, this is what comes of trying to do without totally understanding what I'm doing. I misread that section. It works great now. Thank you, and next time I'll re-read 3 times before asking!