[solved] traffic light code

i did a traffic light project out of a book and have adapted it for pedestrian lights as well but have problems with the lights sequence changing when i add my bit.

byte red_led = 6;
byte yellow_led = 5;
byte green_led = 4;
byte switch_pin = 2;
byte pedestrian_red = 8;
byte pedestrian_green = 7;
unsigned long red_start_time = 0;
unsigned long red_duration = 5000;
unsigned long yellow_start_time = 0;
unsigned long yellow_duration = 3000;
unsigned long green_start_time = 0;
unsigned long green_duration = 5000;
unsigned long current_time = 0;
unsigned long both_start_time = 0;
unsigned long both_duration = 3000;
unsigned long pedestrian_green_start = 0;
unsigned long pedestrian_green_duration = 250;
int button = 0;

void setup()
{
  pinMode (red_led, OUTPUT);
  pinMode (yellow_led, OUTPUT);
  pinMode (green_led, OUTPUT);
  pinMode (switch_pin, INPUT);
  pinMode (pedestrian_red, OUTPUT);
  pinMode (pedestrian_green, OUTPUT);
  digitalWrite (switch_pin, LOW);
  current_time = millis();
}

void loop()
{
 [u][i][i] if (button == 1);
  {
    digitalWrite (yellow_led, HIGH);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, LOW);
    digitalWrite (pedestrian_red, HIGH);
    digitalWrite (pedestrian_green, LOW);
    yellow_start_time = current_time;
    while ((current_time - yellow_start_time) <= yellow_duration)
    {
      current_time = millis();
    } 

    digitalWrite (yellow_led, LOW);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, HIGH);
    digitalWrite (pedestrian_red, LOW);
    digitalWrite (pedestrian_green, HIGH);
    red_start_time = current_time;
    pedestrian_green_start = current_time;
    while ((current_time - red_start_time) <= red_duration)
    {
      current_time = millis();
    }


    digitalWrite (yellow_led, HIGH);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, LOW);
    digitalWrite (pedestrian_red, LOW);
    digitalWrite (pedestrian_green, HIGH);
    yellow_start_time = current_time;
    while ((current_time - yellow_start_time) <= yellow_duration)
    {
      current_time = millis();

      while ((current_time - pedestrian_green_start) <= pedestrian_green_duration)
      {
        current_time = millis();
      }
      if (pedestrian_green == HIGH)
      {
        pedestrian_green = LOW;
        yellow_led = LOW;
      }
      else
      {
        pedestrian_green = HIGH;
        yellow_led = HIGH;
      }
      button = 0;
    }
  }
[/i][/i][/u]

  digitalWrite (yellow_led, HIGH);
  digitalWrite (green_led, LOW);
  digitalWrite (red_led, LOW);
  digitalWrite (pedestrian_red, HIGH);
  digitalWrite (pedestrian_green, LOW);
  yellow_start_time = current_time;
  while ((current_time - yellow_start_time) <= yellow_duration)
  {
    current_time = millis();
  }

  digitalWrite (yellow_led, LOW);
  digitalWrite (green_led, LOW);
  digitalWrite (red_led, HIGH);
  digitalWrite (pedestrian_red, HIGH);
  digitalWrite (pedestrian_green, LOW);
  red_start_time = current_time;
  while ((current_time - red_start_time) <= red_duration)
  {
    current_time = millis();
  }

  digitalWrite (yellow_led, HIGH);
  digitalWrite (green_led, LOW);
  digitalWrite (red_led, HIGH);
  digitalWrite (pedestrian_red, HIGH);
  digitalWrite (pedestrian_green, LOW);
  both_start_time = current_time;
  while ((current_time - both_start_time) <= both_duration)
  {
    current_time = millis();
  }

  digitalWrite (yellow_led, LOW);
  digitalWrite (green_led, HIGH);
  digitalWrite (red_led, LOW);
  digitalWrite (pedestrian_red, HIGH);
  digitalWrite (pedestrian_green, LOW);
  green_start_time = current_time;
  while ((current_time - green_start_time) <= green_duration)
  {
    current_time = millis();

    if (digitalRead (switch_pin) == HIGH)
    {
      button = 1;
      break;
    }
  }
}

the underlined bit is my code.
i also have problems with the pedestrian light not lighting but i think this is due to miswiring.

it didnt underline it properly so the bit i mean is between the (u)(i)(i) markers and is also any line with the word pedestrian

while ((current_time - yellow_start_time) <= yellow_duration)
    {
      current_time = millis();
    }

That is just the same and just as bad as using the delay function.
If you think you are using the blink without delay you are mistaken.

but have problems with the lights sequence changing when i add my bit.

So what sort of problems? The lights will do what you wrote so how do you want it to go?

i want it to go yellow, red, red and yellow, green in a cycle with the pedestrian red light on but if i press the button when green it should go yellow, red and pedestrian green, then flash the yellow and pedestrian green before returning to normal.

what it does instead is yellow, red, yellow, yelow, yellow and red, yellow and green, yellow etc. with the pedestrian green on all the time. this makes me think it is a combination of wiring and code problems

nothing happens when i press the button.

digitalWrite (switch_pin, LOW);

The processor has a built-in pullup which you have deliberately chosen not to use.
Why?

pins 4,5,6 -3 x leds - 3 x 220r resistors - 1 x ground

pins 7,8 - 2 x leds - 2 x 220r - 1 x ground

pin 2 - switch - 5v
|
10k resistor - ground

this is my wiring

the grounds are different but should i use the one at the end of the line of digital i/o pins on the uno?

when i did use the pullup the switch didn't work so i looked on google and someone had changed it and it worked

until i added the extra code

it probably didn't work originally because i wired it wrong

 if (button == 1);

Have you ever seen a conditional written like this?
Lose the semicolon.

now some of it works!

i know its the wrong forum but have i wired anything wrong?

the added lights still don't work

How can we tell?
We’re not psychic.

its reply 7

So, when you wired it up, you wrote a simple sketch that just sequenced through the LEDs, lighting and extinguishing them one by one, then in combinations.
Does that still work?

it works on the red_led, green_led and yellow_led but not the pedestrian_red and pedestrian_green

Test code:

const byte pinArray [] = {4, 5, 6, 7, 8};
const int N_LEDS = sizeof (pinArray) / sizeof (pinArray [0]);
int x;

void setup ()
{
  for (int i = 0; i < N_LEDS; i++)
  {
    pinMode (pinArray [i], OUTPUT);
    digitalWrite (pinArray [i], LOW);
  }
}

void loop ()
{
  for (int i = 0; i < N_LEDS; i++)
  {
    digitalWrite (pinArray [i], bitRead (x, i));
  }
  delay (250);
  x++;
}

all light up at some point except the pedestrian red on pin 8