3 button servo control for HS422 with mbed

Dependencies:   mbed

main.cpp

Committer:
asloop18
Date:
2016-01-07
Revision:
0:c21f0ed70bcd
Child:
1:a00c2b3c73e8

File content as of revision 0:c21f0ed70bcd:

/* Servo controller mini-project. Three button control for HS422 servo, left 10*, right 10*, reset to center. 
Austin Sloop, 1/7/16
*/

#include "mbed.h"

DigitalIn right(p5);        //Right button, CW
DigitalIn left(p9);         //Left button, CCW
DigitalIn reset(p7);        //Middle button, reset to 0

PwmOut servo(p21);          //Servo Control
DigitalOut L(LED1);
DigitalOut R(LED3);         //Debugging Purposes
DigitalOut C(LED2);

char a;                   //value of switching
int i=1500;               //value of servo position
int main(){
servo.period(0.020);
    while(1){       
        if (right==1){
            a=3; 
            R=1;
            wait(.5);
            R=0;   
        }
        if (left==1) {
            a=1;
            L=1;
            wait(1);
            L=0;
        }
        if (reset==1) {
            a=2;
            C=1;
            wait(1);
            C=0;
        }
        
        switch(a){
            case 0: break;
            case 1: i=i-100; break;         //CCW 10
            case 2: i=1500; break;          //Reset 0 
            case 3: i=i+100; break;         //CW 10
        }
        servo.pulsewidth_us(i);
        wait(1); 
        a=0; 
        
    } 
}