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

Dependencies:   mbed

Revision:
4:85a8391945b2
Parent:
3:91070e0c0431
--- a/main.cpp	Wed Mar 08 23:15:42 2017 +0000
+++ b/main.cpp	Thu Mar 09 01:16:43 2017 +0000
@@ -1,4 +1,5 @@
-//This software is derived from the example Nucleo_pwm.
+//247-426-Nucleo_Lab_7
+//This software is derived from Nucleo_pwm_servo_sg90.
 //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.
@@ -14,21 +15,61 @@
 
 //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 control the servo.
-// 
+//
+
+//Remote control of a SG90 servo is also possible if the Nucleo board is allowed
+//to control an infrared LED as an infrared receptor is connected to the servo.
+//
+//material:
+//      Infrared LED = LTE-5802A, Receiver = RPM7140-V4R
+//
+//signals:
+//      D11= pwm signal to use to control SG90 directly
+//      D10= !D11 = signal to use to control the transmitter's LED
+//      D9 = 40kHz signal to use to modulate the LED output
+//
+//
+//Transmitter: (powered by Nucleo's supply)
+//   +5 ---/\/\/\----------- |
+//           180             |
+//                           |_
+//                           \/  LTE-5802A
+//                           --
+//                     ______|______
+//                   |/             \|
+//  D10---/\/\/\-----|               |-----/\/\/\----- D9
+//  (!pwm)   10k      |\ <-emitter-> /|       10k      (40kHz)
+//                    |             |
+//                   GND           GND
+//
+//  GND of Nucleo board ------ GND
+//
+//Receiver: (powered by remote supply)
+//
+//                                                             _____
+//                                                             | O | RPM7140-V4R
+//                                      +5---/\/\/\----        ----- (front view)
+//                                            10k     |        | | |
+//                                                    |        | | |______+5v
+//      Orange wire of servo SG90 --------------------.--      | |________GND 
+//      Red wire of servo SG90 -----+5V                  \|    | 
+//      Brown wire of servo SG90 ---GND                   |-----
+//                                              emitter  /|
+//                                                       |
+//                                                      GND
 
 #include "mbed.h"
 #define NUMBER_OF_POSITIONS sizeof(pulseDurationInMicroSeconds)/sizeof(int)
-#define PWM_PERIOD_FOR_SG90_IN_MS  20
-
+#define PWM_PERIOD_FOR_SG90_IN_MS           20
+#define PWM_PERIOD_FOR_MODULATION_IN_US     25  
 DigitalOut userLED(LED1);
 PwmOut towerProSG90(D11);
-PwmOut testOutput(D10);
+PwmOut remoteControlOutput(D10);
+PwmOut modulatingOutput(D9);
 InterruptIn userButton(USER_BUTTON);
 int index;
 int pulseDurationInMicroSeconds[]=
-//    {10, 1500, 1750, 2000, 1750, 1500, 10, 1500, 1250, 1000, 1250, 1500};
-    {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375};    
-//{700, 1550, 2500, 1550}
+        {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375};    
 void responseToUserButtonPressed(void)
 {
     index++;
@@ -37,7 +78,7 @@
         index = 0;
     }
     towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);    
-    testOutput.pulsewidth_us(20000 - pulseDurationInMicroSeconds[index]);
+    remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
 }
 
 int main()
@@ -45,8 +86,10 @@
     index = 0;    
     towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
     towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
-    testOutput.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
-    testOutput.pulsewidth_us(20000 - pulseDurationInMicroSeconds[index]);
+    remoteControlOutput.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
+    remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
+    modulatingOutput.period_us(PWM_PERIOD_FOR_MODULATION_IN_US);
+    modulatingOutput.pulsewidth_us(PWM_PERIOD_FOR_MODULATION_IN_US/2);
     userButton.fall(&responseToUserButtonPressed);
       
     while(1)