Turn on the light after a certain time

I want to turn on a LED when the potentiometer is above a set value for a set time. I'm able to turn the LED on when the potentiometer has a value above, for example, 500. The delay function does not work, because within the delay time the value may have been below the set value again.

I've used the Millis based on several tutorials, but I can't get it to work.

What I have now:

const int analogPin = A2; //potentiometer
const int ledPin = 13;    
int ledBoard = 2;        //Led 
const int threshold = 500;  //the minimum value for the potentiometer to turn the led on

unsigned long startTime = 0;
unsigned long totalTime;
unsigned long time; 

void setup() {
pinMode(ledPin, OUTPUT);    
pinMode(ledBoard, OUTPUT);  
Serial.begin(9600);        
}

void loop() {
Serial.print("startTime");
Serial.print("\t");
Serial.println(startTime);
Serial.print("\t");
Serial.println(time);
  int analogValue = analogRead(analogPin); 
  if(analogValue > threshold) {

    digitalWrite(ledPin, HIGH);
    digitalWrite(ledBoard, HIGH);
 
  }   else {
  digitalWrite(ledPin, LOW);
  digitalWrite(ledBoard, LOW);
}

I know this code is not complete, I tried different things. I try start a timer when the value is above threshold, and substract the time Millis (time running since the Arduino is on) with the startTime. when the outcome is above a set time, the led needs to be turned on for a set amount of time, and then reset.

Hope you can help me.

I am a beginner, but I would suggest moving the potentiometer off of the A(1, 2, 3, 4, 5,) pins. I would put it on pin 7 or 8, if it's open. It's a crazy habit for me to use those first. I'd also suggest another pinMode in the setup. pinMode(analogPin, INPUT); otherwise, it won't reciognize the pin as an input.

const int analogPin = A2; //potentiometer
const int ledPin = 13;    
int ledBoard = 2;        //Led 
const int threshold = 500;  //the minimum value for the potentiometer to turn the led on

unsigned long startTime = 0;
unsigned long totalTime;
unsigned long time; 

void setup() {
pinMode(ledPin, OUTPUT);    
pinMode(ledBoard, OUTPUT);  
pinMode(analogPin, INPUT);
Serial.begin(9600);        
}

void loop() {
Serial.print("startTime");
Serial.print("\t");
Serial.println(startTime);
Serial.print("\t");
Serial.println(time);
  int analogValue = analogRead(analogPin); 
  if(analogValue > threshold) {

    digitalWrite(ledPin, HIGH);
    digitalWrite(ledBoard, HIGH);
 
  }   else {
  digitalWrite(ledPin, LOW);
  digitalWrite(ledBoard, LOW);
}

also, where is your timer? I don't see an int, const in, or #define anywhere outlining a timer.

should the LED remain on if the pot is still above the threshold or should the pot value be ignored until it goes below the threshold after setting the LED?

where do you capture the time when the pot is above the thresshold?

where do you test that the time as expired? where is the period of time you want the LED on for?

@avrahamgray I'm also a beginner. I don't have a timer yet and removed what I tried because I didn't get it to work. I don't know how to define the timer and use it, because I saw different methodes that didn't work for me.

"pinMode(analogPin, INPUT); otherwise, it won't reciognize the pin as an input"

I get the potentiometer working and writing the value to the monitor. Also turn the led on based on the threshold.

@gcjr The led needs to be on after 8sec above a set value. And stays on for 40seconds, then reset. If the value is still above threshold after resetting, the led will be on again for 40sec. if not, then nothing.

I had the Millis working, it just counts from the moment the Arduino is turned on. The rest?

so the led needs to be on for at least 40 secs and in intervals of 40 secs (i.e. not 40 secs after the pot goes below the threshold)

post that code

if you would like the led on for 8 seconds, here's an example.

// all the code before and after, add this to your led on/off

digitalWrite(ledPin, HIGH);
digitalWrite(ledBoard, HIGH);
delay(8000); // 1000 is one second
digitalWrite(ledPin,LOW);
digitalWrite(ledBoard,LOW);

This would be unacceptable for the OP since the OP stated the following:

I want to turn on a LED when the potentiometer is above a set value for a set time. I'm able to turn the LED on when the potentiometer has a value above, for example, 500. The delay function does not work, because within the delay time the value may have been below the set value again.

Indeed, in all but the most basic of cases it is unacceptable to block your code for 8 seconds.

Do you want it to turn off if you set it below the value? If not, use the delay, here's what will happen.

You go above the threshold, and it turns on for just 8 seconds, then takes another potentiometer reading. If it's above the threshold, it stays on for another 8 seconds, if it's below the threshold, it turns off. If you are looking for it to turn off after 8 seconds no matter what, and turn off when the potentiometer turns low, i can't help you.

@avrahamgray
Thanks, but that is not what I need. During the 8 second delay, the value could be lower than threshold.

For now, I use a potentiometer to write the code and test it. In the end I want to use a tiltsensor. so I change the value to an angle, but it should basically the same.

@ gcjr
with:

unsigned long currentMillis = millis(); 
Serial.print("currentMillis :");
Serial.println(currentMillis);

It prints the millis.

the pot exceeding the threshold triggers several events. the first it to capture the current time stamp.

it's unclear if you want the LED on immediately or after 8 secs. if so, the code needs to recognize when millis() now exceeds the captured time stamp + 8 secs and turn the LED on.

if (millis() - timeStamp > 8000 msec)

the time stamp should be captured again and millis() checked when it exceeds 40 sec + the timestamp

then there's the issue of retriggering if the pot still exceeds the threshold after 40 sec. seems that a state machine is needed

Thanks for the replies.

If the value is 9 seconds or more above the threshold, then the led needs to be turned on for 40 secsonds.

Can you show me the function for a timestamp?

const int analogPin = A2; //potentiometer
const int ledPin    = 13;
const int ledBoard  = 2;        //Led

const int threshold = 500;  //the minimum value for the potentiometer to turn the led on

const unsigned long OnTime    = 3000;
unsigned long       startTime = 0;

enum { Reset, Set, Timeout };
int state = Reset;

// -----------------------------------------------------------------------------
void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(ledBoard, OUTPUT);
    Serial.begin(9600);
}

// -----------------------------------------------------------------------------
void loop() {
    unsigned long msec = millis ();

    int analogValue = analogRead(analogPin);

    switch (state)  {
    case Reset:
        if(analogValue > threshold) {
            digitalWrite(ledPin, HIGH);
            state     = Set;
            startTime = msec;
        }
        break;

    case Set:
        if ( (msec - startTime) > OnTime)  {
            digitalWrite(ledPin, LOW);
            state     = Timeout;
        }
        break;

    case Timeout:
        if(analogValue < threshold)
            state = Reset;
        break;
    }

    Serial.println (state);
    delay (250);
}

Thank you for the reply and the code. I will look at it tomorrow.

What I see now with this code, is that the led is turned on as soon as the value goes above 500. And then stays on for 3 seconds.

When I change the code to this:

    case Set:
        if ( (msec - startTime) > OnTime)  {
            digitalWrite(ledPin, HIGH);
            digitalWrite(ledBoard, HIGH);

It wait 3 seconds, but it also goes on when the value is just a short moment above 500, just like the delay does.

thanks in advance