I have searched this forum and Google high and low, but have yet to find a straightforward answer to this:
Can millis be used after a button?
I have reviewed the newbie posts here and tried as many examples as I could find that are relevant but I just can't seem to figure this one out.
My goal is to be able to have a few buttons (starting with 2 for now) and be able to execute a function using millis after it is pressed. The idea being that it would not be blocked and I could somehow add another button down the line to stop the function and go back.
I have been using TinkerCAD with a UNO R3. I have included a picture of the set up (sorry it's not the prettiest. I've been trying many things).
I have created 2 functions (I think that's the right name):
LED1_flash (which uses the delay function)
and
LED2_flash (which uses the millis function).
The goal right now is just to get them to flash appropriately when the correct button is pushed. I have bigger plans, but 1 step at a time.
Right now the following is happening:
- both LEDs are starting without need for the button to be pressed
- LED 2 turns itself on, but then does not blink
I have tried:
- putting the plain code after each button
- putting the function code after each button
- removing the flashing altogether and just turn on with the buttons (I think I got this to work but I can't find the code for it now)
I have been sick now for over 5 weeks so my brain isn't working right. Sorry if this is an obvious solution that I have missed!
const int BUTTON1 = 2; //right button
const int BUTTON2 = 4; //left button
const int LED1 = 8; //green
const int LED2 = 12; //yellow
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
int mode = 0;
const long eventTime_1_LED1 = 5000;
const long eventTime_2_LED2 = 1000;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long currentTime = millis();
unsigned long currentTime2 = millis();
boolean BUTTONoldState1 = HIGH;
boolean BUTTONoldState2 = HIGH;
void setup()
{
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//Get current button state
BUTTONstate1 = digitalRead(BUTTON1);
// check if state changed from high to low (button press)
if ((BUTTONstate1 == LOW) && (BUTTONoldState1 == HIGH)) {
Serial.print("ButtonState1");
Serial.print (BUTTONstate1);
// short delay to debouce button
delay(20);
//check if button is still low after debounce
BUTTONstate1 = digitalRead(BUTTON1);
if (BUTTONstate1 == LOW) { //Yes, still low
LED1_flash();
Serial.print("LED1 Flash");
}
delay(1000);
}
// Set the last-read state to the old state
BUTTONoldState1 = BUTTONstate1;
//Get current button state
BUTTONstate2 = digitalRead(BUTTON2);
// check if state changed from high to low (button press)
if ((BUTTONstate2 == LOW) && (BUTTONoldState2 == HIGH)) {
// short delay to debouce button
delay(20);
//check if button is still low after debounce
BUTTONstate2 = digitalRead(BUTTON2);
if (BUTTONstate2 == LOW) { //Yes, still low
LED2_flash();
Serial.print("LED2 Flash");
}
}
// Set the last-read state to the old state
BUTTONoldState2 = BUTTONstate2;
}
void LED1_flash() {
digitalWrite(LED1,HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(1000);
}
void LED2_flash() {
currentTime2 = millis();{
if ((currentTime2 - previousTime_2) >= eventTime_2_LED2){
digitalWrite(LED2, !digitalRead(LED2));
previousTime_2 = currentTime2;
}
}
}
Thank you for your assistance. I want to continue to develop my Arduino skills but sometimes self-teaching from tutorials only gets me so far
![]()
EQL


