Hi, I am new to Arduino and trying out a tutorial to create a piezo heart rate sensor. The tutorial works but I do not understand part of the code. The code contains a for loop that loops 64 times and contains an analogRead function. The comment says that the code takes 16 measurements. I am puzzled why 64 readings are needed to get 16 measurements from the piezo. Can anyone help?
This is the line that I am confused about:
for(int i=0; i<64; i++){ // Average over 16 measurements
Here is the full code:
//initiate variables here
int threshold = 9;
int oldvalue = 0;
int newvalue = 0;
int analogPin = 0;
unsigned long oldmillis = 0;
unsigned long newmillis = 0;
int cnt = 0;
int timings[16];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
oldvalue = newvalue;
newvalue = 0;
for(int i=0; i<64; i++){ // Average over 16 measurements
newvalue += analogRead(analogPin);
}
newvalue = newvalue/64;
Serial.println(newvalue, DEC);
// find triggering edge
if(oldvalue<threshold && newvalue>=threshold)
{
oldmillis = newmillis;
newmillis = millis();
// fill in the current time difference in ringbuffer
timings[cnt%16]= (int)(newmillis-oldmillis);
int totalmillis = 0;
// calculate average of the last 16 time differences
for(int i=0;i<16;i++)
{
totalmillis += timings*;*
- }*
- // calculate heart rate*
- //int heartrate = 60000/(totalmillis/16);*
- //Serial.println("Your heartrate is:");*
- //Serial.println(heartrate,DEC);*
- cnt++;*
- }*
- delay(5);*
}