To be used to directly or remotely control a servo motor (ex. sg90) Version that works with both the F302R8 and the F401RE

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #define NUMBER_OF_POSITIONS sizeof(pulseDurationInMicroSeconds)/sizeof(int)
00003 #define PWM_PERIOD_FOR_SG90_IN_MS           20
00004 #define PWM_PERIOD_FOR_MODULATION_IN_US     25 
00005 DigitalOut myled(LED1);
00006 PwmOut towerProSG90(D11);
00007 //PwmOut remoteControlOutput(D10);
00008 //PwmOut modulatingOutput(D9);
00009 InterruptIn userButton(USER_BUTTON);
00010 int index;
00011 int pulseDurationInMicroSeconds[]=
00012       {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375};    
00013 void responseToUserButtonPressed(void)
00014 {
00015     index++;
00016     if (index >= NUMBER_OF_POSITIONS)
00017     {
00018         index = 0;
00019     }
00020     towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);    
00021 //    remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
00022 }
00023 
00024 int main() {
00025     index = 0;    
00026     towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
00027     towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
00028  //   remoteControlOutput.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
00029  //   remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
00030  //   modulatingOutput.period_us(PWM_PERIOD_FOR_MODULATION_IN_US);
00031  //   modulatingOutput.pulsewidth_us(PWM_PERIOD_FOR_MODULATION_IN_US/2);
00032     userButton.fall(&responseToUserButtonPressed);
00033  //   userLED = 1;
00034     while(1) {
00035         myled = 1; // LED is ON
00036         wait(0.2); // 200 ms
00037         myled = 0; // LED is OFF
00038         wait(1.0); // 1 sec
00039     }
00040 }