Hello, I'm relatively new to Arduino. I'm using a DFrobot Gravity EMG sensor to move a 9g servo. I used the following code:
#include <Servo.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "EMGFilters.h"
#define TIMING_DEBUG 1
#define SensorInputPin A0 // input pin number
Servo myservo;
EMGFilters myFilter;
// discrete filters must works with fixed sample frequence
// our emg filter only support "SAMPLE_FREQ_500HZ" or "SAMPLE_FREQ_1000HZ"
// other sampleRate inputs will bypass all the EMG_FILTER
int sampleRate = SAMPLE_FREQ_1000HZ;
// For countries where power transmission is at 50 Hz
// For countries where power transmission is at 60 Hz, need to change to
// "NOTCH_FREQ_60HZ"
// our emg filter only support 50Hz and 60Hz input
// other inputs will bypass all the EMG_FILTER
int humFreq = NOTCH_FREQ_50HZ;
// Calibration:
// put on the sensors, and release your muscles;
// wait a few seconds, and select the max value as the throhold;
// any value under throhold will be set to zero
static int Throhold = 100;
unsigned long timeStamp;
unsigned long timeBudget;
void setup() {
/* add setup code here */
myFilter.init(sampleRate, humFreq, true, true, true);
// open serial
Serial.begin(115200);
// setup for time cost measure
// using micros()
timeBudget = 1e6 / sampleRate;
// micros will overflow and auto return to zero every 70 minutes
// Attach the servo on pin 9 to the servo object
myservo.attach(10);
}
void loop() {
/* add main program code here /
// In order to make sure the ADC sample frequence on arduino,
// the time cost should be measured each loop
/------------start here-------------------*/
timeStamp = micros();
int Value = analogRead(SensorInputPin);
// filter processing
int DataAfterFilter = myFilter.update(Value);
int envlope = sq(DataAfterFilter);
// any value under throhold will be set to zero
envlope = (envlope > Throhold) ? envlope : 0;
timeStamp = micros() - timeStamp;
if (TIMING_DEBUG) {
// Serial.print("Read Data: "); Serial.println(Value);
// Serial.print("Filtered Data: ");Serial.println(DataAfterFilter);
Serial.print("Squared Data: ");
Serial.println(envlope);
// Serial.print("Filters cost time: "); Serial.println(timeStamp);
// the filter cost average around 520 us
}
/*------------end here---------------------*/
// if less than timeBudget, then you still have (timeBudget - timeStamp) to
// do your work
delayMicroseconds(500);
// if more than timeBudget, the sample rate need to reduce to
// SAMPLE_FREQ_500HZ
// Trigger the servo motor if the filtered value is greater than the threshold
if (envlope > Throhold) {
// Set the servo motor to the desired position
myservo.write(180);
} else {
// Set the servo motor to the opposite position
myservo.write(0);
}
}
However, when I flex my hands, the servo doesn't move. I check the serial plotter and do see that it's getting a signal. If it helps, the EMG is connected to 3.3v and the servo is connected to 5v.