Script for controlling 2 DC-motors and a gripper-servo using buttons

Dependencies:   MODSERIAL QEI Servo mbed

Revision:
1:0d55a4bf2269
Parent:
0:6c8444d06e97
Child:
2:b20570f160c6
--- a/main.cpp	Fri Oct 07 16:55:36 2016 +0000
+++ b/main.cpp	Fri Oct 07 17:30:26 2016 +0000
@@ -1,19 +1,58 @@
 #include "mbed.h"
 #include "MODSERIAL.h'
 
-DigitalIn Direcion_M1 = 4;
-DigitalIn Speed_M1 = 5;
-DigitalIn Speed_M2 = 6;                  
-DigitalIn Direction_M2 = 7;  
+DigitalOut Direcion_M1(D4);
+DigitalOut Speed_M1(D5);
+//DigitalOut Speed_M2(D6);                  
+//DigitalOut Direction_M2(D7);
+
+DigitalIn Switch_1(D8);
+int counter=1;
 
 MODSERIAL pc(USBTX, USBRX);                   
 
 int main(){
     pc.baud(115200);
     pc.printf("RESET \n");
-    digitalWrite(Direction_M1, 1);   //Direction Control of Motor 1 (1 or 0)
-    digitalWrite(Direction_M2, 1);   //Direction Control of Motor 1 (1 or 0)   
-    analogWrite(Speed_M1, 120);   //Speed Control of Motor 1 (0<speed<255)
-    analogWrite(Speed_M2, 200);   //Speed Control of Motor 2 (0<speed<255)
-    wait(2.0f); 
+    
+    digitalWrite(Direction_M1, 1);   //The arm will initially get longer  
+    analogWrite(Speed_M1, 0);        //The motor is initially turned off
+    
+    while (true){
+        if (switch_1 == 0){
+            switch (counter){
+                case 1:
+                    counter++;
+                    digitalWrite(Direction_M1, 1);   //The arm will get longer  
+                    analogWrite(Speed_M1, 255);      //The motor is turned on
+                    pc.printf("The arm will now get longer");
+                    wait(0.5f);
+                    break;
+                case 2:
+                    counter++;
+                    digitalWrite(Direction_M1, 1);   //The arm will get longer  
+                    analogWrite(Speed_M1, 0);        //The motor is turned off
+                    pc.printf("The arm will now stop");
+                    wait(0.5f);
+                    break;
+                case 3:
+                    counter++;
+                    digitalWrite(Direction_M1, 0);   //The arm will get shorter  
+                    analogWrite(Speed_M1, 255);      //The motor is turned off
+                    pc.printf("The arm will now get shorter");
+                    wait(0.5f);
+                    break;
+                case 4:
+                    counter++;
+                    digitalWrite(Direction_M1, 0);   //The arm will get shorter 
+                    analogWrite(Speed_M1, 0);        //The motor is turned off
+                    pc.printf("The arm will now stop");
+                    wait(0.5f);
+                    break;
+                case 5:
+                    counter = 1;
+                    break;
+            }     
+        } 
+    }
 }
\ No newline at end of file