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

Committer:
YROY2004
Date:
Fri Mar 24 17:34:09 2017 +0000
Revision:
0:52104be74a8a
version that works on both the f302r8 and the f401re

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YROY2004 0:52104be74a8a 1 #include "mbed.h"
YROY2004 0:52104be74a8a 2 #define NUMBER_OF_POSITIONS sizeof(pulseDurationInMicroSeconds)/sizeof(int)
YROY2004 0:52104be74a8a 3 #define PWM_PERIOD_FOR_SG90_IN_MS 20
YROY2004 0:52104be74a8a 4 #define PWM_PERIOD_FOR_MODULATION_IN_US 25
YROY2004 0:52104be74a8a 5 DigitalOut myled(LED1);
YROY2004 0:52104be74a8a 6 PwmOut towerProSG90(D11);
YROY2004 0:52104be74a8a 7 //PwmOut remoteControlOutput(D10);
YROY2004 0:52104be74a8a 8 //PwmOut modulatingOutput(D9);
YROY2004 0:52104be74a8a 9 InterruptIn userButton(USER_BUTTON);
YROY2004 0:52104be74a8a 10 int index;
YROY2004 0:52104be74a8a 11 int pulseDurationInMicroSeconds[]=
YROY2004 0:52104be74a8a 12 {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375};
YROY2004 0:52104be74a8a 13 void responseToUserButtonPressed(void)
YROY2004 0:52104be74a8a 14 {
YROY2004 0:52104be74a8a 15 index++;
YROY2004 0:52104be74a8a 16 if (index >= NUMBER_OF_POSITIONS)
YROY2004 0:52104be74a8a 17 {
YROY2004 0:52104be74a8a 18 index = 0;
YROY2004 0:52104be74a8a 19 }
YROY2004 0:52104be74a8a 20 towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
YROY2004 0:52104be74a8a 21 // remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
YROY2004 0:52104be74a8a 22 }
YROY2004 0:52104be74a8a 23
YROY2004 0:52104be74a8a 24 int main() {
YROY2004 0:52104be74a8a 25 index = 0;
YROY2004 0:52104be74a8a 26 towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
YROY2004 0:52104be74a8a 27 towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
YROY2004 0:52104be74a8a 28 // remoteControlOutput.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
YROY2004 0:52104be74a8a 29 // remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
YROY2004 0:52104be74a8a 30 // modulatingOutput.period_us(PWM_PERIOD_FOR_MODULATION_IN_US);
YROY2004 0:52104be74a8a 31 // modulatingOutput.pulsewidth_us(PWM_PERIOD_FOR_MODULATION_IN_US/2);
YROY2004 0:52104be74a8a 32 userButton.fall(&responseToUserButtonPressed);
YROY2004 0:52104be74a8a 33 // userLED = 1;
YROY2004 0:52104be74a8a 34 while(1) {
YROY2004 0:52104be74a8a 35 myled = 1; // LED is ON
YROY2004 0:52104be74a8a 36 wait(0.2); // 200 ms
YROY2004 0:52104be74a8a 37 myled = 0; // LED is OFF
YROY2004 0:52104be74a8a 38 wait(1.0); // 1 sec
YROY2004 0:52104be74a8a 39 }
YROY2004 0:52104be74a8a 40 }