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
main.cpp
- Committer:
- jiteshg
- Date:
- 2015-10-08
- Revision:
- 2:bf817b299c19
- Parent:
- 1:8c73948a0864
- Child:
- 3:66a52943c525
File content as of revision 2:bf817b299c19:
/* 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); //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(); 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); } currentState = val; openGate(); closeGate(); } } 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); }