Issues with millis timing with a button press as well as seven segment display

Hi guys, I'm having a few issues with this code.
When the button is pressed, the code enters changeLights but the if statement in State1 isn't being met. I've been testing a lot of things but the if statement just won't become true. Not sure why.

Another issue is timing the SSD with State4. I've tried having an if statement when the WritetoSSD is called in order to countdown from 9 to 0 but that seems to not work either.

#define State1             0
#define State2             1
#define State3             2
#define State4             3
#define State5             4
#define State6             5
int state                = 0;
int redstate           = LOW;

int serialState = 1;
int counter = 0;
unsigned long previousTime;
bool oldButtonState;


const int redPeda        = 11;
const int greenPeda      = 6;
const int potent         = A3;
const int photoresist    = A0;
const int red            = 9;
const int green          = 10;
const int buttonPIN      = 12;
const int Streetlight    = A1;

int brightness;           
int potentValue;
int sensorValue;


int segAtoGpinout [7] = {5,4,3,2,7,13,8}; //A-G excluding DP led 


                          //{a,b,c,d,e,f,g}
int seg_numbers [10][7] =  {{1,1,1,1,1,1,0}, //0
                            {0,1,1,0,0,0,0}, //1
                            {1,1,0,1,1,0,1}, //2
                            {1,1,1,1,0,0,1}, //3                                       
                            {0,1,1,0,0,1,1}, //4
                            {1,0,1,1,0,1,1}, //5
                            {1,0,1,1,1,1,1}, //6
                            {1,1,1,0,0,0,0}, //7
                            {1,1,1,1,1,1,1}, //8
                            {1,1,1,0,0,1,1}};//9
  
void numWrite(int);
void WritetoSSD(int *);

void setup()
  
{
  previousTime = millis();
  for (int i=0; i<=7 ;i++) {
  pinMode(segAtoGpinout[i], OUTPUT);
  }
  
  pinMode(buttonPIN, INPUT_PULLUP);
  pinMode(redPeda, OUTPUT);
  pinMode(greenPeda, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(Streetlight, OUTPUT);
  pinMode(photoresist, INPUT);
  Serial.begin(9600);
  Serial.println("State 1");
  digitalWrite(redPeda, HIGH);
  analogWrite(green, 255);
  analogWrite(red, 0);
  digitalWrite(greenPeda, LOW);
}



void loop()
{
  digitalWrite(redPeda, HIGH);
  analogWrite(green, 255);
  analogWrite(red, 0);
  digitalWrite(greenPeda, LOW);
  potentValue = analogRead(potent); //read and store value from potentiometer
  sensorValue = analogRead(photoresist);
  brightness = map(potentValue, 0, 1023, 0, 255);
  analogWrite(redPeda, brightness);
  analogWrite(greenPeda, brightness);
  analogWrite(Streetlight, sensorValue);
  analogWrite(red, brightness); 
  analogWrite(green, brightness);
  
  bool buttonState = digitalRead(buttonPIN);
   if (oldButtonState != buttonState)
      {
        oldButtonState = buttonState;
        if (buttonState == LOW)
        {
          Serial.println ("State 1  Button: Pressed  Traffic Light: Green  Pedestrian Light: Red");
          previousTime = millis();
          changeLights();
        }
      }
}
/*


void SerialMonitor()
{
 Serial.print("Traffic/Pedestrian LED Brightness (Potentiometer):");
 Serial.print(brightness);
 Serial.println("Traffic: Red:");
 Serial.print(red);
 Serial.print(",Green:");
 Serial.print(green);
 Serial.print(",Yellow:");
 Serial.println("Pedestrian: Red:");
 Serial.print(redPeda);
 Serial.print(",Green:");
 Serial.print(greenPeda);
 Serial.println("Streetlights: ON");
 Serial.println("Button: Pressed");
}
  */


void WritetoSSD(int * segArray ) 
{
  if (millis() - previousTime >= 1000){
    for (int i=0 ;i<=9 ;i++) {
    digitalWrite(segAtoGpinout[i], segArray[i]);
    }
   }
}  

void changeLights()
{
  Serial.println("hi");
  switch (state)
  {
    
    //*********************************
    case State1:
      Serial.println("hi there 2");
      if (millis() - previousTime >= 2000)
      {
        analogWrite(red, 255);
        digitalWrite(green, 255);
        Serial.println ("State 2  Button: Not Pressed  Traffic Light: Green  Pedestrian Light: Red");
        previousTime = millis();
        state = State2;
      }
      
      break;
    
    //*********************************
    case State2:
      if (millis() - previousTime >= 5000)
      {
        analogWrite(red, 255);
        analogWrite(green, 0);
        Serial.println ("State 3  Button: Not Pressed  Traffic Light: Yellow  Pedestrian Light: Red");
        previousTime = millis();
        state = State3;
      }
      break;
    
    //*********************************
    case State3:
      if (millis() - previousTime >= 3000)
      {
        digitalWrite(redPeda, LOW);
        digitalWrite(greenPeda, HIGH);
        Serial.println("State 4");
        previousTime = millis();
        state = State4;
      }

      break;
    
    //*********************************
    case State4:
    
      if ((millis() - previousTime) >= 1000) // this line 
      {
       digitalWrite(greenPeda, LOW);
       counter = counter + 1;
       Serial.println("State 5");
       redstate = !redstate;
       previousTime = millis();
       digitalWrite (redPeda, redstate);
      }
    
      for (int count = 9; count>=9; count--){
       WritetoSSD(seg_numbers[count]);
      }
      
     if (counter >= 9)
      {
     digitalWrite(redPeda, HIGH);
     state = State5;
      }
    break;
 
    //**********************************
    case State5:
      if ((millis() - previousTime) >= 1000)
    {
      Serial.println("State6");
      digitalWrite (redPeda, HIGH);
      previousTime = millis();
      state = State6;
    }

    
    break;
    
    //***********************************
    case State6:
      if (millis() - previousTime >= 7000)
    {
      Serial.println("State7");
      analogWrite (red, 0);
      analogWrite (green, 255);
    }
    break;

    

  } //END of switch/case

} //END of loop()

int segAtoGpinout [7] = {5,4,3,2,7,13,8}; seven elements

Eight iterations and a 7 element array ?

That was a mistake on our part. Thanks for spotting it.

What do you think this does ?

You do know the above count variable is not the same as the below counter variable:

I'm told its supposed to countdown from 9 to 0. I didn't write this segment so if it's wrong I'm not sure why that is.

Yes they do different things. the count is for the ssd, the counter is for the LEDs.

The above says count starts at 9 and can go greater or equal to 9.


Tell us what this does ?


for (int count = 9; count >= 0; count--)

The SSD appears to show 0 while in State 4 but I think it may be going through the numbers extremely quickly but I'm not sure.

The above goes from 9 to 0

Your version only goes to 9 only

Do you see a problem here ?

Honestly, I don't understand what you're trying to say. Sorry for my ignorace.

You said:

It does not, it goes to 9 only.

BTW

digitalWrite(green, 255);
is this the proper syntax ?

Ok, why is that? How can I fix it? Also I fixed the syntax on your other comment.

Read the reference:

https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/

https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

ForLoopIllustrated