Lara Grant

Button Press to Move 2 Servos Mirrored

INPUT: 1 Momentary Button
OUTPUT: Two Servos – SG90 T Pro Micro Servos

Description: Press button, rotate servo 1 right and servo 2 left hold 1 sec, reverse. Press once for one right/left action. Keep pressing to continue action loop.

[code lang=”arduino” light=”true”] /*
* push button once – servo 1 rotates forward, servo 2 rotated back, hold 1 sec, reverse
*
* halloween workshop 2012
* lara grant
*/

#include <Servo.h>
#include "ClickButton.h"

// Set digital pin numbers:
const int servoPin = 9; // The number of the right Servo pin facing
const int servoPin2 = 8; // The number of the left Servo pin facing
const int buttonPin = 2; // The number of the Pushbutton pin

int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0; // Variable for reading direction of the servo

Servo myservo; // Create servo object to control a servo
Servo myservo2;

int pos = 0; // Variable to store the servo position

void setup() {
Serial.begin(9600);
myservo.attach(8); // attaches the servo on pin 8 to the servo object
myservo2.attach(9); // attaches the servo on pin 7 to the servo object
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);

//The button is pushed
if (buttonState == LOW) {

forward();
delay(1000);
reverse();
delay(1000 );
}
}

void forward() {
myservo.write(0);
myservo2.write(180);
}

void reverse() {
myservo.write(180);
myservo2.write(0);
}

[/code]

Have your say