PWM with L298 for two DC Motors.

Dependencies:   mbed FPointer TextLCD1 keypad

Fork of L298 by Juan Carlos Suárez Barón

main.cpp

Committer:
juanitoinig84
Date:
2016-07-14
Revision:
4:72727d4c1d72
Parent:
2:2e93c305bb62
Child:
5:486d0a696705

File content as of revision 4:72727d4c1d72:

#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
DigitalOut in1(PTC3);
DigitalOut in2(PTC2);
PwmOut     ena(PTA2);


DigitalOut in3(PTD1);
DigitalOut in4(PTD2);
PwmOut     enb(PTD3);

///////////****Prototype Functions*******//////////
void stop1(void);
void turnRight1(void);
void turnLeft1(void);
void toggle(void); // function prototype

///////////////////////////////////////////////////

int main()

{
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) {
        
        sw2.rise(&turnRight1); // attach the address of the toggle
        sw2.fall(&stop1);
        
       /* 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
}