button press to trigger servos. Cant stop it looping!

Hey guys,

trying to make 2 servos turn in opposite directions in response to a button press. When the button is held down the servos are meant to turn and when it is released they are meant to return to their original position.

I've managed to get the two servos moving, however they don't really listen to the button and just go around in a continuous loop. I want it so they do nothing until the button is depressed. I know this is pretty simple stuff but I'm under a deadline at work so need to figure this out best I can, any help or advice would be appreciated! Thank you!

Heres my code:

#include <Servo.h>
Servo myservo;
Servo myservo2;

int pos = 0;
int pos2 = 280;
int buttonState = 0;
int buttonPin = 2;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(3);
myservo2.attach(5);
pinMode (buttonPin,INPUT);
myservo.attach(5);
myservo.attach(3);
}

void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);

if (buttonState == LOW) {

myservo.write (pos);

myservo2.write(pos);

}

else (buttonState == HIGH);

{
myservo.write(pos2);
delay(3000);

myservo.write (pos);

myservo2.write (pos2);
delay (3000);

myservo2.write (pos);
}
}

else (buttonState == HIGH);Oops

Please remember to use code tags when posting code.

else (buttonState == HIGH);

Have a look at the else syntax in the else reference. Does the else have a conditional expression? Does the else have a semicolon at the end?

Also post how you wired your button. You seem to expect it to read HIGH when pressed which implies you've done things backwards. Do you have a pull-down resistor to keep it from floating when not pressed? Is there a particular reason you chose that instead of the built in pull-up?

hey guys cheers for your responses. This is how Ive wired it up, ive put a resistor before the switch. I thought high meant when a button was pressed down? I'll try switching my high and low maybe?

Delta_G:
Also post how you wired your button. You seem to expect it to read HIGH when pressed which implies you've done things backwards. Do you have a pull-down resistor to keep it from floating when not pressed? Is there a particular reason you chose that instead of the built in pull-up?

sorry mate, thanks for the help im just not quite sure how a pull-up works at the moment, ill try and do some research to get myself up to speed!

A pullup works by pulling the voltage up. But not forcing it up. A switch to ground will force the pin voltage
to zero, pullup or not.

A pin that's not connected has no defined voltage, its completely isolated and 'floating' (because MOSFET gates
are floating, isolated by a film of insulated SiO2).

Without a pullup your input voltage in undefined whenever the switch is released, which is no use.

Connect button like S3 in this schematic:
void setup()
{
pinMode(buttonPin,INPUT_PULLUP);

pranghead:
hey guys cheers for your responses. This is how Ive wired it up, ive put a resistor before the switch. I thought high meant when a button was pressed down? I'll try switching my high and low maybe?

That's a picture of a tangle of wires. Can you maybe draw a schematic and take a picture of that instead.

thanks for the help guys, my switch was floating as I had run the resistor through the signal wire and not made a separate line to ground. Thank you for all the comments, really helped me out!