Control a servo with an analog value (knob). A basic example of using a servo.

Dependencies:   Servo mbed

Committer:
jose_23991
Date:
Fri Sep 19 15:26:02 2014 +0000
Revision:
0:3a3bfe92df7c
Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jose_23991 0:3a3bfe92df7c 1 #include "mbed.h"
jose_23991 0:3a3bfe92df7c 2 #include "Servo.h"
jose_23991 0:3a3bfe92df7c 3
jose_23991 0:3a3bfe92df7c 4 int main()
jose_23991 0:3a3bfe92df7c 5 {
jose_23991 0:3a3bfe92df7c 6 Servo myservo(D3); // Create the servo object
jose_23991 0:3a3bfe92df7c 7 AnalogIn knob(A0); // Create the analog input object
jose_23991 0:3a3bfe92df7c 8
jose_23991 0:3a3bfe92df7c 9 float val;
jose_23991 0:3a3bfe92df7c 10
jose_23991 0:3a3bfe92df7c 11 myservo.calibrate(0.00095, 90.0); // Calibrate the servo
jose_23991 0:3a3bfe92df7c 12
jose_23991 0:3a3bfe92df7c 13 while(1)
jose_23991 0:3a3bfe92df7c 14 {
jose_23991 0:3a3bfe92df7c 15 val = knob.read(); // Reads the value of the potentiometer (value between 0 and 1)
jose_23991 0:3a3bfe92df7c 16 myservo.write(val); // Sets the servo position according to the scaled value (0-1)
jose_23991 0:3a3bfe92df7c 17 wait_ms(15); // Waits for the servo to get there
jose_23991 0:3a3bfe92df7c 18 }
jose_23991 0:3a3bfe92df7c 19 }