I'm receiving a programming error message that I'm unable to resolve. I've tried to verify that all curly braces and parenthesis are in matching pairs and correctly placed. As far as I can tell, this is the case. However, I'm only about 5 hours into my first ever project using arduino so Its quite likely that I've got a simple mistake somewhere. A very similar code I have runs just fine, however I decided to rewrite the code and clean it up a bit, and now I receive the error messages seen below. Please help me locate this error, its a been a bit of a head scratcher, however i'm prepared to feel embarrassed at how I missed something quite simple
final_1.ino: In function 'void loop()':
final_1:102: error: expected `}' before 'else'
final_1.ino: At global scope:
final_1:112: error: expected unqualified-id before 'else'
final_1:116: error: expected declaration before '}' token
my code is below . I've marked the highlighted error line with an "ERROR" comment. By the way, the feedback referred to in my code is an input from one of ADAfruit's feedback servo's.
#include<Servo.h>
Servo myservo; //name servo
int feedback = A0; //connects feedback pin
int pos; //creates variable pos
const int buttonpin = 2; //creates button connection
int midpoint = 0; //creates variable midpoint
int openpos; //creates a variable to hold the feedback value for the open position
int closedpos; //creates a variable to hold the feedback value for the closed position
int buttonstate=0; //initializes a button state
int lastbuttonstate=0; //initializes last button state
int higher; //creates a variable to mark which feedback value is higher
int lower; //creates a variable to mark which feedback positoin is lower
int mover; //variable used in creating slow movement
int slower; // variable used in creating slow movement
int posi; // variable not used
int lockout; //variable holding the location in which the servo locks out the linckage
int mindegree; // variable with the minimum degree the servo can experience
int maxdegree; // variable with the maximum degree the servo can experience
void setup() { //setup portion of code (only run once at the begining of power up)
pinMode(feedback, INPUT); //tells arduino that the feedback wire is an input
myservo.attach(9); //tells arduino to drive servo through pin 9
pinMode(buttonpin, INPUT); //tells arduino to read pin 2 as an input
Serial.begin(9600); //initializes on screen monitoring
for (pos=75;pos<155;pos +=1) // This code block tells the servo to go to the closed location, read the feedback value, and convert to degrees
{
myservo.write(pos);
delay(20);
}
delay(200);
openpos=analogRead(feedback);
maxdegree=map(openpos,164,500,0,180) ;
for (pos=155; pos>=75; pos-=1) //this code block tells the servo to go to the open location, read the feedback value, and convert to degrees
{
myservo.write(pos);
delay(20);
}
delay(200);
closedpos=analogRead(feedback);
mindegree=map(closedpos,164,500,0,180) ;
Serial.println(openpos); //this code block outputs values for trouble shooting
Serial.println(maxdegree);
Serial.println(closedpos);
Serial.println(mindegree);
//myservo.detach();
if (closedpos <= openpos) //this code block figures out which position (open or closed) outputs a higher feedback/degree value
{
lower=closedpos ;
higher=openpos ;
}
else
{
lower=openpos ;
higher=closedpos ;
}
midpoint=((higher-lower)/2)+lower ; // calculates the mid range point between open and closed
lockout=.85*(maxdegree-mindegree)+mindegree ; //calculates the positoin of the servo that lock the linkage closed
myservo.write(lockout); // moves servo to the closed position
delay(200);
Serial.println(lockout); //outputs value of closed positoin
myservo.detach(); //disconnects servo power
}
void loop()
{
int pos;
buttonstate=digitalRead(buttonpin); // checks to see if button is open or closed
if ((buttonstate == LOW) && (lastbuttonstate == LOW)) // begins checking if button has been pressed
{
lastbuttonstate = buttonstate;
}
else if ((buttonstate == LOW) && (lastbuttonstate == HIGH))
{
lastbuttonstate = buttonstate;
}
else if ((buttonstate == HIGH) && (lastbuttonstate == LOW)) // detects button press
{
lastbuttonstate = buttonstate ; // updates button state
pos = analogRead(feedback); // reads feedback value
if (pos <= midpoint); // determines which direction flap needs to move
{
pos=map(pos,lower,higher,mindegree,maxdegree) ; // converts pos to a degree value for servo control
for (pos=0 ; pos >= lockout ; pos += 1) // for loop controls speed of flap movement in this direction
{
myservo.write(pos);
delay(20);
}
}
else // ERROR LINE - else loop controls speed of flap movement in other direction
{
pos=map(pos,lower,higher,mindegree,maxdegree) ; // converts pos to a degree value for servo control
for (pos=180 ; pos <= mindegree ; pos -= 1)
{
myservo.write(pos);
delay(20);
}
}
}
else{ //updates button state for any other scenario
lastbuttonstate = buttonstate;
}
}