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

Revision:
1:df2cee74ab8b
Parent:
0:7a91558f8c41
Child:
2:7aacbdf39425
diff -r 7a91558f8c41 -r df2cee74ab8b main.cpp
--- a/main.cpp	Mon Jan 30 05:20:16 2017 +0000
+++ b/main.cpp	Mon Jan 30 06:07:59 2017 +0000
@@ -1,31 +1,50 @@
-#include "mbed.h"
+//This software is derived from the example Nucleo_pwm.
+//It outputs a PWM signal that is compatible with Tower Pro SG90 servos.
+//It makes the servo move from one programmed position to another each time the
+//user button is pressed.
+//The programmed positions are defined by the values that are stored in
+//the variable "pulseDurationInMicroSeconds". 
+//An index counter is used to select which value is to be used to set the pulse
+//duration of the PWM signal that controls that servo.
+//The value of this index counter is changed each time the user button is
+//pressed and the changes are made in a way that makes the program repeatedly
+//cycles through all the possible positions.
+//It is just necessary to add values to "pulseDurationInMicroSeconds" to have
+//the servo adopt more positions.
+
+//Just connect the brown wire of a SG90 servo to GND, its red wire to AVDD and
+//its the orange wire to D11 to have the SMT32-F401RE contol the servo.
+// 
 
-PwmOut mypwm(PA_7);
-InterruptIn monBouton(USER_BUTTON);
-int compteur;
-int cycle[]={700, 1550, 2500, 1550};
-void pressed(void)
+#include "mbed.h"
+#define NUMBER_OF_POSITIONS sizeof(pulseDurationInMicroSeconds)/sizeof(int)
+#define PWM_PERIOD_FOR_SG90_IN_MS  20
+
+DigitalOut userLED(LED1);
+PwmOut towerProSG90(D11); //PA_7);
+InterruptIn userButton(USER_BUTTON);
+int index;
+int pulseDurationInMicroSeconds[]={700, 1550, 2500, 1550};
+void responseToUserButtonPressed(void)
 {
-    compteur++;
-    if (compteur >= sizeof(cycle)/sizeof(int))
+    index++;
+    if (index >= NUMBER_OF_POSITIONS)
     {
-        compteur = 0;
+        index = 0;
     }
-    mypwm.pulsewidth_us(cycle[compteur]);    
+    towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);    
 }
 
-DigitalOut myled(LED1);
-
-int main() {
-    compteur = 0;    
-    mypwm.period_ms(20);
-    mypwm.pulsewidth_us(500);
-  
-//    printf("pwm set to %.2f %%\n", mypwm.read() * 100);
-    monBouton.fall(&pressed);
+int main()
+{
+    index = 0;    
+    towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
+    towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
+    userButton.fall(&responseToUserButtonPressed);
       
-    while(1) {
-        myled = !myled;
+    while(1)
+    {
+        userLED = !userLED;
         wait(1);
     }
 }