Example to sweep a servo through its range

Dependencies:   Servo mbed

Fork of Servo_HelloWorld by Simon Ford

Revision:
2:da287ee6fb03
Parent:
1:40d2fd0b99e6
--- a/main.cpp	Tue Nov 23 16:10:35 2010 +0000
+++ b/main.cpp	Tue Jul 14 14:16:12 2015 +0000
@@ -1,13 +1,92 @@
-// Hello World to sweep a servo through its full range
+// Servo Motor controlled with Terminal
 
 #include "mbed.h"
 #include "Servo.h"
 
-Servo myservo(p21);
+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')) {
 
-int main() {    
-    for(float p=0; p<1.0; p += 0.1) {
-        myservo = p;
-        wait(0.2);
+        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);
+
     }
-}
+
+
+}
\ No newline at end of file