
Arduino
The code
It looks complicated, but it’s not that bad.
The incoming dataflow exists out of values that represent the amount of muscle activity. The problem with EMG is that it’s different for each individual person so everyone has different threshold values, so it will need to be calibrated.
If a person exceeds this threshold, the servo starts moving. If he or she gets below the threshold it stops.
And if at any giving moment the muscle is really active (maximal flexion) the servo goes back to the ‘home’ position.
This all happens if the FSR is below his own threshold value. So if the finger hits or grips an object it stops moving.
So in short, the EMG sensor moves the finger if the targeted muscle is active. It stops if the muscle is relaxed. And if the person grips an object it will stop pinching at a certain pressure.
#include <Servo.h>
Servo myservo;
int FSR_v = A0;
int EMG_v = A1;
int fsrReading;
int EMGReading;
int pos = 0;
int maxpos = 160;
int pos1 = 0;
int emghoog = 500;
int emglaag = 70;
int direction = 0; //direction state, +1,0or -1
void setup(void) {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
fsrReading = analogRead(FSR_v);
EMGReading = analogRead(EMG_v);
// Serial.print("FSR_v = ");
// Serial.println(fsrReading);
Serial.print("EMG_v = ");
Serial.println(EMGReading);
//Serial.println("pos= ");
// Serial.println(pos);
if(EMGReading >= emghoog){
myservo.write (pos1);
pos=0;
delay(1);
}
if (fsrReading < 50 & EMGReading > emglaag & EMGReading < emghoog){
if (pos == maxpos){
if (EMGReading >= emghoog){
myservo.write (pos1);
pos=0;
delay(1);
}
else{
myservo.write (maxpos) ;
delay(1);
}
}
else {
direction == 1;
pos ++ ; // 1 graden per keer
myservo.write (pos) ;// go forward
}
}
else{
direction = 0; // stopped
}
}
