Software that allows basic remote control of the position of a servo (schematic included in the comments)
Fork of Nucleo_sg90_remote_control by
main.cpp@1:df2cee74ab8b, 2017-01-30 (annotated)
- Committer:
- YPROY
- Date:
- Mon Jan 30 06:07:59 2017 +0000
- Revision:
- 1:df2cee74ab8b
- Parent:
- 0:7a91558f8c41
- Child:
- 2:7aacbdf39425
Name changed, comments added and clean up made.
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:df2cee74ab8b | 16 | //its the orange wire to D11 to have the SMT32-F401RE contol 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 | 1:df2cee74ab8b | 24 | PwmOut towerProSG90(D11); //PA_7); |
YPROY | 1:df2cee74ab8b | 25 | InterruptIn userButton(USER_BUTTON); |
YPROY | 1:df2cee74ab8b | 26 | int index; |
YPROY | 1:df2cee74ab8b | 27 | int pulseDurationInMicroSeconds[]={700, 1550, 2500, 1550}; |
YPROY | 1:df2cee74ab8b | 28 | void responseToUserButtonPressed(void) |
YPROY | 0:7a91558f8c41 | 29 | { |
YPROY | 1:df2cee74ab8b | 30 | index++; |
YPROY | 1:df2cee74ab8b | 31 | if (index >= NUMBER_OF_POSITIONS) |
YPROY | 0:7a91558f8c41 | 32 | { |
YPROY | 1:df2cee74ab8b | 33 | index = 0; |
YPROY | 0:7a91558f8c41 | 34 | } |
YPROY | 1:df2cee74ab8b | 35 | towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]); |
YPROY | 0:7a91558f8c41 | 36 | } |
YPROY | 0:7a91558f8c41 | 37 | |
YPROY | 1:df2cee74ab8b | 38 | int main() |
YPROY | 1:df2cee74ab8b | 39 | { |
YPROY | 1:df2cee74ab8b | 40 | index = 0; |
YPROY | 1:df2cee74ab8b | 41 | towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS); |
YPROY | 1:df2cee74ab8b | 42 | towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]); |
YPROY | 1:df2cee74ab8b | 43 | userButton.fall(&responseToUserButtonPressed); |
YPROY | 0:7a91558f8c41 | 44 | |
YPROY | 1:df2cee74ab8b | 45 | while(1) |
YPROY | 1:df2cee74ab8b | 46 | { |
YPROY | 1:df2cee74ab8b | 47 | userLED = !userLED; |
YPROY | 0:7a91558f8c41 | 48 | wait(1); |
YPROY | 0:7a91558f8c41 | 49 | } |
YPROY | 0:7a91558f8c41 | 50 | } |