Cant get button to restart timer

Hello!
Im working on an assignment, but are having trouble with some basic code. The goal is to have six LEDs cycling through phases, like traffic light. and the button should reset the cycling back to phase one. But im not getting any reaction while pressing the button. Can anyone help?

Also, how can i limit the println to only write the phases once as they occur, and not to-fold times?

unsigned long current;
unsigned long previous = 0;
int switchState = 0;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);  // bryter
  pinMode(3, OUTPUT); // Rød nord/syd
  pinMode(4, OUTPUT); // Gul nord/syd
  pinMode(5, OUTPUT); // Grønn nord/syd
  pinMode(6, OUTPUT); // Rød øst/vest
  pinMode(7, OUTPUT); // Gul øst/vest
  pinMode(8, OUTPUT); // Grønn øst/vest
}

void loop() {
switchState = digitalRead(2);  
current = millis()/1000;
if (switchState == HIGH) {
    current = 0;    // reset time if button is pressed
    }
while(switchState == LOW) {
  current = millis()/1000;
   if (current < 30) {
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    Serial.println("Fase 1");
    } 
   else {
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    }
   if (current >= 30 && current < 35) {
    digitalWrite(4, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    Serial.println("Fase 2");
    } 
   else {
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    }
   if (current >= 35 && current < 75) {
    digitalWrite(3, HIGH);
    digitalWrite(8, HIGH);
    Serial.println("Fase 3");
    } 
   else {
    digitalWrite(3, LOW);
    digitalWrite(8, LOW);
    }
   if (current >= 75 && current < 80) {
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(7, HIGH);
    Serial.println("Fase 4");
    } 
   else {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(7, LOW);
    }      
   if (current >= 80) {
    current = 0;  // start over
    }  
  }
}
while(switchState == LOW) {

Once this while loop is entered you never read the state of the switch so the code is stuck in the while loop.

oh, I see... Do you have any ideas to how I can fix this?

You could add

switchState = digitalRead(2);

to the loop. Then the switch state is updated each time through the while loop.

But,

switchState = digitalRead(2); 
current = millis()/1000;
if (switchState == HIGH) {
    current = 0;    // reset time if button is pressed
    }

That code reads the state of the switch and tests for the button being pushed. There are only 2 states that the switch can have. Pushed and not pushed. So if that test fails there can be only one reason, the switch is not pushed. So you can replace the while with an else statement. The code then becomes; if the switch is pressed reset the timer, else run the sequence.

   switchState = digitalRead(2);
   current = millis() / 1000;
   if (switchState == HIGH)
   {
      current = 0;    // reset time if button is pressed
   }
   else
   {
      current = millis() / 1000;
      if (current < 30)
      {
         digitalWrite(5, HIGH);
         digitalWrite(6, HIGH);
         Serial.println("Fase 1");
      }
      else

there are several issues with your code

do you really want to execute the code with all the ifs only when the button in a particular state? don't you wan t that code to always be executed?

and in all those if/else, if/else, if/else ... isn't there really only one case you want executed? would you want to execute an if case that set the LEDs one way and then execute one or more else cases later that sets the LEDs differently

and don't you want the time condition you are comparing to to be something other than zero if you want this to work more than once

... for your assignment

groundFungus:
You could add

switchState = digitalRead(2);

to the loop. Then the switch state is updated each time through the while loop.

But,

That code reads the state of the switch and tests for the button being pushed. There are only 2 states that the switch can have. Pushed and not pushed. So if that test fails there can be only one reason, the switch is not pushed. So you can replace the while with an else statement. The code then becomes; if the switch is pressed reset the timer, else run the sequence.

I tried this, but whenever i push the button, the code just pauses, it does not go back to scratch. hmm.

gcjr:
there are several issues with your code

do you really want to execute the code with all the ifs only when the button in a particular state? don't you wan t that code to always be executed?

and in all those if/else, if/else, if/else ... isn't there really only one case you want executed? would you want to execute an if case that set the LEDs one way and then execute one or more else cases later that sets the LEDs differently

and don't you want the time condition you are comparing to to be something other than zero if you want this to work more than once

... for your assignment

Probably a good idea to get those conditions outside the if/else loops, but im too early in my "career" to know how.

but im too early in my "career" to know how.

isn't figuring out how the purpose of the assignment?

Look at where and when you set the value of the current variable.

Been trying for hours now, but Im getting nowhere... I want to figure it out by myself, but time is limited and not really seeing any progress on this assignment...

But I were wondering, maybe a switch statement sould be a better choice?

edit: got an pm with help, thanks!

what are you struggling with?

Do you have a pulldown resistor(10k) from pin 2 to GND?

JCA34F:
Do you have a pulldown resistor(10k) from pin 2 to GND?

there are logic errors (some pointed out) in the posted code that make reading the switch a tertiary issue

gcjr:
there are logic errors (some pointed out) in the posted code that make reading the switch a tertiary issue

If all those errors are corrected will the code then work with a floating input?

if it doesn't have a resistor, it's easy enough to configure the pin as INPUT_PULLUP