/*############################################################# Program Name : ex1_servo_switches Author : Grant Phillips Date Modified : 14/01/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example program that changes the position of a servo based on the inputs from 8 switches. Each switch will correspond to a specific pulse value, with SW0 being 700us and SW7 being 2300us. SW0 has the lowest priority and SW7 has the highest, i.e. if SW0 and SW3 is both ON, SW3 will take priority. If all the switches are off the servo will move to the centre position (1500us) Requirements : * STM32F4-Discovery Board Circuit : * Servo control signal connected to PA15 * 8 switches connected to PE8 - PE15 with pullup resitors ##############################################################*/ #include "mbed.h" DigitalIn SW0(PE_8); DigitalIn SW1(PE_9); DigitalIn SW2(PE_10); DigitalIn SW3(PE_11); DigitalIn SW4(PE_12); DigitalIn SW5(PE_13); DigitalIn SW6(PE_14); DigitalIn SW7(PE_15); PwmOut servo(PA_15); //default period is 20ms (which is perfect for servos) int main() { while(1) { if(SW7 == 0) //remember the switches are inverse logic servo.pulsewidth_us(2300); else if(SW6 == 0) servo.pulsewidth_us(2000); else if(SW5 == 0) servo.pulsewidth_us(1800); else if(SW4 == 0) servo.pulsewidth_us(1600); else if(SW3 == 0) servo.pulsewidth_us(1400); else if(SW2 == 0) servo.pulsewidth_us(1200); else if(SW1 == 0) servo.pulsewidth_us(1000); else if(SW0 == 0) servo.pulsewidth_us(700); else servo.pulsewidth_us(1500); //centre position if all switches are off } }