For a school project I was designing a sketch to control 2 servo's with a button. when the button is pressed the servo with arms attached close and when the button is pressed again the arms open. The sketch says: 'servo' does not name a type. I dont have enought knowledge to see the problem, I tried to search for a sollution but did not find one. Does anyone know how to solve this?
This is the code with the problem:
// Set digital pin numbers:
const int servoPin = 7; // The number of the Servo pin
const int servoPin2 = 8; // the number of the Servo pin
const int buttonPin = A2; // The number of the Pushbutton pin
int ledg = 5; // The number of led pin green led
int ledr = 6; // The number of the led pin red led
int buttonState = 0; // Variable for reading the pushbutton status
int directionStatel = 0; // Variable for reading direction of the servo
int directionStater = 1; // Variable for reading direction of the servo
Servo leftarm; // Create servo object to control a leftservo
Servo rightarm; // Create servo object to control a rightservo
int pos = 0; // Variable to store the servo position
void setup() {
leftarm.attach(7); // attaches the servo on pin 7 to the servo object
rightarm.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
pinMode(ledg, OUTPUT); // initialize the led green pin as an output
pinMode(ledr, OUTPUT); // initialize the led red pin as an output
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (directionStatel == 0 & directionStater ==1){
//The button is pushed
if (buttonState == HIGH) {
digitalWrite (ledg, HIGH); // led start green light
digitalWrite (ledr, LOW); // led red is of
directionStatel = 1;// The direction for the servo is clockwise
directionStater = 0;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 180; pos=pos+1)
{
leftarm.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
// goes from 180 degrees to 0 degrees in steps of 1 degree
}
for(pos = 180; pos>=1; pos=pos-1)
{
rightarm.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
} else if (directionState == 1) {
// The button is pushed
if (buttonState == HIGH) {
digitalWrite (ledg, LOW); //led green is of
digitalWrite (ledr, HIGH); // led red is light
directionState = 0; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 180; pos>=1; pos=pos-1)
{
leftarm.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
// goes from 0 degrees to 180 degrees in steps of 1 degree
}
for(pos = 0; pos < 180; pos=pos+1)
{
rightarm.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
}
}