Code

Code

 

First We need to include servo library, then We have to define all needed inputs and variables. PrusaHand consists of 3 servos, let’s define them by Servo servoX command. PrusaHand reacts to two inputs – press of the button or output from Muscle Sensor. Button is on pin 5, Muscle sensor on pin 6. For both inputs We create variable to store the state of it. I wanted the PrusaHand to react to an impuls, you press the button once or send an impuls by flexing a muscle, PrusaHand closes, you press the button or flex a muscle the second time, PrusaHand opens. I have found the example code for such a behaviour on arduino forum (http://forum.arduino.cc/index.php?topic=158327.0, user Zoomkat).

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
int button = 5;
int pressed = 0;
int emg = 6;
int flexed = 0;
boolean toggle = true;

Now We need to attach our servos to the corresponding pins. Let’s do it with servoX.attach(pin#). Inputs have to be defined. For a button let’s use an built-in pull up resistor for more reliable function. Button is HIGH at idle, when pressed the signals drops LOW. Signal from the Muscle sensor goes HIGH when the muscle is flexed and it is LOW when the muscle is relaxed. We can also reset the servos position after switching PrusaHand ON with command servoX.write(angle).

void setup() {
// put your setup code here, to run once:

servo1.attach(10);
servo2.attach(9);
servo3.attach(11);

pinMode(button, INPUT_PULLUP);
pinMode(flexed, INPUT);

digitalWrite(button, HIGH);

servo1.write(90);
servo2.write(0);
servo3.write(90);
}

In the void loop We are reading the state of the button and the Muscle Sensor. When the button goes LOW OR the signal from the Muscle Sensor goes HIGH arduino will read the value of boolean variable toggle. If toggle is TRUE the PrusaHand will go the the open position and the toggle variable gets value FALSE. When button goes LOW or signal from Muscle Sensor goes HIGH again, the PrusaHand will go to the closed position and the toggle variable will go TRUE again, so PrusaHand is ready to open. There is 0,5s delay for the debounce.

void loop() {
// put your main code here, to run repeatedly:

pressed = digitalRead(button);
flexed = digitalRead(emg);
if (pressed == LOW || flexed == HIGH)
{
if (toggle)
{
servo1.write(90);
servo2.write(0);
servo3.write(90);
toggle = !toggle;
}
else
{
servo1.write(0);
servo2.write(45);
servo3.write(0);
toggle = !toggle;
}
}
delay(500);
}

Now We are ready to upload the sketch to the arduino!