RTES / Mbed 2 deprecated mbed_pwmLib

Dependencies:   mbed

Committer:
jiteshg
Date:
Thu Oct 08 23:06:45 2015 +0000
Revision:
2:bf817b299c19
Parent:
1:8c73948a0864
Child:
3:66a52943c525
Added DC motor and serial input; Simple model ready!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jiteshg 1:8c73948a0864 1 /*
jiteshg 1:8c73948a0864 2 Using pwm to run a servo motor
jiteshg 1:8c73948a0864 3 Connect the red wire of the servo motor to 3.3V and not 5V
jiteshg 2:bf817b299c19 4
jiteshg 2:bf817b299c19 5 DC motor pins - p5 and p6 to control the direction of the motor
jiteshg 2:bf817b299c19 6 p23 to control the duty cycle and period
jiteshg 1:8c73948a0864 7 */
jiteshg 0:fd080fb55bae 8 #include "mbed.h"
jiteshg 2:bf817b299c19 9 PwmOut pwm1(p21); //Servo Motor-1 PWM channel
jiteshg 2:bf817b299c19 10 PwmOut pwm2(p22); //Servo Motor-2 PWM channel
jiteshg 2:bf817b299c19 11
jiteshg 2:bf817b299c19 12 DigitalOut dc1(p5); //DC motor input-1
jiteshg 2:bf817b299c19 13 DigitalOut dc2(p6); //DC motor input-2
jiteshg 2:bf817b299c19 14 PwmOut pwm3(p23); //DC motor PWM channel
jiteshg 2:bf817b299c19 15
jiteshg 1:8c73948a0864 16 Serial pc(USBTX, USBRX);
jiteshg 0:fd080fb55bae 17
jiteshg 2:bf817b299c19 18 void openGate();
jiteshg 2:bf817b299c19 19 void closeGate();
jiteshg 2:bf817b299c19 20 int currentState = 1;
jiteshg 2:bf817b299c19 21
jiteshg 0:fd080fb55bae 22 int main() {
jiteshg 2:bf817b299c19 23 //Setting dc1 to high and dc2 to low initially
jiteshg 2:bf817b299c19 24 dc1 = 1;
jiteshg 2:bf817b299c19 25 dc2 = 0;
jiteshg 2:bf817b299c19 26 pwm3.period_ms(20);
jiteshg 2:bf817b299c19 27 pwm3.write(0);
jiteshg 2:bf817b299c19 28
jiteshg 2:bf817b299c19 29 //Setting the period and duty cycle for Servo motors
jiteshg 0:fd080fb55bae 30 pwm1.period_ms(20);
jiteshg 2:bf817b299c19 31 pwm2.period_ms(20);
jiteshg 2:bf817b299c19 32 pwm1.write(0);
jiteshg 2:bf817b299c19 33 pwm2.write(0);
jiteshg 1:8c73948a0864 34
jiteshg 1:8c73948a0864 35 while(1){
jiteshg 1:8c73948a0864 36 char c = pc.getc();
jiteshg 2:bf817b299c19 37 int val = c - 48;
jiteshg 2:bf817b299c19 38 if(val==currentState){
jiteshg 2:bf817b299c19 39 pwm3.write(0);
jiteshg 2:bf817b299c19 40 }
jiteshg 2:bf817b299c19 41 else if(val > currentState){
jiteshg 2:bf817b299c19 42 //Move Up
jiteshg 2:bf817b299c19 43 dc1 = 1;
jiteshg 2:bf817b299c19 44 dc2 = 0;
jiteshg 2:bf817b299c19 45 pwm3.write(0.1);
jiteshg 2:bf817b299c19 46 wait(2);
jiteshg 2:bf817b299c19 47 pwm3.write(0);
jiteshg 2:bf817b299c19 48 }else{
jiteshg 2:bf817b299c19 49 //Move Down
jiteshg 2:bf817b299c19 50 dc1 = 0;
jiteshg 2:bf817b299c19 51 dc2 = 1;
jiteshg 2:bf817b299c19 52 pwm3.write(0.1);
jiteshg 2:bf817b299c19 53 wait(2);
jiteshg 2:bf817b299c19 54 pwm3.write(0);
jiteshg 1:8c73948a0864 55 }
jiteshg 2:bf817b299c19 56 currentState = val;
jiteshg 2:bf817b299c19 57 openGate();
jiteshg 2:bf817b299c19 58 closeGate();
jiteshg 1:8c73948a0864 59 }
jiteshg 2:bf817b299c19 60 }
jiteshg 2:bf817b299c19 61
jiteshg 2:bf817b299c19 62 void openGate(){
jiteshg 2:bf817b299c19 63 pwm1.write(0.0375); // 3.75% duty cycle - Open the gate
jiteshg 2:bf817b299c19 64 pwm2.write(0.1125); // 11.25% duty cycle - Open the gate
jiteshg 2:bf817b299c19 65 wait(2); // 2 sec delay
jiteshg 2:bf817b299c19 66 pwm1.write(0); // Stop
jiteshg 2:bf817b299c19 67 pwm2.write(0); // Stop
jiteshg 2:bf817b299c19 68 wait(2);
jiteshg 0:fd080fb55bae 69 }
jiteshg 2:bf817b299c19 70
jiteshg 2:bf817b299c19 71 void closeGate(){
jiteshg 2:bf817b299c19 72 pwm1.write(0.1125); // 11.25% duty cycle - Close the gate
jiteshg 2:bf817b299c19 73 pwm2.write(0.0375); // 3.75% duty cycle - Close the gate
jiteshg 2:bf817b299c19 74 wait(2); // 2 sec delay
jiteshg 2:bf817b299c19 75 pwm1.write(0); // Stop
jiteshg 2:bf817b299c19 76 pwm2.write(0); // Stop
jiteshg 2:bf817b299c19 77 wait(2);
jiteshg 2:bf817b299c19 78 }