I am looking of help with a coding issue. The following code works as required except for loop 3. As written the servo returns to the zero position rapidly. I would like it to return slowly.
The block comment of “for (pos = 90; pos >= 0; pos -= 3)” does not function when it replaces the “myservo.write(0); “ command. When using the “for (pos = 90; pos >= 0; pos -= 3)” code the servo moves slowly back in forth and does not stop. Any help would be appreciated.
#include <Servo.h>;
// pushbutton pin
const int buttonPin12 = 12; // servo switch & LED’s switch on/off
//LED levels
const int led2 = 2;
const int led3 = 3;
const int led5 = 5;
const int led6 = 6;
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach (7); // attaches the servo on pin 7 to the servo object
// Set up the pushbutton pins to be an input:
pinMode(buttonPin12, INPUT_PULLUP);
// Set up the LED’s for output
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led5, OUTPUT);
pinMode (led6, OUTPUT);
}
void loop()
{
loop1();
//loop2();
loop3();
}
void loop1(void)
{
int buttonState12;
buttonState12 = digitalRead(buttonPin12);
if (buttonState12 == LOW) // light the LEDs and start servo
{
for (pos = 0; pos <= 90; pos += 3) { // goes from 0 degrees to 90 degrees
// in steps of 3 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(200); // waits 0.2 seconds for the servo to reach the position
}
// light the LED
delay (2000);
digitalWrite(led2, LOW);
delay (2000);
digitalWrite(led3, LOW);
delay (2000);
digitalWrite(led5, LOW);
delay (2000);
digitalWrite(led6, LOW);
delay (2000);
}
else
{
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
}
}
void loop3(void)
{
int buttonState12;
buttonState12 = digitalRead(buttonPin12);
if (buttonState12 == HIGH) // light the LED
myservo.write(0); //0 degrees // position servo
delay(200);
/*
for (pos = 90; pos >= 0; pos -= 3) { // goes from 90 degrees to 0 degrees
// in steps of 3 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(200); // waits 0.2 seconds for the servo to reach the position
}
*/
}