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

Dependencies:   MODSERIAL QEI Servo mbed

Committer:
huismaja
Date:
Fri Oct 07 17:40:28 2016 +0000
Revision:
2:b20570f160c6
Parent:
1:0d55a4bf2269
Child:
3:0a4bfcb3f339
Control of 1 DC-motor in a void-loop instead of in the main-loop

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 2:b20570f160c6 9 InterruptIn Switch_1(D8);
huismaja 2:b20570f160c6 10 int counter_extension=1;
huismaja 2:b20570f160c6 11
huismaja 2:b20570f160c6 12 MODSERIAL pc(USBTX, USBRX);
huismaja 0:6c8444d06e97 13
huismaja 2:b20570f160c6 14 void extension (){
huismaja 2:b20570f160c6 15 switch (counter_extension){
huismaja 2:b20570f160c6 16 case 1:
huismaja 2:b20570f160c6 17 digitalWrite(Direction_M1, 1); //The arm will get longer
huismaja 2:b20570f160c6 18 analogWrite(Speed_M1, 255); //The motor is turned on
huismaja 2:b20570f160c6 19 pc.printf("The arm will now get longer");
huismaja 2:b20570f160c6 20 wait(0.5f);
huismaja 2:b20570f160c6 21 break;
huismaja 2:b20570f160c6 22 case 2:
huismaja 2:b20570f160c6 23 digitalWrite(Direction_M1, 1); //The arm will get longer
huismaja 2:b20570f160c6 24 analogWrite(Speed_M1, 0); //The motor is turned off
huismaja 2:b20570f160c6 25 pc.printf("The arm will now stop");
huismaja 2:b20570f160c6 26 wait(0.5f);
huismaja 2:b20570f160c6 27 break;
huismaja 2:b20570f160c6 28 case 3:
huismaja 2:b20570f160c6 29 digitalWrite(Direction_M1, 0); //The arm will get shorter
huismaja 2:b20570f160c6 30 analogWrite(Speed_M1, 255); //The motor is turned off
huismaja 2:b20570f160c6 31 pc.printf("The arm will now get shorter");
huismaja 2:b20570f160c6 32 wait(0.5f);
huismaja 2:b20570f160c6 33 break;
huismaja 2:b20570f160c6 34 case 4:
huismaja 2:b20570f160c6 35 digitalWrite(Direction_M1, 0); //The arm will get shorter
huismaja 2:b20570f160c6 36 analogWrite(Speed_M1, 0); //The motor is turned off
huismaja 2:b20570f160c6 37 pc.printf("The arm will now stop");
huismaja 2:b20570f160c6 38 wait(0.5f);
huismaja 2:b20570f160c6 39 break;
huismaja 2:b20570f160c6 40 }
huismaja 2:b20570f160c6 41 }
huismaja 2:b20570f160c6 42
huismaja 2:b20570f160c6 43 void switch_counter_extension (){
huismaja 2:b20570f160c6 44 counter_extension++;
huismaja 2:b20570f160c6 45 if (counter_extension > 4){
huismaja 2:b20570f160c6 46 counter_extension=1;
huismaja 2:b20570f160c6 47 }
huismaja 2:b20570f160c6 48 extension();
huismaja 2:b20570f160c6 49 }
huismaja 0:6c8444d06e97 50
huismaja 0:6c8444d06e97 51 int main(){
huismaja 0:6c8444d06e97 52 pc.baud(115200);
huismaja 0:6c8444d06e97 53 pc.printf("RESET \n");
huismaja 1:0d55a4bf2269 54
huismaja 1:0d55a4bf2269 55 digitalWrite(Direction_M1, 1); //The arm will initially get longer
huismaja 1:0d55a4bf2269 56 analogWrite(Speed_M1, 0); //The motor is initially turned off
huismaja 1:0d55a4bf2269 57
huismaja 2:b20570f160c6 58 switch_1.rise(&switch_counter_extension);
huismaja 0:6c8444d06e97 59 }