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

Dependencies:   MODSERIAL QEI Servo mbed

main.cpp

Committer:
huismaja
Date:
2016-10-07
Revision:
3:0a4bfcb3f339
Parent:
2:b20570f160c6
Child:
4:84bd5ead83f9

File content as of revision 3:0a4bfcb3f339:

#include "mbed.h"
#include "MODSERIAL.h'

DigitalOut Direcion_M1(D4);
DigitalOut Speed_M1(D5);
//DigitalOut Speed_M2(D6);                  
//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);  

void extension (){
    switch (counter_extension){
        case 1:
            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:
            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:
            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:
            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;
    }     
}                 

void switch_counter_extension (){
    counter_extension++;
    if (counter_extension > 4){
        counter_extension=1;
    }
    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);
}