-5

I am trying to make an Arduino heart beat counter . I looked for some code online and found this code on arduino.cc forum but I couldn't understand working of the given code .. specifically these few lines of code: The full code is given below:

int sensorValue = 0;
int sensorValue = 0;  // variable to store the value coming from the sensor
int count = 0;
unsigned long time1 = 0;  // store the initial time
unsigned long time2;    // store the current time 
void setup() {
    // declare the ledPin as an OUTPUT:
    pinMode(sensorPin, INPUT);
    Serial.begin(9600);
}

void loop() {
    // read the value from the sensor:
    if (count == 0)
    {
        time1 = millis();
    }
    time2 = millis();
    sensorValue = analogRead(sensorPin);
    if (sensorValue > 156)
    {
        increment();
    }
    if (time2 >= time1 + 10000)
    {
        counter();
    }

}


void increment()
{
    count++;
    while (sensorValue > 156)
    {
        Serial.print("Sensor value is greater than .75V\n");
    }
}
void counter()
{
    count = count * 6;
    Serial.print("Heart beat is ");
    Serial.print(count);
    Serial.print(" per min\n");
    time1 = 0;
    time2 = 0;
    count = 0;
}

Lines of code I'm not able to understand :

if (count == 0)
{
    time1 = millis();
}
time2 = millis();
sensorValue = analogRead(sensorPin);
if (sensorValue > 156)
{
    increment();
}
if (time2 >= time1 + 10000)
{
    counter();
}

PS: the given code is in void loop(). in the beginning of the loop.

if u are not able to understand the code i'm leaving a link to the page:this is the link

i would be highly obliged if anyone could really help me soon. Thank u in advance :-)

  • 2
    Just copy paste the code. Don't post screenshot of it. – gre_gor Mar 31 '17 at 19:54
  • What exactly don't you udnerstand in this code. Note that this code is using functions (increment(), counter()) that you did not post, hence it will be difficult to answer. As already commented, don't post pictures of code as this would make it more difficult to copy paste into an answer. Finally, provide the exact link where you found this source code. – jfpoilpret Apr 1 '17 at 8:04
  • jfpoilpret i made some revisions in the code now can you help me – user7186002 Apr 1 '17 at 9:05
1

I've reformatted you code to make it easier to read.

Looking just at the lines you don't understand.

Line 1 stores the start time of this loop in the variable time1 and stores the current time in the variable time2 these are going to be almost identical until the first heart beat is seen (which will increment count).

Line 6 sensorValue = analogRead(sensorPin); reads the value from the sensor, it the reading is greater than some predetermined threshold (156) then it increment() the counter and waits until the value goes below 156.

Line 11 checks to see if 10 seconds (10,000 mS) have elapsed since the loop started. If they have then you get your reading via the function counter()

Does that make sense?

It would be a lot easier to understand if the original author had put comments in, but most people writing examples assume there code is so easy to understand they don't need to, don't fall into that trap when you become a great programmer! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.