hall effect sensor and delay

Hi, i have a circuit with hall effect sensor and led. This is my sketch:

const int LedPin= 12; const int HallPin= 2;

void setup(){ pinMode(LedPin, OUTPUT); pinMode(HallPin, INPUT); }

void loop(){ int value= digitalRead(HallPin);

if (value == LOW)

{ digitalWrite(LedPin, HIGH); } else { digitalWrite(LedPin, LOW); } }

In this case when the magnet is closer to the hall sensor - the led is turn on. When you remove the magnet - the led turned off.

My question is could i delay turning on the led when the magnet is closer to the sensor. When i add for example delay(3000) - this turning on the led after 3s. but you need to keep the magnet near sensor for 3s. Is it posible to just pass the magnet for a moment near the sensor and after 3s. the led become turned on.


using a delay() stops everything for the duration so whats needed is a timing sequence. basically it's like checking a watch and then acting on it only when the time is up.

unsigned long startTime = 0;
const long interval = 1000;  // 1 second = 1000 millis
bool timeFlag = false;

void setup()

void loop()
  if (something you want to Time)
    timeFlag = true;
    startTime = millis(); // Begin timing sequence
    // do stuff here when timing sequence starts

  unsigned long currentTime = millis();
  if (timeFlag == true && currentTime - startTime >= interval) // End timing sequence
    timeFlag = false;
    // do stuff here when time is up

if you want the LED to also stay on only for a specific amount of time you will need a 2nd timer with a 2nd set of variables that is started when the first is finished.

Thank you very much! I must read more about millis().