#include <Servo.h>
//Define the pin numbers
constintJoystickH=0;constintJoystickV=1;constintPotmeterH=4;constintPotmeterV=5;constintServoHPin=11;constintServoVPin=9;constintServoGripperPin=10;constintEMG=3;//Define the constants for the inputs and outputs
floatSpeedX;floatSpeedY;floatVoltagePotH;floatVoltagePotV;floatRotationsPotH;floatRotationsPotV;intEMG_Value;intJoystickX;intJoystickY;//Define resting positions of the servos
constintRestPosX=65;constintRestPosY=88;//Define the threshold for the EMG sensor
constintEMG_threshold=250;//Define servo objects
ServoServoH;ServoServoV;ServoServoGripper;voidsetup(){//This code is run only once, in the setup phase
Serial.begin(9600);//Set the pin mode for the analog inputs
pinMode(JoystickH,INPUT);pinMode(JoystickV,INPUT);pinMode(PotmeterV,INPUT);pinMode(PotmeterH,INPUT);//Add the pins to the right servos
ServoH.attach(ServoHPin);ServoV.attach(ServoVPin);ServoGripper.attach(ServoGripperPin);}voidloop(){//This code is executed constantly
//Read the joystick values
JoystickX=analogRead(JoystickH);JoystickY=analogRead(JoystickV);//Read the potentiometer values
VoltagePotH=analogRead(PotmeterH);VoltagePotV=analogRead(PotmeterV);//Convert the potentiometer readings to the amount of rotations
RotationsPotH=VoltagePotH*5/1024*2;RotationsPotV=VoltagePotV*5/1024*2;////////////////////////////////////
// Code for the first direction //
////////////////////////////////////
if(RotationsPotH>4&&RotationsPotH<6)//Rotations are in the safe range
{if(JoystickX<300){//Convert to a speed linear with the joystick value
SpeedX=(512-JoystickX)/512*180;ServoH.write(SpeedX);//Send to servo
}elseif(JoystickX>750){//Convert to a speed linear with the joystick value
SpeedX=(JoystickX-512)/512*180;ServoH.write(-1*SpeedX);//Send to servo
}else{//Servo should not move
ServoH.write(RestPosX);}}elseif(RotationsPotH<4)//No longer in the safe zone, disable further movement in this direction
{if(JoystickX>750){//It can still move in this direction
SpeedX=(JoystickX-512)/512*180;ServoH.write(-1*SpeedX);}else{//No more movement permitted in this direction
ServoH.write(RestPosX);}}elseif(rotationsPotH>6)//No longer in the safe zone, disable further movement in this direction
{if(JoystickX<300){//It can still move in this direction
SpeedX=(512-JoystickX)/512*180;ServoH.write(SpeedX);}else{//No more movement permitted in this direction
ServoH.write(RestPosX);}}/////////////////////////////////////
// Code for the second direction //
/////////////////////////////////////
if(RotationsPotV>4&&RotationsPotV<6)//Rotations are in the safe range
{if(JoystickY<300){//Move at full speed
ServoV.write(180);//Send to servo
}elseif(JoystickY>750){//Move at full speed
ServoV.write(-180);//Send to servo
}else{//Servo should not move
ServoV.write(RestPosY);}}elseif(RotationsPotV<4)//No longer in the safe zone, disable further movement in this direction
{if(JoystickY<300){//It can still move in this direction
ServoV.write(180);}else{//No more movement permitted in this direction
ServoV.write(RestPosY);}}elseif(RotationsPotV>6)//No longer in the safe zone, disable further movement in this direction
{if(JoystickY>750){//It can still move in this direction
ServoV.write(-180);}else{//No more movement permitted in this direction
ServoV.write(RestPosY);}}///////////////////////////////
// Code for the EMG sensor //
///////////////////////////////
//Read EMG value
EMG_Value=analogRead(EMG);if(EMG_Value<EMG_threshold){//Muscle relaxed, open gripper
ServoGripper.write(0);}else{//Muscle tensioned, close gripper
ServoGripper.write(180);}//Wait 100 ms before starting a new loop
delay(100);}