Code for motor driver

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //Setting the output pins for the motor driver
00004 DigitalOut RL(p15);
00005 DigitalOut RH(p16);
00006 DigitalOut LL(p17);
00007 DigitalOut LH(p18);
00008 
00009 //Setting an indicating LED
00010 DigitalOut LED(LED1);
00011 
00012 //Setting a Pin to power a switch 
00013 DigitalOut On(p10);
00014 
00015 //Setting the Input Pin
00016 DigitalIn S1(p13);
00017 
00018 //Function to get the motor to run one way
00019 void Run_a(){
00020     RH = 1;
00021     LL = 1;
00022     LH = 0;
00023     RL = 0;    
00024 }
00025 
00026 //Function to get the motor to run another way
00027 void Run_b(){
00028     LH = 1;
00029     RL = 1;
00030     RH = 0;
00031     LL = 0;  
00032 }
00033 
00034 //Function to turn off the motor
00035 void off(){
00036     LH = 0;
00037     RL = 0;
00038     RH = 0;
00039     LL = 0;  
00040 }
00041 
00042 //Function to set the motor to 0V
00043 void discharge(){
00044     LH = 0;
00045     RL = 1;
00046     RH = 0;
00047     LL = 1; 
00048 }
00049 
00050 //Function to test the operation of the pins
00051 void test(){
00052     LH = 1;
00053     RL = 1;
00054     RH = 1;
00055     LL = 1; 
00056 }
00057 
00058 int main() {
00059     
00060     On = 1;
00061     
00062     //Initial setting the motor to 0V and switching off
00063     discharge();
00064     off();
00065     
00066     while(1) {
00067         //Wait until the motor is on
00068         if(S1 == 1){
00069             LED = 1;
00070             //Turning the motor on for a set time
00071             test();
00072             wait(0.045);
00073             off();
00074             //A wait time to stop de-bouncing from the switch
00075             wait(0.5);
00076         }
00077         LED = 0;
00078     }
00079 }