Need help with millis

Hello,
I am working on a little project for school and need some help.
First part of project is done and working but I am missing part with third button.
So I have a pulldown buttons and I need to hold buttonpin3 for 3 sec and LED has to turn on, it has to be done using millis.
Here is one part of project:

int LED = 5;
int buttonpin1= 8;
int buttonpin2 = 9;
int buttonpin3= 10;
int buttonState1= 0;
int buttonState2= 0;
int buttonState3= 0;

void setup() {
pinMode(LED, OUTPUT);
pinMode(buttonpin1, INPUT_PULLUP);
pinMode(buttonpin2 , INPUT_PULLUP);
pinMode(buttonpin3, INPUT_PULLUP);
}

void loop() {
buttonState1= digitalRead(buttonpin1);
if(buttonState1== LOW){
delay(20);
buttonState1= digitalRead(buttonpin1);
if(buttonState1== HIGH){
digitalWrite(LED, HIGH);
}
}
buttonState2= digitalRead(buttonpin2 );
if(buttonState2 == LOW){
digitalWrite(LED, LOW);
}

}

Also would appreciate if you could keep it simple because I am still learning and don't know a lot
Big thanks to anyone who helps

Welcome to the forum. I hope you stick with Arduino long after this assignment and come back here often for help and to help others. Can you try to write the 3rd button logic using your own words? Let's see your best effort at it and I don't have any problem helping with syntax or code.

1 Like

Look at the Blink Without Delay Example and Using multiple millis() for timing.

Try to understand how Blink Without Delay works. millis() basically "runs forever" so you just grab snapshots of the millis() time every time through the loop and subtract & compare to see how much time has passed.

Of course there is only one currentMillis but you can have multiple intervals (such as interval1 & interval2) and you can have multiple-associated previousMillis (such as previousMillis1 & previousMillis(2).

Or you can use more descriptive variable names, so if you have red & green LEDs you can have intervalRed & intervalGreen, etc.

1 Like

Thanks,
So when the LED is off I need to press button 3 and hold it. After 3 sec of holding the button, LED has to turn on. After the LED turns on, button can be released and LED has to stay on.

Now make that look like code.

// global variable
unsigned long button3Start;
//
if (digitalRead(LED) == LOW && digitalRead(buttonpin3) == LOW)  button3Start = millis();
if (digitalRead(buttonpin3) == LOW && millis() - button3Start >= 3000) digitalWrite(LED, HIGH);
1 Like

More conventional formatting...

// global variable
unsigned long button3Start;
//
if (digitalRead(LED) == LOW && digitalRead(buttonpin3) == LOW)
  button3Start = millis();
if (digitalRead(buttonpin3) == LOW && millis() - button3Start >= 3000)
  digitalWrite(LED, HIGH);

1 Like

Thanks a lot for help,
I just had to change "digitalRead(buttonpin3) == LOW " to "digitalRead(buttonpin3) == HIGH" and now it works perfectly

1 Like

Here is full program:

int LED = 5;
int buttonpin1 = 8;
int buttonpin2 = 9;
int buttonpin3 = 10;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

unsigned long button3Start;

void setup() {
pinMode(LED, OUTPUT);
pinMode(buttonpin1, INPUT_PULLUP);
pinMode(buttonpin2 , INPUT_PULLUP);
pinMode(buttonpin3, INPUT_PULLUP);
}

void loop() {
buttonState1= digitalRead(buttonpin1);
if(buttonState1== LOW){
delay(20);
buttonState1= digitalRead(buttonpin1);
if(buttonState1== HIGH){
digitalWrite(LED, HIGH);
}
}
buttonState2= digitalRead(buttonpin2 );
if(buttonState2 == LOW){
digitalWrite(LED, LOW);
}

if (digitalRead(LED) == LOW && digitalRead(buttonpin3) == HIGH) button3Start = millis();
if (digitalRead(buttonpin3) == LOW && millis() - button3Start >= 3000) digitalWrite(LED, HIGH);
}

1 Like

I am going to pretend that was intentional. Good luck on your assignment.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.