Trying to solve millis problem

Hi guys,

im a first year robotics student from holland and have a small question. Tomorrow i will have a test where i should show some small set of skills. One of them is to let an led blink. With a delay this is very easy ofcourse. But because i want to use a button and i want that on any given time in the code the button can be used to switch to another method (or function) i thought working with the millis(); function was one of the best options.

(problem is in the void blinkRight(), the leds will keep on burning when i push the right button (buttonRight).
Does anyone have an idea why this is happening? And maybe you guys have some more feedback or suggestions! Thanks a lot in advance!
I wrote the following code:

int led2 = 2;
int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int buttonRight = 8;
int buttonLeft = 9;
int state;
unsigned long Timer2;
unsigned long Timer3;
unsigned long Timer4;
unsigned long Timer5;
unsigned long Timer6;
unsigned long Timer7;
long previousMillis2 = 0;        
long previousMillis3 = 0; 
long previousMillis4 = 0; 
long previousMillis5 = 0; 
long previousMillis6 = 0; 
long previousMillis7 = 0; 
long interval = 1000;


void setup() {                
  pinMode(led2, OUTPUT);     
  pinMode(led3, OUTPUT); 
  pinMode(led4, OUTPUT); 
  pinMode(led5, OUTPUT); 
  pinMode(led6, OUTPUT); 
  pinMode(led7, OUTPUT); 
  pinMode(buttonRight, INPUT);
  pinMode(buttonLeft, INPUT);
  
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led7, LOW);
}

void blinkRight(){
  while(digitalRead(buttonLeft) != HIGH){    
    digitalWrite(led2, HIGH);
    Timer2 = millis();
      if((millis() - Timer2) >= 150){
        digitalWrite(led2, LOW);
      }
    digitalWrite(led3, HIGH);
    Timer3 = millis();
      if((millis() - Timer3) >= 150){
        digitalWrite(led3, LOW);
      }
    digitalWrite(led4, HIGH);
    Timer4 = millis();
      if((millis() - Timer4) >= 150){
        digitalWrite(led4, LOW);
      }
    digitalWrite(led5, HIGH);
    Timer5 = millis();
      if((millis() - Timer5) >= 150){
        digitalWrite(led5, LOW);
      }
    digitalWrite(led6, HIGH);
    Timer6 = millis();
      if((millis() - Timer6) >= 150){
        digitalWrite(led6, LOW);
      }
    digitalWrite(led7, HIGH);
    Timer7 = millis();
      if((millis() - Timer7) >= 150){
        digitalWrite(led7, LOW); 
      }
  }
}

void blinkLeft(){
  while(digitalRead(buttonRight) != HIGH){
        
    digitalWrite(led7, HIGH);
    delay(150);
    digitalWrite(led7, LOW);
    digitalWrite(led6, HIGH);
    delay(150);
    digitalWrite(led6, LOW);
    digitalWrite(led5, HIGH);
    delay(150);
    digitalWrite(led5, LOW);
    digitalWrite(led4, HIGH);
    delay(150);
    digitalWrite(led4, LOW);
    digitalWrite(led3, HIGH);
    delay(150);
    digitalWrite(led3, LOW);
    digitalWrite(led2, HIGH);
    delay(150);
    digitalWrite(led2, LOW); 
  }  
}

// the loop routine runs over and over again forever:
void loop() {
  if(digitalRead(buttonRight) == HIGH){
    blinkRight();
  } else if(digitalRead(buttonLeft) == HIGH){
    blinkLeft();
  }
}
    Timer2 = millis();
      if((millis() - Timer2) >= 150){
        digitalWrite(led2, LOW);
      }

Lets say the value returned by millis() is 100. What will the value of "millis() - Timer2" be? Let's say its 1,000, 10,000, 100,000, etc. How can the result of that if statement ever be true?

Hi!

Thank you for your reply.

So the method millis(); will be only checked there when the code is running there.

I hoped the millis(); function would be checked at anytime the application is running.
Now i still dont know how i can make a button which can be pressed at any given moment..

Lets say i want to make a circle of LED's which will blink one for one. But at any given moment when these LED's are making a circle of blinks i want to be able to press a button which will make them start from the other side... Do you have any ideas for me?

Once again,

i'm really thankfull for your help

Zzasr:
So the method millis(); will be only checked there when the code is running there.

What code running where? Maybe try rephrasing this

I hoped the millis(); function would be checked at anytime the application is running.

millis() is updated in the “background”. It’s “checked” whenever you call it.

Now i still dont know how i can make a button which can be pressed at any given moment…

What is physically limiting you from pressing the button? I’ve never come across a button that limits itself from being pressed at certain times. Maybe try rephrasing this too.

Lets say i want to make a circle of LED's which will blink one for one. But at any given moment when these LED's are making a circle of blinks i want to be able to press a button which will make them start from the other side... Do you have any ideas for me?

BlinkWithoutDelay example shows you how to blink a single LED without blocking code. Combine this with a finite state machine that keeps track of which LED should be turned on next will led you build a sequence out of this.

Outside of all that code, you can poll the buttons and make adjustments to the parameters of the finite state machine. Simple pseudo code:

if its been long enough
{
  turn the current led off
  turn the next led on
  set the next led to the current led
  set the last change time to now
}

if button1 is pushed
  set the next led to current led + 1
else if button2 is pushed
  set the next led to current led - 1

Thank you for your help. Im now going off to bed. Its 1:37 AM over here. I almost got it working now. Thank you for your help, it was really usefull! I'll let you know how it went tomorrow!

Greetings from Holland!