Script for controlling 2 DC-motors and a gripper-servo using buttons
Dependencies: MODSERIAL QEI Servo mbed
Diff: main.cpp
- Revision:
- 3:0a4bfcb3f339
- Parent:
- 2:b20570f160c6
- Child:
- 4:84bd5ead83f9
diff -r b20570f160c6 -r 0a4bfcb3f339 main.cpp --- a/main.cpp Fri Oct 07 17:40:28 2016 +0000 +++ b/main.cpp Fri Oct 07 17:48:34 2016 +0000 @@ -7,7 +7,11 @@ //DigitalOut Direction_M2(D7); InterruptIn Switch_1(D8); +InterruptIn Switch_2(D9); +InterruptIn Switch_3(D10); int counter_extension=1; +int counter_rotation_left=1; +int counter_rotation_right=1; MODSERIAL pc(USBTX, USBRX); @@ -48,12 +52,66 @@ extension(); } +void rotation_left (){ + switch (counter_rotation_left){ + case 1: + digitalWrite(Direction_M1, 1); //The arm will rotate to the left + analogWrite(Speed_M1, 255); //The motor is turned on + pc.printf("The arm will now rotate to the left"); + wait(0.5f); + break; + case 2: + digitalWrite(Direction_M1, 1); //The arm will rotate to the left + analogWrite(Speed_M1, 0); //The motor is turned off + pc.printf("The arm will now stop"); + wait(0.5f); + break; + } +} + +void switch_counter_rotation_left (){ + counter_rotation_left++; + if (counter_rotation_left > 2){ + counter_rotation_left=1; + } + rotation_left(); +} + +void rotation_right (){ + switch (counter_rotation_right){ + case 1: + digitalWrite(Direction_M1, 0); //The arm will rotate to the right + analogWrite(Speed_M1, 255); //The motor is turned on + pc.printf("The arm will now rotate to the right"); + wait(0.5f); + break; + case 2: + digitalWrite(Direction_M1, 0); //The arm will rotate to the right + analogWrite(Speed_M1, 0); //The motor is turned off + pc.printf("The arm will now stop"); + wait(0.5f); + break; + } +} + +void switch_counter_rotation_right (){ + counter_rotation_right++; + if (counter_rotation_right> 2){ + counter_rotation_right=1; + } + rotation_right(); +} + int main(){ pc.baud(115200); pc.printf("RESET \n"); digitalWrite(Direction_M1, 1); //The arm will initially get longer analogWrite(Speed_M1, 0); //The motor is initially turned off + digitalWrite(Direction_M1, 1); //The arm will initially get longer + analogWrite(Speed_M1, 0); //The motor is initially turned off switch_1.rise(&switch_counter_extension); + switch_2.rise(&switch_counter_rotation_left); + switch_3.rise(&switch_counter_rotation_right); } \ No newline at end of file