Hey there,
I'm rather new to working with an Arduino but have to start useing a Servo for a school project now.
What i'm trying to achieve is that when a push button is pressed the servo will follow the Sweep library script, and when the button is pressed again is stops, until it is being pressed again (and follows the script again).
what it does with my current program ; script uploaded - nothing happens to the servo - button pressed - servo sweeps 2x and stops, i can press the button again and the servo will do another set of 2 sweeps.
Not bad, but not exactly what im looking for pref. i'd like an on // off kinda thing going on
#include <Servo.h>
Servo myservo;
const int servoPin = 9; // the pin that the servo is attached to
const int buttonPin = 11; // the pin that the pushbutton is attached to
int servoState = 0; // current state of the servo
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int pos = 0; // variable to store the servo position
void setup() {
pinMode(buttonPin, INPUT); // initialize the button pin as an input
pinMode(servoPin, OUTPUT); // initialize the servo pin as an output
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// read the pushbutton input pin
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
// change the state of the servo when someone pressed the button
if (buttonState == 1) {
if(servoState==1) servoState=0;
else servoState=1;
}
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
lastButtonState = buttonState;
}
// turns servo on if the servoState=1 or off if the servoState=0
digitalWrite(servoPin, servoState);
//delay(20);
}
}
any pointers would be greatly appreciated!
-R