Skip to content

Commit

Permalink
EMG Envelop sketch
Browse files Browse the repository at this point in the history
This updated EMG Envelop code now works for Arduino UNO/NANO, ESP32, and other development boards.
  • Loading branch information
lorforlinux committed Aug 22, 2021
1 parent 1a584bb commit 14cf18d
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions software/EMGEnvelop/EMGEnvelop.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// EMG Envelop - EXG Pill
// EMG Envelop - BioAmp EXG Pill
// https://github.com/upsidedownlabs/BioAmp-EXG-Pill

// Copyright (c) 2021 Upside Down Labs
// Upside Down Labs invests time and resources providing this open source code,
// please support Upside Down Labs and open-source hardware by purchasing
// products from Upside Down Labs!

// Copyright (c) 2021 Upside Down Labs - [email protected]

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,17 +25,17 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#define SampleRate 500
#define BaudRate 115200
#define inputPin A0
#define BufferSize 128
#define SAMPLE_RATE 500
#define BAUD_RATE 115200
#define INPUT_PIN A0
#define BUFFER_SIZE 128

int circularBuffer[BufferSize];
int index, sum;
int circular_buffer[BUFFER_SIZE];
int data_index, sum;

void setup() {
// Serial connection begin
Serial.begin(BaudRate);
Serial.begin(BAUD_RATE);
}

void loop() {
Expand All @@ -46,23 +51,23 @@ void loop() {

// Sample and get envelop
if(timer < 0) {
timer += 1000000 / SampleRate;
int sensorValue = analogRead(inputPin);
int EMGSignal = EMGFilter(sensorValue);
int envelop = getEnvelop(abs(EMGSignal));
Serial.print(EMGSignal);
timer += 1000000 / SAMPLE_RATE;
int sensor_value = analogRead(INPUT_PIN);
int signal = EMGFilter(sensor_value);
int envelop = getEnvelop(abs(signal));
Serial.print(signal);
Serial.print(",");
Serial.println(envelop);
}
}

// Envelop detection algorithm
int getEnvelop(int absEMG){
sum -= circularBuffer[index];
sum += absEMG;
circularBuffer[index] = absEMG;
index = (index + 1) % BufferSize;
return (sum/BufferSize) * 2;
int getEnvelop(int abs_emg){
sum -= circular_buffer[data_index];
sum += abs_emg;
circular_buffer[data_index] = abs_emg;
data_index = (data_index + 1) % BUFFER_SIZE;
return (sum/BUFFER_SIZE) * 2;
}

// Band-Pass Butterworth IIR digital filter, generated using filter_gen.py.
Expand Down

0 comments on commit 14cf18d

Please sign in to comment.