Servo Control with Pushbutton

Hy Guys

I have a question:
I will control a Servo with a Pushbutton. If I push the Button, hte Servo Programm should stop or Start, so the Pushbutton should be an ON/OFF Switch

Now my Problem with my code is ,(you see it there) that I can only Stop the Programm when its finished
Could anyonehelp?

#include <Servo.h>
Servo myServo; // Create Servo object to control the servo
int pinButton = 2;
int LED = 13;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;

void setup() {
pinMode(pinButton, INPUT);
pinMode(LED, OUTPUT);
myServo.attach(9); // Servo is connected to digital pin 9
}

void loop() {
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateLED == HIGH){
stateLED = LOW;

myServo.writeMicroseconds(1515);
delay(5000);
myServo.writeMicroseconds(1485);
delay(5000);

} else {
stateLED = HIGH;
myServo.write(1500);
delay(2000);
}
time = millis();
}
digitalWrite(LED, stateLED);
previous == stateButton;
}

Now my Problem with my code is ,(you see it there) that I can only Stop the Programm when its finished

Some of it, anyway, improperly posted.

Could anyonehelp?

Not until you get rid of EVERY delay().

You must implement a state machine. On any given pass through loop(), it might be time to do something (see the blink without delay example). If it is, you set a time until the next thing happens, do whatever needs doing (based on current state) and change to the next state.

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

I see you have posted code all over the forum, but still not posting it as suggested, please read above, then we can help you.

Thanks… Tom… :slight_smile: