Software that allows basic remote control of the position of a servo (schematic included in the comments)

Dependencies:   MyDS1820 mbed

Fork of Nucleo_sg90_remote_control by YP Roy

Committer:
YPROY
Date:
Wed Mar 08 23:15:42 2017 +0000
Revision:
3:91070e0c0431
Parent:
2:7aacbdf39425
Child:
4:85a8391945b2
Limit index counter to NUMBER_OF_POSITIONS - 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YPROY 1:df2cee74ab8b 1 //This software is derived from the example Nucleo_pwm.
YPROY 1:df2cee74ab8b 2 //It outputs a PWM signal that is compatible with Tower Pro SG90 servos.
YPROY 1:df2cee74ab8b 3 //It makes the servo move from one programmed position to another each time the
YPROY 1:df2cee74ab8b 4 //user button is pressed.
YPROY 1:df2cee74ab8b 5 //The programmed positions are defined by the values that are stored in
YPROY 1:df2cee74ab8b 6 //the variable "pulseDurationInMicroSeconds".
YPROY 1:df2cee74ab8b 7 //An index counter is used to select which value is to be used to set the pulse
YPROY 1:df2cee74ab8b 8 //duration of the PWM signal that controls that servo.
YPROY 1:df2cee74ab8b 9 //The value of this index counter is changed each time the user button is
YPROY 1:df2cee74ab8b 10 //pressed and the changes are made in a way that makes the program repeatedly
YPROY 1:df2cee74ab8b 11 //cycles through all the possible positions.
YPROY 1:df2cee74ab8b 12 //It is just necessary to add values to "pulseDurationInMicroSeconds" to have
YPROY 1:df2cee74ab8b 13 //the servo adopt more positions.
YPROY 1:df2cee74ab8b 14
YPROY 1:df2cee74ab8b 15 //Just connect the brown wire of a SG90 servo to GND, its red wire to AVDD and
YPROY 2:7aacbdf39425 16 //its the orange wire to D11 to have the SMT32-F401RE control the servo.
YPROY 1:df2cee74ab8b 17 //
YPROY 0:7a91558f8c41 18
YPROY 1:df2cee74ab8b 19 #include "mbed.h"
YPROY 1:df2cee74ab8b 20 #define NUMBER_OF_POSITIONS sizeof(pulseDurationInMicroSeconds)/sizeof(int)
YPROY 1:df2cee74ab8b 21 #define PWM_PERIOD_FOR_SG90_IN_MS 20
YPROY 1:df2cee74ab8b 22
YPROY 1:df2cee74ab8b 23 DigitalOut userLED(LED1);
YPROY 2:7aacbdf39425 24 PwmOut towerProSG90(D11);
YPROY 3:91070e0c0431 25 PwmOut testOutput(D10);
YPROY 1:df2cee74ab8b 26 InterruptIn userButton(USER_BUTTON);
YPROY 1:df2cee74ab8b 27 int index;
YPROY 3:91070e0c0431 28 int pulseDurationInMicroSeconds[]=
YPROY 3:91070e0c0431 29 // {10, 1500, 1750, 2000, 1750, 1500, 10, 1500, 1250, 1000, 1250, 1500};
YPROY 3:91070e0c0431 30 {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375};
YPROY 3:91070e0c0431 31 //{700, 1550, 2500, 1550}
YPROY 1:df2cee74ab8b 32 void responseToUserButtonPressed(void)
YPROY 0:7a91558f8c41 33 {
YPROY 1:df2cee74ab8b 34 index++;
YPROY 1:df2cee74ab8b 35 if (index >= NUMBER_OF_POSITIONS)
YPROY 0:7a91558f8c41 36 {
YPROY 1:df2cee74ab8b 37 index = 0;
YPROY 0:7a91558f8c41 38 }
YPROY 1:df2cee74ab8b 39 towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
YPROY 3:91070e0c0431 40 testOutput.pulsewidth_us(20000 - pulseDurationInMicroSeconds[index]);
YPROY 0:7a91558f8c41 41 }
YPROY 0:7a91558f8c41 42
YPROY 1:df2cee74ab8b 43 int main()
YPROY 1:df2cee74ab8b 44 {
YPROY 1:df2cee74ab8b 45 index = 0;
YPROY 1:df2cee74ab8b 46 towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
YPROY 1:df2cee74ab8b 47 towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
YPROY 3:91070e0c0431 48 testOutput.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
YPROY 3:91070e0c0431 49 testOutput.pulsewidth_us(20000 - pulseDurationInMicroSeconds[index]);
YPROY 1:df2cee74ab8b 50 userButton.fall(&responseToUserButtonPressed);
YPROY 0:7a91558f8c41 51
YPROY 1:df2cee74ab8b 52 while(1)
YPROY 1:df2cee74ab8b 53 {
YPROY 1:df2cee74ab8b 54 userLED = !userLED;
YPROY 0:7a91558f8c41 55 wait(1);
YPROY 0:7a91558f8c41 56 }
YPROY 0:7a91558f8c41 57 }