control for robotic arm that can play chess using a granular gripper

Dependencies:   Encoder mbed HIDScope Servo MODSERIAL

Fork of chessRobot by a steenbeek

buttons.cpp

Committer:
annesteenbeek
Date:
2015-10-08
Revision:
39:41635b1b11de
Parent:
38:2c18429ad352
Child:
40:0d88aa25a57d

File content as of revision 39:41635b1b11de:

#include "buttons.h"
#include "mbed.h"
#include "config.h"
#include "actuators.h"
// functions for reading all the buttons and switches 
AnalogIn pot2(pot2Pin);
AnalogIn pot1(pot1Pin);

// Led states: 	
// 	0 leds: motors disabled
//	redLed: 	control X speed
//	greenLed:	control Y speed
//	blueLed:	control Servo pos
DigitalOut redLed(LED_RED);
DigitalOut greenLed(LED_GREEN);
DigitalOut blueLed(LED_BLUE);

AnalogIn button1(button1Pin);
AnalogIn button2(button2Pin);
float prevState1 = 1;
float prevState2 = 1;
int actuatorState = 0;

float pot1Val = 0;
float pot2Val = 0;


void checkSwitches(){

	// 3 states: X control, Y control en Servo control
	//	 button 1 to enable/disable actuators
	// 	 button 2 to switch between states

	if(button1.read() == 0 && prevState1 != 0){
		motorsEnable = !motorsEnable;
	}
	if(button2.read() == 0 && prevState2 != 0){
		actuatorState++;
		if(actuatorState==3){
			actuatorState = 0;
		}
	}
	prevState1 = button1.read();
	prevState2 = button2.read();

    pot1Val = pot1.read();
    pot2Val = pot2.read();

    if(motorsEnable){
    	switch (actuatorState){
        	case 0: 				// potmeters control X speed
        		redLed.write(0); greenLed.write(1); blueLed.write(1);
        		motorSetSpeed1 = 300*(pot2.read()-pot1.read());
        		break;
        	case 1:         		// potmeters control Y speed
        		redLed.write(1); greenLed.write(0); blueLed.write(1);
        		motorSetSpeed2 = 300*(pot2.read()-pot1.read());
        		break;
        	case 2:         		// potmeters control Servo pos
        		redLed.write(1); greenLed.write(1); blueLed.write(0);
        		servoPos = pot2.read();
        		break;
        }
    }else{
    	redLed.write(1); greenLed.write(1); blueLed.write(1);
    }
   }