Troubleshooting Grove EMG sensor and LED bar

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>

@salman This library is definitely available, can you help test it?

Sure @Baozhu, I tried to compile the lib and did find any compilation error.

Hi, @brad.d. Did you install the library from the GitHub? If not first install the lib,

  1. Step 1. Download the lib - Click here
  2. Step 2. Refer How to install library to install library for Arduino.
  3. Step 3. Restart the Arduino IDE. Open “Level” example via the path: File → Examples → Grove LED Bar → Level.

Then check the LED Bar is working fine and all, then try to merge the example code to your existing working EMG sensor code base.

If you found any error while doing this, please let us know. :slightly_smiling_face::+1:

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".

thank you for the replies btw

@brad.d Did you tried to compile the example sketches and did it work without any error?

example sketches?? how do I get that

After importing the library, you can see the example sketch at the : File → Examples → Grove LED Bar → Level .

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?

@salman
would you know how to fix this issue?

Please share your existing code.

// 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 wish to get a full range on the led bar

@brad.d also share the error getting right now? I have the LED bar not the EMG Sensor, that’s why?

@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.