Servo position controlled by potvalue

Dependencies:   Servo



File content as of revision 0:fe46ce7f03d3:

/* This code controls the main functions of the servomotor. The servomotor needs an external power of 6 or 12 V and is connected to the PWMOut pin
External power can be either 4*1.5V AA batteries (6V), 8*1.5V AA batteries (12V) or an appropiate DC voltage source. It is important that ground of both the servo motor and mBed platform are the same. 
Servo motor speed can be controlled by an external signal such as a potnetiometer or in context of this project an EMG signal.

#include <mbed.h>
//#include <Servo.h> either one can be used, but not both at the same time
#include <VarSpeedServo.h>

Servo myservo(p21);
Serial pc(USBTX, USBRX);
int main() {
    printf("Servo Calibration Controls:\n");
    printf("1,2,3 - Position Servo (full left, middle, full right)\n");
    printf("4,5 - Decrease or Increase range\n");
    float range = 0.0005;
    float position = 0.5;
    while(1) {                   
        switch(pc.getc()) { // Switch to change position or increase range
            case '1': position = 0.0; break;
            case '2': position = 0.5; break;
            case '3': position = 1.0; break;
            case '4': range += 0.0001; break; 
            case '5': range -= 0.0001; break; 
        printf("position = %.1f, range = +/-%0.4f\n", position, range);
        myservo.calibrate(range, 45.0); 
        myservo = position;

/* Example code to control the servomotor by a potentiometer, similarly one could control the speed of the servomotor. 
The speed can be controlled by using the library VarSpeedServo.h 
By mapping the intensity of EMG signal to a range of values (e.g. 0:1:100) you can easily control the speed and position of the motor

 Servo servo_test;      //initialize a servo object for the connected servo  
 int angle = 0;    
 int potentio = A0;      // initialize the A0analog pin for potentiometer

 void setup() 
  servo_test.attach(9);     // attach the signal pin of servo to pin9 of arduino
 void loop() 
  angle = analogRead(potentio);            // reading the potentiometer value between 0 and 1023 
  angle = map(angle, 0, 1023, 0, 179);     // scaling the potentiometer value to angle value for servo between 0 and 180) 
  servo_test.write(angle);                   //command to rotate the servo to the specified angle