Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 2:bf817b299c19
- Parent:
- 1:8c73948a0864
- Child:
- 3:66a52943c525
--- a/main.cpp Thu Oct 08 19:56:17 2015 +0000 +++ b/main.cpp Thu Oct 08 23:06:45 2015 +0000 @@ -1,26 +1,78 @@ /* Using pwm to run a servo motor Connect the red wire of the servo motor to 3.3V and not 5V + +DC motor pins - p5 and p6 to control the direction of the motor + p23 to control the duty cycle and period */ #include "mbed.h" -PwmOut pwm1(p21); +PwmOut pwm1(p21); //Servo Motor-1 PWM channel +PwmOut pwm2(p22); //Servo Motor-2 PWM channel + +DigitalOut dc1(p5); //DC motor input-1 +DigitalOut dc2(p6); //DC motor input-2 +PwmOut pwm3(p23); //DC motor PWM channel + Serial pc(USBTX, USBRX); +void openGate(); +void closeGate(); +int currentState = 1; + int main() { + //Setting dc1 to high and dc2 to low initially + dc1 = 1; + dc2 = 0; + pwm3.period_ms(20); + pwm3.write(0); + + //Setting the period and duty cycle for Servo motors pwm1.period_ms(20); + pwm2.period_ms(20); + pwm1.write(0); + pwm2.write(0); while(1){ char c = pc.getc(); - if(c=='1'){ - pwm1.write(0); - wait(0.5); - pwm1.write(0.0375); // 3.75% duty cycle - Open the gate + int val = c - 48; + if(val==currentState){ + pwm3.write(0); + } + else if(val > currentState){ + //Move Up + dc1 = 1; + dc2 = 0; + pwm3.write(0.1); + wait(2); + pwm3.write(0); + }else{ + //Move Down + dc1 = 0; + dc2 = 1; + pwm3.write(0.1); + wait(2); + pwm3.write(0); } - else{ - pwm1.write(0); - wait(0.5); - pwm1.write(0.1125); // 11.25% duty cycle - Close the gate - } + currentState = val; + openGate(); + closeGate(); } - return 0; +} + +void openGate(){ + pwm1.write(0.0375); // 3.75% duty cycle - Open the gate + pwm2.write(0.1125); // 11.25% duty cycle - Open the gate + wait(2); // 2 sec delay + pwm1.write(0); // Stop + pwm2.write(0); // Stop + wait(2); } + +void closeGate(){ + pwm1.write(0.1125); // 11.25% duty cycle - Close the gate + pwm2.write(0.0375); // 3.75% duty cycle - Close the gate + wait(2); // 2 sec delay + pwm1.write(0); // Stop + pwm2.write(0); // Stop + wait(2); +} \ No newline at end of file