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

Dependencies:   MODSERIAL QEI Servo mbed

Committer:
huismaja
Date:
Fri Oct 07 17:30:26 2016 +0000
Revision:
1:0d55a4bf2269
Parent:
0:6c8444d06e97
Child:
2:b20570f160c6
Control of 1 DC-motor for the extension of the robotarm using a switch statement

Who changed what in which revision?

UserRevisionLine numberNew contents of line
huismaja 0:6c8444d06e97 1 #include "mbed.h"
huismaja 0:6c8444d06e97 2 #include "MODSERIAL.h'
huismaja 0:6c8444d06e97 3
huismaja 1:0d55a4bf2269 4 DigitalOut Direcion_M1(D4);
huismaja 1:0d55a4bf2269 5 DigitalOut Speed_M1(D5);
huismaja 1:0d55a4bf2269 6 //DigitalOut Speed_M2(D6);
huismaja 1:0d55a4bf2269 7 //DigitalOut Direction_M2(D7);
huismaja 1:0d55a4bf2269 8
huismaja 1:0d55a4bf2269 9 DigitalIn Switch_1(D8);
huismaja 1:0d55a4bf2269 10 int counter=1;
huismaja 0:6c8444d06e97 11
huismaja 0:6c8444d06e97 12 MODSERIAL pc(USBTX, USBRX);
huismaja 0:6c8444d06e97 13
huismaja 0:6c8444d06e97 14 int main(){
huismaja 0:6c8444d06e97 15 pc.baud(115200);
huismaja 0:6c8444d06e97 16 pc.printf("RESET \n");
huismaja 1:0d55a4bf2269 17
huismaja 1:0d55a4bf2269 18 digitalWrite(Direction_M1, 1); //The arm will initially get longer
huismaja 1:0d55a4bf2269 19 analogWrite(Speed_M1, 0); //The motor is initially turned off
huismaja 1:0d55a4bf2269 20
huismaja 1:0d55a4bf2269 21 while (true){
huismaja 1:0d55a4bf2269 22 if (switch_1 == 0){
huismaja 1:0d55a4bf2269 23 switch (counter){
huismaja 1:0d55a4bf2269 24 case 1:
huismaja 1:0d55a4bf2269 25 counter++;
huismaja 1:0d55a4bf2269 26 digitalWrite(Direction_M1, 1); //The arm will get longer
huismaja 1:0d55a4bf2269 27 analogWrite(Speed_M1, 255); //The motor is turned on
huismaja 1:0d55a4bf2269 28 pc.printf("The arm will now get longer");
huismaja 1:0d55a4bf2269 29 wait(0.5f);
huismaja 1:0d55a4bf2269 30 break;
huismaja 1:0d55a4bf2269 31 case 2:
huismaja 1:0d55a4bf2269 32 counter++;
huismaja 1:0d55a4bf2269 33 digitalWrite(Direction_M1, 1); //The arm will get longer
huismaja 1:0d55a4bf2269 34 analogWrite(Speed_M1, 0); //The motor is turned off
huismaja 1:0d55a4bf2269 35 pc.printf("The arm will now stop");
huismaja 1:0d55a4bf2269 36 wait(0.5f);
huismaja 1:0d55a4bf2269 37 break;
huismaja 1:0d55a4bf2269 38 case 3:
huismaja 1:0d55a4bf2269 39 counter++;
huismaja 1:0d55a4bf2269 40 digitalWrite(Direction_M1, 0); //The arm will get shorter
huismaja 1:0d55a4bf2269 41 analogWrite(Speed_M1, 255); //The motor is turned off
huismaja 1:0d55a4bf2269 42 pc.printf("The arm will now get shorter");
huismaja 1:0d55a4bf2269 43 wait(0.5f);
huismaja 1:0d55a4bf2269 44 break;
huismaja 1:0d55a4bf2269 45 case 4:
huismaja 1:0d55a4bf2269 46 counter++;
huismaja 1:0d55a4bf2269 47 digitalWrite(Direction_M1, 0); //The arm will get shorter
huismaja 1:0d55a4bf2269 48 analogWrite(Speed_M1, 0); //The motor is turned off
huismaja 1:0d55a4bf2269 49 pc.printf("The arm will now stop");
huismaja 1:0d55a4bf2269 50 wait(0.5f);
huismaja 1:0d55a4bf2269 51 break;
huismaja 1:0d55a4bf2269 52 case 5:
huismaja 1:0d55a4bf2269 53 counter = 1;
huismaja 1:0d55a4bf2269 54 break;
huismaja 1:0d55a4bf2269 55 }
huismaja 1:0d55a4bf2269 56 }
huismaja 1:0d55a4bf2269 57 }
huismaja 0:6c8444d06e97 58 }