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: MODSERIAL QEI mbed
main.cpp
00001 #include "mbed.h" 00002 #include "QEI.h" 00003 #include "MODSERIAL.h" 00004 //#include <"math.h"> 00005 00006 QEI motor1(D13,D12,NC, 624);//encoder for motor 1 00007 QEI motor2(D11,D10,NC, 624);//encoder for motor 2 00008 MODSERIAL pc(USBTX,USBRX); 00009 DigitalOut direction1(D7);//direction input for motor 1 00010 DigitalOut direction2(D4);//direction input for motor 2 00011 PwmOut speed1(D6);//speed input for motor 1 00012 PwmOut speed2(D5);//speed input for motor 2 00013 InterruptIn button1(PTC6);//test button for starting motor 1 using intteruptin function 00014 InterruptIn button2(PTA4);//test button for starting motor 2 using interruptin function 00015 DigitalOut led1(LED_RED); 00016 DigitalOut led2(LED_BLUE); 00017 DigitalOut led3(LED_GREEN); 00018 bool motor1_dir=0;//set the direction of motor 1 00019 bool motor2_dir = 0;//set the direction of motor 1 00020 00021 void changedirmotor1(){ 00022 motor1_dir = !motor1_dir;//code for changing direction of motor 1 00023 } 00024 void changedirmotor2(){ 00025 motor2_dir = !motor2_dir;//code for changing direction of motor 2 00026 } 00027 00028 00029 int main() 00030 { 00031 float cycle = 0.3;//define the speed of the motor 00032 bool motor1_on = 1;//set the on variable of motor 1 00033 00034 bool motor2_on =1;//set the on variable of motor 2 00035 int n1=1;//numeric condtions to determine if the speed needs to be increased 00036 int n2=1; 00037 bool CW =1; 00038 bool CCW = 0; 00039 button1.mode(PullDown);//setting the modes for the way the button signals are registered 00040 button2.mode(PullDown); 00041 00042 while(1){ 00043 led3.write(0); 00044 led1.write(1); 00045 led2.write(1); 00046 pc.baud(115200); 00047 00048 button1.fall(changedirmotor1);//replacement code for toggle function, thus changing direction when pressed 00049 /* 00050 int diffa1 = -button1.read();//read out the button 1 signal and calculate if it is being pressed or released interrupt function might work better as toggle 00051 wait(0.3);//from this we can determine if the rotation direction needs to be reversed. 00052 int diffb1 = button1.read(); 00053 int button_toggle1 = diffa1-diffb1; 00054 if(button_toggle1 == 1 && motor1_dir == CW){ 00055 motor1_dir = CCW; 00056 } 00057 else if(button_toggle1 == 1 && motor1_dir == CCW){ 00058 motor1_dir = CW; 00059 } 00060 */ 00061 if(button1.read() !=motor1_on){//check if button 1 is pressed 00062 led3.write(1); 00063 led1.write(0); 00064 while(n1 == 1){ 00065 speed1.write(cycle);//write speed only on first run through the loop 00066 direction1.write(motor1_dir);//turn motor CCW or CW 00067 00068 n1=0; 00069 } 00070 } 00071 else if (button1.read() == motor1_on){//when button is released 00072 while(n1==0){//check if the first run was done 00073 speed1.write(0);//if so set speed to 0 and reset the run counter 00074 n1=1; 00075 } 00076 } 00077 button2.fall(changedirmotor2);//replacement code for toggle function, thus changing direction when pressed 00078 00079 /* 00080 int diffa2 = button2.read();//read out the button 2 signal and calculate if it is being pressed or released 00081 wait(0.3);//from this we can determine if the rotation direction needs to be reversed. 00082 int diffb2 = button2.read(); 00083 int button_toggle2 = diffa2-diffb2; 00084 if(button_toggle2 == 1 && motor2_dir == CW){ 00085 motor2_dir = CCW; 00086 } 00087 else if(button_toggle2 == 1 && motor2_dir == CCW){ 00088 motor2_dir = CW; 00089 } 00090 */ 00091 00092 if(button2.read()!=motor2_on){//check if button 2 is pressed 00093 led3.write(1); 00094 led2.write(0); 00095 while(n2 == 1){ 00096 speed2.write(cycle);//write speed only on first run through the loop 00097 direction2.write(motor2_dir);//turn motor CCW or CW 00098 00099 n2=0; 00100 } 00101 } 00102 else if (button2.read() == motor2_on){//when button is released 00103 while(n2==0){//check if the first run was done 00104 speed2.write(0);//if so set speed to 0 and reset the run counter 00105 n2=1; 00106 } 00107 } 00108 } 00109 }
Generated on Tue Aug 9 2022 19:57:55 by
1.7.2