Sergio Collado / Mbed 2 deprecated servo_xperiment

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //playing with servos...
00004 // Controlling the servo position with a potenciometer.
00005 
00006 // the servo must be powered from an external device, 
00007 // since mbed, will cut it out when the  current goes up 400mA.
00008 // just remember to ground the external batteries, chargers, ...
00009 // to the GND pin. 
00010 
00011 //  trick!:  when control signal goes to 0V. the servo goes freely and dont
00012 // hold any stall torque!-> that could be useful sometimes, ... to load some
00013 // mechanism, or as a safety measurement.
00014         
00015 
00016 PwmOut myservo(p21); //PWM servo control signal goes here
00017 
00018 //   ______       Position 0 degrees
00019 //  |      |_______________________________________________
00020 //  <-1ms->  0.05%duty cycle
00021 //
00022 //   __________________     Position 180 degrees
00023 //  |                  |___________________________________
00024 //  <-------2.1ms----->  0.1%aprox. duty cycle
00025 //
00026 //  <----------------------------20ms---------------------->
00027 
00028 
00029 
00030 AnalogIn input(p20); //analog input is wired to a potenciometer, range 0->3.3V
00031                      //and the value is represented as a float, range 0->1.
00032 
00033 //as the input range is from 0 to 1, and the PWM control, ranges is 
00034 //from 0.05% to 0.1% is pretty easy make the control! 
00035 //with the PWM .write() method,sets the duty cycle specified as a fload in range 0->1
00036 //so, for the control only divide by 10 the input, is needed.
00037 
00038 
00039 //some ultra-light blue leds, for adding a cool effect!
00040 DigitalOut led1(LED1);
00041 DigitalOut led2(LED2);
00042 DigitalOut led3(LED3);
00043 DigitalOut led4(LED4);
00044 
00045 int main() {
00046      float ain;    //ain-> Analog INput<- connected to a potenciometer for controlling the servo
00047      float period=20;
00048   
00049      myservo.period_ms(period);
00050      
00051     while (1) {
00052     
00053       ain=input.read();//the analog reading, is expressed as a float
00054                        // with values from 0 to 1. 1 stands for max. voltage= 3.3V
00055         
00056         if (ain<0.02){                   //if voltage is sooo low...
00057         led1=0; led2=0; led3=0; led4=0;  //no leds!
00058         ain=0;                           //no control signal-> no torque, so you can 
00059                                          //use it as safety measurement, or to manualy move the servo position.
00060         }
00061         else if ( ain>0.02 && ain<0.33){  // When the LED1 turns on, is because you have just hitted the 0 degrees position.
00062         led1=1; led2=0; led3=0; led4=0;   // LED1 will be on from 0 to 60 degrees. ... aprox. :)
00063         }
00064         else if ( ain>0.33 && ain<0.66){   // LED2 switch on, when you pass the 60 degrees.
00065         led1=1; led2=1; led3=0; led4=0;
00066         }
00067         else if ( ain>0.66 && ain<0.95){  // LED3 switch on, when you pass 120 degrees
00068         led1=1; led2=1; led3=1; led4=0;
00069         } 
00070         else {                           // LED4. you are at 180 degrees.
00071         led1=1; led2=2; led3=1; led4=1;
00072         }
00073         
00074         
00075         myservo.write(ain/10);
00076         
00077         } // end while(1)
00078     
00079 }//end main()
00080 
00081 
00082 
00083