Noobie help please run on delay

Hello all,

Newbie here! I am working on a project where when the analogue value is at a preset value the led pin goes high and remains high all the time the analogue is at the preset, should the analogue input drop below the preset then there will be a 'run on delay' for 20 second keeping the led pin high until the 20 seconds has passed then going low.

I played around using delay function however this halts the programme and gives a delay in start up not the instant state change and only the delay in switching low.

so I have used Millis function however the timing seems a bit random.
any help would be greatly received, sorry my code is ot very tidy!

thank you all!!

code below

[int sensorPin = A0; // select the input pin for the current coil input
int ledPin = 13; // select the pin for the LED for output
int senseCoil = 0; // variable to store the value coming from current coil
const unsigned long runonDelay = 20000; // run on delay
unsigned long prevoiusTime = 0; // set prevoius time

void setup() {
pinMode(ledPin, OUTPUT); // set pin as output

}

void loop() {
// read the value from the sensor:
senseCoil = analogRead(sensorPin);
if (senseCoil >= 500) {
digitalWrite(ledPin, HIGH);

}
else {
unsigned long currentTime = millis();
if (currentTime - prevoiusTime >= runonDelay) {
digitalWrite(ledPin, LOW);
prevoiusTime = currentTime;
}

}
}]

You have to keep track of what state you are in so you are only doing the timing after you go from HIGH to LOW

int sensorPin = A0;    // select the input pin for the current coil input
int ledPin = 13;      // select the pin for the LED for output
int senseCoil = 0;  // variable to store the value coming from current coil
const unsigned long runonDelay = 20000; // run on delay
unsigned long previousTime = 0; // set prevoius time

int state = 0;

void setup() {
  pinMode(ledPin, OUTPUT); // set pin as output
}

void loop() {
  unsigned long currentTime = millis();

  // read the value from the sensor:
  senseCoil = analogRead(sensorPin);
  if (senseCoil >= 500) {
    digitalWrite(ledPin, HIGH);
    state = 1;
  }
  else {
    if (state == 1) {
      // senseCoil was HIGH but has dropped LOW
      previousTime = currentTime;
      state == 2;
    }
  }

  if ( state == 2 ) {
    if (currentTime - previousTime >= runonDelay) {
      digitalWrite(ledPin, LOW);
      state = 0;
    }
  }
}

Blh64 thank you soo much!

I was working toward your answer but you have helped me out very much.
Thank you again for your time.

and for a bit more clarity, you can introduce an enumeration for your states, so they are more readable...

int sensorPin = A0;    // select the input pin for the current coil input
int ledPin = 13;      // select the pin for the LED for output
int senseCoil = 0;  // variable to store the value coming from current coil
const unsigned long runonDelay = 20000; // run on delay
unsigned long previousTime = 0; // set prevoius time

enum { WAITING, SENSOR_HIGH, ON_DELAY };
int state = WAITING;

void setup() {
  pinMode(ledPin, OUTPUT); // set pin as output
}

void loop() {
  unsigned long currentTime = millis();

  // read the value from the sensor:
  senseCoil = analogRead(sensorPin);
  if (senseCoil >= 500) {
    digitalWrite(ledPin, HIGH);
    state = SENSOR_HIGH;
  }
  else {
    if (state == SENSOR_HIGH) {
      // senseCoil was HIGH but has dropped LOW
      previousTime = currentTime;
      state = ON_DELAY;
    }
  }

  if ( state == ON_DELAY ) {
    if (currentTime - previousTime >= runonDelay) {
      digitalWrite(ledPin, LOW);
      state = WAITING;
    }
  }
}

Note: I also have a typo in the original code, I did a comparision '==' when it should have been an assignment '=' when transitioning to the on delay state.