#include <Servo.h>
Servo myservo;
int pos = 85;
int pos2 = 85;
int pos3 = 85;
const int maxDeg = 180;
const int minDeg = 0;
const int movement = 90;
const int leftPin = 2;
const int rightPin =3;
const int outputPin = 8;
int leftPressed=0;
int rightPressed = 0;
void setup()
{
myservo.attach(9);
pinMode(leftPin,INPUT);
pinMode(rightPin,INPUT);
}
void loop()
{
leftPressed = digitalRead(2);
rightPressed = digitalRead(3);
}
if(leftPressed)
{
if(pos < maxDeg);
pos += movement;
myservo.write(30);
}
if(rightPressed){
if(pos > minDeg);
pos -= movement;
myservo.write(150);
}
Where does your loop() function end?
Honestly I've barely been exposed to c++ and arduino. I'm doing a project for class. I'm not sure, I would think it is supposed to end before the if statements???
I can see some errors in this now but I doubt it would fix the initial error I'm getting. I really just want to use a couple of push buttons to control the servo motor.
I'm not sure, I would think it is supposed to end before the if statements???
All code must be in a function.
The code in a function is enclosed by { and }
Does your code follow these rules ?
If is a statement not a function so that would make those extra brackets following the loop function invalid?
If is a statement not a function
True, but are you using that program statement (and many others) in a function ?
Are you following the rules outlined in reply #4 ?
Answer : No
system
April 26, 2017, 10:15am
8
I'm not sure, I would think it is supposed to end before the if statements???
Your loop() function DOES end before the if statements. And, THAT is the problem. It is not supposed to.
void loop()
{
leftPressed = digitalRead(2);
rightPressed = digitalRead(3);
if(leftPressed)
if(pos < maxDeg);
pos += movement;
myservo.write(30);
if(rightPressed)
if(pos < minDeg);
pos -= movement;
myservo.write(150);
}
if(pos < maxDeg);No. Think again. Compare it to other if statements in your code.
system
April 26, 2017, 5:59pm
11
If statements should always have curly braces after them.
If statements rarely end with semicolons.
Nice job on finding the code tag icon. Not!
My competence level is decreasing, I'm trying to understand this.
system
April 26, 2017, 6:17pm
14
void loop()
{
leftPressed = digitalRead(2);
rightPressed = digitalRead(3);
if(leftPressed)
{
if(pos < maxDeg)
{
pos += movement;
myservo.write(30);
}
}
if(rightPressed)
{
if(pos < minDeg)
{
pos -= movement;
myservo.write(150);
}
}
}
At least in terms of using curly braces after the if statements, and not using ; at the end of if statements, this is what your code should look like.
Now, it does not make sense to me to be diddling with pos in both statements, and then writing constant values to the servo. I'd expect that you would write whatever is in pos to the servo.
The const values are un needed. Idk the I borrowed some of the code, As i said Iv e barely been wxposed to programming recently.
Arduino: 1.8.2
ArduinoButtons:42: error: expected ';' before 'myservo'
myservo.write(180)
^
ArduinoButtons:54: error: expected ';' before 'myservo'
myservo.write(0)
^
exit status 1
expected ';' before 'myservo'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I forgot semicolon's after write
system
April 26, 2017, 6:38pm
18
This report answer would have more information with
your code, in code tags this time.
Thank you guys for your help, I appreciate it. I have a functioning bug free code for the moment.