3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.


Arduino animatronics- make your awesome costumes more awesome!

Step 7Adding a servo

Adding a servo
Example 3- Adding a servo controlled by another button

Building on the previous example now we'll add a servo that is tied to a second push button. This uses the Arduino Servo library to control the servo position. By changing the value in parentheses after the servo1.write code you can control how far the servo moves.

Copy and paste this sketch into your Arduino window-

/*
 * Example 3
 * This example will blink two LEDs and then fade another LED when button1 is pressed and released
 * and a servo will move after button2 is pressed and released
 * Honus 2010
 * Modified from Adafruit alternating switch code, http://www.adafruit.com
*/
 

#include  "Servo.h"  // include the servo library

Servo servo1; // creates an instance of the servo object to control a servo

int servoPin1 = 9; // control pin for servo
int ledPin1 = 8;  // control pin for LED 
int ledPin2 = 7;
int ledPin3 = 11;

int buttonPin1 = 14;  // button is connected to pin 14 (analog 0 pin)
int buttonPin2 = 15;  // button is connected to pin 15 (analog 1 pin)
int val1;             // variable for reading the pin status
int val2;
int buttonState1;     // variable to hold the last button state
int buttonState2;              

void setup() {
 
  servo1.attach(servoPin1);   // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin1, INPUT);  // set the button pin as input
  pinMode(buttonPin2, INPUT);

 
  buttonState1 = digitalRead(buttonPin1);   // read the initial state
  buttonState2 = digitalRead(buttonPin2);   // read the initial state
 
   pinMode(ledPin1, OUTPUT);  // sets the LED pin as output
   pinMode(ledPin2, OUTPUT);
  
}


void loop(){
  servo1.write(20);
  val1 = digitalRead(buttonPin1);  // read input value and store it in val
 

  if (val1 != buttonState1) {  // the button state has changed!
    if (val1 == LOW) {  // check if the button is pressed
      Serial.println("button just pressed");
     
   digitalWrite(ledPin1, HIGH);   // sets the LED pin HIGH (turns it on)
   delay(500);                    // wait 500 milliseconds
   digitalWrite(ledPin2, HIGH);
   delay(500);
   digitalWrite(ledPin1, LOW);    // sets the LED pin LOW (turns it off)
   delay(500);
   digitalWrite(ledPin2, LOW);
   delay(500);
 
  
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin3, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect   
    delay(40);                           
  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin3, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect   
    delay(40);                           
  }
    } else { // the button is -not- pressed...
     
       digitalWrite(ledPin1, LOW);  // turn off the LED
       digitalWrite(ledPin2, LOW);
    }
  }
 
   val2 = digitalRead(buttonPin2);      // read input value and store it in val 2

  if (val2 != buttonState2) {          // the button state has changed!
    if (val2 == LOW) {                // check if the button is pressed
     
     
   servo1.write(160);  // rotate the servo to 160 degrees
   delay(3000);   // wait 3 seconds
   servo1.write(20);  // rotate to 20 degrees
 
    } else {                         // the button is -not- pressed...
   
      servo1.write(20);
    }
  }
 
  buttonState1 = val1;                 // save the new state in our variable
  buttonState2 = val2;                
}
« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
619
Followers
23
Author:Honus(Multi-Bot)
I'm a former bicycle industry designer turned professional jeweler. I like working with my hands and am happiest when I'm in the shop building my creations. If you need help with your project just let...
more »