code for basic movement of robot
Dependencies: MODSERIAL QEI mbed
Diff: main.cpp
- Revision:
- 12:2d3d7a9ca496
- Parent:
- 11:be7660614c5c
- Child:
- 13:6cea4a9fcf7a
--- a/main.cpp Thu Oct 15 21:20:23 2015 +0000 +++ b/main.cpp Fri Oct 16 09:51:14 2015 +0000 @@ -18,45 +18,46 @@ int main() { - float cycle = 0.7;//define the speed of the motor + float cycle = 0.3;//define the speed of the motor bool motor1_on = 1;//set the on variable of motor 1 int motor1_dir=0;//set the direction of motor 1 bool motor2_on =1;//set the on variable of motor 2 int motor2_dir = 0;//set the direction of motor 1 - int n1=1; + int n1=1;//numeric condtions to determine if the speed needs to be increased int n2=1; + bool CW =1; + bool CCW = 0; while(1){ led3.write(0); led1.write(1); led2.write(1); - speed1.write(0);//set motor 1 speed to 0 - speed2.write(0);//set motor 2 speed to 0 pc.baud(115200); int diffa1 = button1.read();//read out the button 1 signal and calculate if it is being pressed or released wait(0.2);//from this we can determine if the rotation direction needs to be reversed. int diffb1 = button1.read(); int button_toggle1 = diffa1-diffb1; - if(button_toggle1 == 1 && motor1_dir == 1){ - motor1_dir = 0; + if(button_toggle1 == 1 && motor1_dir == CW){ + motor1_dir = CCW; } - else if(button_toggle1 == 1 && motor1_dir == 0){ - motor1_dir = 1; + else if(button_toggle1 == 1 && motor1_dir == CCW){ + motor1_dir = CW; } - if(button1.read()==false){ - while(n1 == 1){ - speed1.write(cycle); - direction1.write(1);//turn motor CCW or CW + if(button1.read() !=motor1_on){//check if button 1 is pressed led3.write(1); led1.write(0); + while(n1 == 1){ + speed1.write(cycle);//write speed only on first run through the loop + direction1.write(motor1_dir);//turn motor CCW or CW + n1=0; } } - else if (button1.read() == true){ - while(n1==0){ - speed1.write(0); + else if (button1.read() == motor1_on){//when button is released + while(n1==0){//check if the first run was done + speed1.write(0);//if so set speed to 0 and reset the run counter n1=1; } } @@ -65,29 +66,30 @@ wait(0.2);//from this we can determine if the rotation direction needs to be reversed. int diffb2 = button2.read(); int button_toggle2 = diffa2-diffb2; - if(button_toggle2 == 1 && motor2_dir == 1){ - motor2_dir = 0; + if(button_toggle2 == 1 && motor2_dir == CW){ + motor2_dir = CCW; } - else if(button_toggle2 == 1 && motor2_dir == 0){ - motor2_dir = 1; + else if(button_toggle2 == 1 && motor2_dir == CCW){ + motor2_dir = CW; } //motor CW = 0 //motor CCW = 1 - if(button1.read()==false){ - while(n2 == 1){ - speed2.write(cycle); - direction2.write(1);//turn motor CCW or CW + if(button2.read()!=motor2_on){//check if button 2 is pressed led3.write(1); led2.write(0); + while(n2 == 1){ + speed2.write(cycle);//write speed only on first run through the loop + direction2.write(1);//turn motor CCW or CW + n2=0; } } - else if (button1.read() == true){ - while(n2==0){ - speed1.write(0); + else if (button2.read() == motor2_on){//when button is released + while(n2==0){//check if the first run was done + speed2.write(0);//if so set speed to 0 and reset the run counter n2=1; } - } + } } } \ No newline at end of file