The millis function has been my nemesis in learning to code arduino but this time i'm determined to learn it once and for all.
So what i'm trying to do is once a button is pressed, open a lock after 5 seconds.
#define lock 8
#define sensor 2
bool stepFinished = false;
unsigned long t;
void setup() {
Serial.begin(9600);
pinMode(lock,OUTPUT);
pinMode(sensor,INPUT_PULLUP);
digitalWrite(lock,LOW);
Serial.println("arduino start");
}
void loop() {
if(digitalRead(sensor)==0 && stepFinished == false)
{
Serial.println("step finished");
stepFinished = true;
t = millis();
}
if(stepFinished = true && millis() - t >= 5000)
{
digitalWrite(lock,HIGH);
Serial.println("lock opened");
}
}
This code will turn the lock HIGH after 5 seconds regardless of the button position.
I tried all the ways around it, i'm pretty sure i'm missing something basic.