ECG/EKG/EMG Shield, deepening on the demo

Started by youngz, November 25, 2016, 06:56:07 PM

Previous topic - Next topic

youngz

Hi,

in the demo file for Arduino (I am using Leonardo, you suggest to use another one?) I found this part:


//Read the 6 ADC inputs and store current values in Packet
  for(CurrentCh=0;CurrentCh<6;CurrentCh++){
    ADC_Value = analogRead(CurrentCh);
    TXBuf[((2*CurrentCh) + HEADERLEN)] = ((unsigned char)((ADC_Value & 0xFF00) >> 8)); // Write High Byte
    TXBuf[((2*CurrentCh) + HEADERLEN + 1)] = ((unsigned char)(ADC_Value & 0x00FF)); // Write Low Byte
}


I have a couple of questions:

  • The for loop for six times in order to reveal the possible six shields? If it is true, why the ADC_Value has a value that varies though I connect only one shield?
  • ADC_Value reveal the raw data of the shield. In what format the data are revealed? They are in DEC, or in Byte, or etc.?
  • The last two rows assign the ADC_Value to the "array" of char. What is the difference between High byte and Low Byte in physiological "context"? How it splits these data (I mean, how it works)?

Incidentally, I want to use the shield as EMG. Is there any difference on the parameters to set compared to ECG?

Thanks!

svan

You have asked some very good, very basic questions.  I will share my perspective which is quite simple and based on several projects displaying EKG's on a 2.8" lcd as well as various Android devices.  Others can undoubtedly provide a more detailed response.

1. I have no experience using a Leonardo with an Olimex EKG/EMG shield.  Most of my experience has been with an UNO.
2. I have no experience with EMG, only EKG.

I do not understand the need for a 6 channel 'for' loop in your example code; it must be for a special application.  For EKG's you would not need this.  An example of the code that I use is listed below.

It is natural for the Arduino analog sensor value to vary and will output a number between 0 and 1023.  The problem that you will encounter with serial output (sending your data somewhere to be displayed or stored) is that you can only transfer one byte at a time (0 to 255).  So how do you turn a big number into something which can be sent by single bytes?

There are a couple of options, but first you should have some concept of binary arithmetic.  Instead of counting with base 10, like humans do, computers use base 2, which gives numbers represented by zeroes and ones.  For this example let's assume that your data is represented by 16 ones and zeroes all lined up horizontally next to one another, and that we want to send the number 300, which is larger than a single byte since it goes beyond 255.  In binary, 300 would look like this: 00000001|00101100, read from right to left.  If we split the number into two parts at the vertical red line with 8 zeroes or ones on each side of the line, we would have a High Byte and a Low Byte.  The High Byte would be equal to 256 and the Low Byte would be equal to 44 (256+44=300).  After we have sent each byte to its destination, they are then re-assembled into the data value.  Splitting and re-assembly is done by a process called bit-shifting.  There are two types of bit-shifts, left (<<) and right(>>).  Shifting our number to the right 8 times would cleave it just past 256 and represent the High Byte. Following that transmission we would next send the Low Byte, which is what's left over.  In order to retrieve our number on the destination end, we would need to do the reverse, i.e., left shift eight times and add in the Low Byte.

A second technique that is easier in my opinion is to just divide the original number by 4 (>>2) and then send it.  This turns out to be convenient, particularly if the output is going to a 2.8" lcd with a height of 240 pixels.  The original analog output which is 0-1023 is reduced to 0-255 which fits nicely on the screen and may be sent by a serial connection.  The data is converted to a string of 3 characters 'framed' by an asterisk (could use commas or another character) to facilitate reconstruction on the other end.  Sometimes the numbers are split in the middle and it helps to have frame delimiters to make sure that we have groups of 3 characters each when the strings are converted back to numbers, prior to plotting.


/*
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
*/

// Olimex EKG/EMG Shield on Arduino UNO

const int analogInPin = A5;
char outStr[5];
unsigned long LoopTimer = 0;
int sensorValue = 0;
const int LoopTime5000 = 5000;

void setup(){
  Serial.begin(9600);
}

void loop(){
  if (micros() >= LoopTimer+LoopTime5000)
  {
   LoopTimer += LoopTime5000;
   sensorValue = analogRead(analogInPin);           
   sensorValue = sensorValue >> 2;
   sprintf(outStr,"%03d",sensorValue);
   Serial.print("*");
   Serial.write(outStr);
  }
}



Hopefully that will help give you a starting point.

svan

> I have no experience using a Leonardo with an Olimex EKG/EMG shield

Just used Olimex EKG/EMG shield with Leonardo and it seems to run ok.  Previously posted code ran without alteration.