Example to sweep a servo through its range

Dependencies:   Servo mbed

Fork of Servo_HelloWorld by Simon Ford

main.cpp

Committer:
dreamworker
Date:
2015-07-14
Revision:
2:da287ee6fb03
Parent:
1:40d2fd0b99e6

File content as of revision 2:da287ee6fb03:

// Servo Motor controlled with Terminal

#include "mbed.h"
#include "Servo.h"

Serial pc(USBTX, USBRX);
Servo myservo(D6);
float angolo;
int getangle(){
    printf("\n insert angle value to sweep with servo motor \n");

    int numero = 0;
    int angle=0;
    int minus=0;
    int count = 0;
    char c=pc.getc();
    printf("\n angle: \t");
    int number[2]={0, 0};
    if(c=='-'){
        printf("-");
        minus=1;
        char c=pc.getc();
        angle = c -'0';
        printf("%d",angle);
            }
        else{
        angle = c -'0';
        printf("%d",angle);}      
    while((count !=1) && (c!='\r')) {

        number[count]=angle;
        count++;
        c=pc.getc();
        if (c!='\r'){
        angle = c -'0';
        printf("%d",angle);}
        }
    if (c!='\r'){
      number[count]=angle;}
      else{number[1]=angle;
      number[0]=0;}  
      numero = 10*number[0]+number[1];
      if(minus==0){
      return numero;}
      else{numero=-numero;
      return numero;}
      }  
int main() {
        printf("\033[2J");
        printf("\n*****************************************\n");
        printf("*  Use key q to rotate at 90 degree     *\n");
        printf("*  Use key e to rotate at -90 degree    *\n");
        printf("*  Use key w to rotate at  0 degree     *\n");
        printf("*  Use key a to insert the sweep angle  *\n");
        printf("*Use key s to rotate 1 degree at a time *\n");
        printf("*Use key d to rotate -1 degree at a time*\n");
        printf("*****************************************\n");
        myservo.calibrate(0.0008,90);
        while(1) {
        char c = pc.getc();
        if((c == 's') && (angolo < 90.0)) {
            angolo += 1;
            myservo.position(angolo);
        }
        if((c == 'd') && (angolo > -90.0)) {
            angolo -= 1;
            myservo.position(angolo);
        } 
        if((c == 'q')) {
            angolo= 90;
            myservo.position(angolo);
        } 
        if((c == 'w')) {
            angolo= 0;
            myservo.position(angolo);
        }   
        if((c == 'e')) {
            angolo= -90;
            myservo.position(angolo);
        }   
        if ((c == 'a')){

                angolo = getangle();
                printf("\n Servo motor position:%f\n",angolo);
                myservo.position(angolo);
        }  
        printf("\n Angle : %f", angolo);

    }


}