Timing problem

Hello, I’m trying to display the time difference between the first time the if statement is used and when you get out of the last if statement the results are always 300 on the time difference

if (deriv>=500 )
{delay(700);
if (count==1)
{
Serial.println(“in”);
Serial.println(count);
unsigned long previousMillis=micros();
}
if (2<=count && count<5)
{
Serial.println(“pulse”);
Serial.println(count);
}
if (count==5)
{
unsigned long currentMillis=micros();
unsigned long top=(currentMillis-previousMillis);
unsigned long heartrate=((top*(currentMillis-previousMillis)/1000000));
Serial.println(currentMillis);
Serial.println(previousMillis);
Serial.println(top);
Serial.println(heartrate);
count=0;
}
count++;

}
all non declared variables here are global variables

if (count==1)
   {
Serial.println("in");
   Serial.println(count);
    unsigned long previousMillis=micros();
   }

That previousMillis variable goes out of scope immediately.

     unsigned long currentMillis=micros();
     unsigned long top=(currentMillis-previousMillis);
     unsigned long heartrate=((top*(currentMillis-previousMillis)/1000000));
     Serial.println(currentMillis);
     Serial.println(previousMillis);

These uses of previousMillis refer to some other variable, that you set somewhere else.

The use of millis in the name of variables is a poor idea. The variable name is supposed to tell you something about the value that it contains. You can't even make the claim that the name suggests that the variable holds the number of milliseconds that the Arduino had been running at some point, because it does NOT.

Thank you for your responce I have a new code now and I belive its better at detecting and keeping the count where it should be, mind telling me if something can be inproved ?

unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) <= intervals)
{
//Serial.println(“in”);
if( deriv>=600)
{
delay(700);
Serial.println(count);
count++;

}
return;
}
int heartrate=count*6;
Serial.print("heartrate: ");
Serial.println(heartrate);
previousMillis=millis();
count=0;
}

DSDF97:
mind telling me if something can be inproved ?

Well, your posting style certainly could use a lot of improvement.

First, there's a "sticky" post at the top of every forum page: Read this before posting a programming question ...
I know that's a pretty cryptic title. But, you should have, you know, read it BEFORE posting your questions. Read it now and pay particular attention to Item #6 that tells you how to properly post your code with Code Tags.

Second, you should, whenever possible, post Complete Code. That makes it a lot easier for someone to help you by copy / pasting it into the Arduino IDE and compiling it. When I help others with their code, I prefer to make sure my solution compiles and works before I post it back to the forum. So, your posted code without setup(), loop(), and variable definitions makes that a lot harder and more work for me. So, I'm not going to help someone who makes more work for me. YMMV.

You know those global variable declarations that look meaningless? They are not. We cannot comment on your code without seeing all of it.

In code tags, please.