// @author : Brad Fettes // @date : Dec 04, 2010 // @thanks : Sweep by BARRAGAN // : Emacs arduino-mode #include int inputPin = 10; // Push button input pin int btnVal = 0; // Current value of pushbutton int ledPin = 5; // Pin for Green LED built into proto shield int outputPin = 9; // Servo pin int servo_pos = 0; // Current servo position int servo_pos_a = 20; // Position destination a int servo_pos_b = 65; // Position destination b int servo_state = 0; // 0 = position a, 1 = position b Servo mrservo; // create servo object to control the servo void setup() { pinMode(inputPin, INPUT); // Push Button pinMode(ledPin, OUTPUT); // LED mrservo.attach(outputPin); // attaches the servo on pin 9 to the servo object // Debug messages via serial monitor Serial.begin(9600); Serial.println("Starting..."); } void loop() { // Check to see if button is LOW or HIGH (5v) btnVal = digitalRead(inputPin); if(btnVal == LOW) { Serial.println("Button: LOW"); digitalWrite(ledPin, LOW); if(servo_state) { Serial.println("Position: a"); } else { Serial.println("Position: b"); } } if(btnVal == HIGH) { Serial.println("Button: HIGH"); digitalWrite(ledPin, HIGH); if(servo_state) { Serial.println("Position: a"); } else { Serial.println("Position: b"); } // Move from position a to position b if(servo_state == 0) { // I don't know how to concatenate Serial.print(servo_pos_a); Serial.print(" to "); Serial.println(servo_pos_b); for(servo_pos = servo_pos_a; servo_pos < servo_pos_b; servo_pos += 1) { mrservo.write(servo_pos); delay(15); // waits 15ms for the servo to reach the position } servo_state = 1; } else { // Move from position b to position a Serial.print(servo_pos_b); Serial.print(" to "); Serial.println(servo_pos_a); for(servo_pos = servo_pos_b; servo_pos >= servo_pos_a ; servo_pos -= 1) { mrservo.write(servo_pos); delay(15); } servo_state = 0; } } Serial.println(); Serial.println(); // Separate each cycle's output delay(250); }