Examples - ExampleMotor - Servo Motor

Servo Motor

This Example shows the use of a Servo Motor.






/**************************************************************************************************
*
*  Servo Motor
*
*   Version:      1.0.0 - September 2008
*   Author:       Etienne Ribeiro    / tutorial assistant caad      /  eribeiro[at]ethz.ch
*   Supervisor:   Christoph Wartmann / chair for caad - ETH Zürich  /  wartmann[at].arch.ethz.ch
*
*   Desc:         Shows the use of a Servo Motor.
*
***************************************************************************************************/


#include <Servo.h>





// Const

static int pinServo = 0;
static int digitalButtonPin[] = {6, 7};
static boolean enableSerial = true;


// (Var)
Servo myservo;
int pos = 0;




// Setup

void setup()
{

        // LED (2 * blink)
        pinMode(48, OUTPUT);
        digitalWrite(48, HIGH);
        delay(200);
        digitalWrite(48, LOW);
        delay(200);
        digitalWrite(48, HIGH);


        // Serial (Print to serial if enableSerial = true)
        if (enableSerial == true) Serial.begin(9600);


        // Attach Servo and Pin Mode
        myservo.attach(pinServo);
        pinMode(digitalButtonPin[0], INPUT);
        pinMode(digitalButtonPin[1], INPUT);

}




// Loop

void loop()
{

        // Servo
        if (digitalRead(digitalButtonPin[0]) == LOW) {

                pos += 20;
                if (pos > 180) pos = 180;
                myservo.write(pos);
                delay(200);

        }
        if (digitalRead(digitalButtonPin[1]) == LOW) {

                pos -= 20;
                if (pos < 0) pos =  0;
                myservo.write(pos);
                delay(200);

        }


        // Serial
        if (enableSerial == true) {
                Serial.print(" ServoMotor ");
        }

}