PWM with L298 for two DC Motors.
Dependencies: mbed FPointer TextLCD1 keypad
Fork of L298 by
Diff: main.cpp
- Revision:
- 4:72727d4c1d72
- Parent:
- 2:2e93c305bb62
- Child:
- 5:486d0a696705
--- a/main.cpp Thu Jun 23 12:43:49 2016 +0000 +++ b/main.cpp Thu Jul 14 22:49:36 2016 +0000 @@ -1,4 +1,17 @@ #include "mbed.h" +# define ON 1 +# define OFF 0 + +////////////////********Definitions***********/////// +DigitalIn sw3(PTA4); + +DigitalOut led_red(LED_RED); +DigitalOut led_green(LED_GREEN); +DigitalOut led_blue(LED_BLUE); + +InterruptIn sw2(SW2); + +Timer debounce; // define debounce timer Serial pc(USBTX,USBRX); // tx,rx @@ -6,49 +19,70 @@ DigitalOut in2(PTC2); PwmOut ena(PTA2); + DigitalOut in3(PTD1); DigitalOut in4(PTD2); PwmOut enb(PTD3); -void rxInterrupt(){ -} +///////////****Prototype Functions*******////////// +void stop1(void); +void turnRight1(void); +void turnLeft1(void); +void toggle(void); // function prototype + +/////////////////////////////////////////////////// + int main() { -pc.baud(115200); -pc.attach(&rxInterrupt); +debounce.start(); +ena.period(1.0/10000.0);// 5Khz period + sw2.rise(&turnRight1); // attach the address of the toggle + sw2.fall(&stop1); +//pc.baud(115200); + while (true) { - /////******MOTOR 1****//// - // Detiene el motor - in1 = 0; - in2 = 0; - wait(0.5f); - // Gira en sentido 1 con velocidad 50% - in1 = 1; - in2 = 0; - ena.write(0.50f); - wait(0.5f); - // Gira en sentido 2 con velocidad 100% - in1 = 0; - in2 = 1; - ena.write(1.00f); - wait(0.5f); + + sw2.rise(&turnRight1); // attach the address of the toggle + sw2.fall(&stop1); - /////******MOTOR 2****//// - /* in3 = 0; - in4 = 0; - wait(0.5f); - // Gira en sentido 1 con velocidad 50% - in3 = 1; - in4 = 0; - ena.write(0.50f); - wait(0.5f); - // Gira en sentido 2 con velocidad 100% - in3 = 0; - in4 = 1; - ena.write(1.00f); - wait(0.5f);*/ - pc.printf("MOTOR Funcionando\n"); - + /* if (sw3 == ON){ + led_red = ON; + turnRight1(); + ena.write(0.50f); + } + else{ + led_red = OFF; + stop1(); + wait(0.5f); + }*/ + } +} + +///////////////***********FUNCTIONS***********************////////////// +void stop1(void) { + //led_red = ON; + in1 = OFF; + in2 = OFF; + //wait(0.5f); +} + +void turnRight1(void) { + // led_red = ON; + in1 = ON; + in2 = OFF; + +} + +void turnLeft1() { + led_blue = ON; + in1 = OFF; + in2 = ON; +} + +void toggle() { +if (debounce.read_ms()>200) // only allow toggle if debounce timer +led_red=!led_red; // has passed 200 ms + debounce.reset(); // restart timer when the toggle is performed } \ No newline at end of file