currently working on a EMG project that I found on Seeed’s blog. the code was provided through open source on the blog and on git hub.
// Grove - EMG Sensor demo code
// This demo will need a Grove - Led Bar to show the motion
// Grove - EMG Sensor connect to A0
// Grove - LED Bar connect to D8, D9
// note: it'll take about serval seconds to detect static analog value
// when you should hold your muscle static. You will see led bar from level 10 turn to
// level 0, it means static analog value get ok
#include <LED_Bar.h>
LED_Bar bar(9, 8);
int max_analog_dta = 300; // max analog data
int min_analog_dta = 100; // min analog data
int static_analog_dta = 0; // static analog data
// get analog value
int getAnalog(int pin)
{
long sum = 0;
for(int i=0; i<32; i++)
{
sum += analogRead(pin);
}
int dta = sum>>5;
max_analog_dta = dta>max_analog_dta ? dta : max_analog_dta; // if max data
min_analog_dta = min_analog_dta>dta ? dta : min_analog_dta; // if min data
return sum>>5;
}
void setup()
{
Serial.begin(115200);
long sum = 0;
for(int i=0; i<=10; i++)
{
for(int j=0; j<100; j++)
{
sum += getAnalog(A0);
delay(1);
}
bar.setLevel(10-i);
}
sum /= 1100;
static_analog_dta = sum;
Serial.print("static_analog_dta = ");
Serial.println(static_analog_dta);
}
int level = 5;
int level_buf = 5;
void loop()
{
int val = getAnalog(A0); // get Analog value
int level2;
if(val>static_analog_dta) // larger than static_analog_dta
{
level2 = 5 + map(val, static_analog_dta, max_analog_dta, 0, 5);
}
else
{
level2 = 5 - map(val, min_analog_dta, static_analog_dta, 0, 5);
}
// to smooth the change of led bar
if(level2 > level)
{
level++;
}
else if(level2 < level)
{
level--;
}
if(level != level_buf)
{
level_buf = level;
bar.setLevel(level);
}
delay(10);
}
the error codes I get are:
-‘LED_bar’ does not name a type (line 11)
-‘Bar.’ was not declared in this scope. (line 50 and 93)
I would greatly appreciate any help, I am not a coder and just want this device to play around and test muscle/nerve functioning.
I had to change <LED_Bar.h> to <Grove_LED_Bar.h>
Hey @salman and @Baozhu,
I had installed the grove LED bar library and the code will run the new #include <Grove_LED_Bar.h> however I still get error codes on telling the board that the LED is on D8 and D9 pins. I tried changing the “LED_Bar bar(9,8);” to “Grove_LED_Bar bar(9,8);” but I now get the error code 'no matching function to call".
I looked into the example sketches and changed some of the lines to match the sample and VIOLA! the program uploaded with no error codes… now just to see if the LED will display the volatage picked up from the EMG sensor! thank you so much
The LED bar works with mucsle activation, however not the range I would like. What would I need to change? the “static analog sum” or the max and min data levels?
// Grove - EMG Sensor demo code
// This demo will need a Grove - Led Bar to show the motion
// Grove - EMG Sensor connect to A0
// Grove - LED Bar connect to D8, D9
// note: it'll take about serval seconds to detect static analog value
// when you should hold your muscle static. You will see led bar from level 10 turn to
// level 0, it means static analog value get ok
int max_analog_dta = 300; // max analog data
int min_analog_dta = 100; // min analog data
int static_analog_dta = 0; // static analog data
#include <Grove_LED_Bar.h>
Grove_LED_Bar bar(9, 8, 0);
// get analog value
int getAnalog(int pin)
{
long sum = 0;
for(int i=0; i<32; i++)
{
sum += analogRead(pin);
}
int dta = sum>>5;
max_analog_dta = dta>max_analog_dta ? dta : max_analog_dta; // if max data
min_analog_dta = min_analog_dta>dta ? dta : min_analog_dta; // if min data
return sum>>5;
}
void setup()
{
//noting to initialize
bar.begin();
Serial.begin(115200);
long sum = 0;
for(int i=0; i<=10; i++)
{
for(int j=0; j<100; j++)
{
sum += getAnalog(A0);
delay(1);
}
bar.setLevel(10-i);
}
sum /= 300;
static_analog_dta = sum;
Serial.print("static_analog_dta = ");
Serial.println(static_analog_dta);
}
int level = 5;
int level_buf = 5;
void loop()
{
//Printing the EMG data
Serial.println(analogRead(5));
int val = getAnalog(A0); // get Analog value
int level2;
if(val>static_analog_dta) // larger than static_analog_dta
{
level2 = 5 + map(val, static_analog_dta, max_analog_dta, 0, 5);
}
else
{
level2 = 5 - map(val, min_analog_dta, static_analog_dta, 0, 5);
}
// to smooth the change of led bar
if(level2 > level)
{
level++;
}
else if(level2 < level)
{
level--;
}
if(level != level_buf)
{
level_buf = level;
bar.setLevel(level);
}
delay(50);
}
@salman
I am not currently getting an error code. I just want the LED to cover a wider range of signals. I want the LED to light up with the amount of muscle force input coming in through the analogue A0 port. Currently the LED bar will be half lit up without any muscle contraction, I wish that to be 0 bars showing at rest.