timing the digital signals

I am actually trying to make my own Tachometer will hall sensor. I have attached my code below. It might not be a great coding but still i wish to know why my elapsed time is not coming a finite value. every time the sensor detects the magnet it tells the time but not from zero but from very first time the programme started to run. Though is saw a few tutorials in advance to this and reffered to some of my programming schoolmates none were able to help. So i wish for guidance.

Thanking you in advance

qfiuhqfuih.ino (740 Bytes)

Please post your code, in code tags.

OP's code

int state=0;
unsigned long StartTime =0;
unsigned long EndTime =0;

void setup() {
  pinMode(hallsensor, INPUT);
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);// put your setup code here, to run once:


void loop() {
  if (state==LOW){
    unsigned long EndTime = millis();
    Serial.print ("no signal");
    Serial.println ("\n");
    digitalWrite (ledpin, LOW);
  else if(state==HIGH){
        Serial.println (" signal");
        Serial.print ("\n");
    digitalWrite (ledpin, HIGH);

    // put your main code here, to run repeatedly:
  //unsigned long elapsedTime  = EndTime - StartTime;


You need to change this line

unsigned long EndTime = millis();


EndTime = millis();

When you put unsigned long at the start of the line it creates a new temporary variable every time that loop() repeats.