[solved] combination code

i am trying to run a program which runs a traffic light program and the example servo knob simultaneously. it worked perfectly until i pressed the pushbutton, when it disconnected from my computer and restarted the program. i doubt it could be a wiring problem so this is my code

byte red_led = 6;
byte yellow_led = 5;
byte green_led = 4;
byte switch_pin = 2;
byte pedestrian_red = 8;
byte pedestrian_green = 7;
byte waiting = 9;
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;
int button = 0;
int i = 0;
#include <Servo.h> 
Servo myservo;
int potpin = 0;
int val;

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, HIGH);
  current_time = millis();
  Serial.begin(9600);
  myservo.attach(10);
}

void loop()
{
  if (button == 0)
  {
    current_time = millis();
    digitalWrite (yellow_led, LOW);
    digitalWrite (green_led, HIGH);
    digitalWrite (red_led, LOW);
    digitalWrite (pedestrian_red, HIGH);
    digitalWrite (pedestrian_green, LOW);
    Serial.println(48);
    green_start_time = current_time;
    while ((current_time - green_start_time) <= green_duration)
    {
      current_time = millis();
      val = analogRead(potpin);
      val = map(val, 0, 1023, 1, 179);
      myservo.write(val);

      if
        (digitalRead (switch_pin) == LOW)
      {
        button = 1;
        digitalWrite (waiting, HIGH);
      }
    }

    current_time = millis();
    digitalWrite (yellow_led, HIGH);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, LOW);
    digitalWrite (pedestrian_red, HIGH);
    digitalWrite (pedestrian_green, LOW);
    Serial.println(58);
    yellow_start_time = current_time;
    while ((current_time - yellow_start_time) <= yellow_duration)
    {
      current_time = millis();
      val = analogRead(potpin);
      val = map(val, 0, 1023, 1, 179);
      myservo.write(val);
      if (digitalRead (switch_pin) == LOW)
      {
        button = 1;
        digitalWrite (waiting, HIGH);
      }
    }

    current_time = millis();
    digitalWrite (yellow_led, LOW);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, HIGH);
    digitalWrite (pedestrian_red, HIGH);
    digitalWrite (pedestrian_green, LOW);
    Serial.println(68);
    red_start_time = current_time;
    while ((current_time - red_start_time) <= red_duration)
    {
      current_time = millis();
      val = analogRead(potpin);
      val = map(val, 0, 1023, 1, 179);
      myservo.write(val);
      if (digitalRead (switch_pin) == LOW)
      {
        button = 1;
        digitalWrite (waiting, HIGH);
      }
    }

    current_time = millis();
    digitalWrite (yellow_led, HIGH);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, HIGH);
    digitalWrite (pedestrian_red, HIGH);
    digitalWrite (pedestrian_green, LOW);
    Serial.println(568);
    both_start_time = current_time;
    while ((current_time - both_start_time) <= both_duration)
    {
      current_time = millis();
      val = analogRead(potpin);
      val = map(val, 0, 1023, 1, 179);
      myservo.write(val);
      if (digitalRead (switch_pin) == LOW)
      {
        button = 1;
        digitalWrite (waiting, HIGH);
      }
    }
  }

  current_time = millis();
  digitalWrite (yellow_led, LOW);
  digitalWrite (green_led, HIGH);
  digitalWrite (red_led, LOW);
  digitalWrite (pedestrian_red, HIGH);
  digitalWrite (pedestrian_green, LOW);
  Serial.println(48);
  green_start_time = current_time;
  while ((current_time - green_start_time) <= green_duration)
  {
    current_time = millis();
    val = analogRead(potpin);
    val = map(val, 0, 1023, 1, 179);
    myservo.write(val);

    if (digitalRead (switch_pin) == LOW)
    {
      button = 1;
      digitalWrite (waiting, HIGH);
    }
  }

  if (button == 1)
  {
    current_time = millis();
    digitalWrite (yellow_led, HIGH);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, LOW);
    digitalWrite (pedestrian_red, HIGH);
    digitalWrite (pedestrian_green, LOW);
    Serial.println(57);
    yellow_start_time = current_time;
    while ((current_time - yellow_start_time) <= yellow_duration)
    {
      current_time = millis();
      val = analogRead(potpin);
      val = map(val, 0, 1023, 1, 179);
      myservo.write(val);
    } 

    current_time = millis();
    digitalWrite (waiting, LOW);
    digitalWrite (yellow_led, LOW);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, HIGH);
    digitalWrite (pedestrian_red, LOW);
    digitalWrite (pedestrian_green, HIGH);
    Serial.println(67);
    red_start_time = current_time;
    while ((current_time - red_start_time) <= red_duration)
    {
      current_time = millis();
      val = analogRead(potpin);
      val = map(val, 0, 1023, 1, 179);
      myservo.write(val);
    }

    current_time = millis();
    digitalWrite (yellow_led, HIGH);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, LOW);
    digitalWrite (pedestrian_red, LOW);
    digitalWrite (pedestrian_green, HIGH);
    Serial.println(57);
    while (i < 10)
    {
      red_start_time = current_time;
      while ((current_time - red_start_time) <= 400)
      {
      }
        if(yellow_led == HIGH)
        {
          digitalWrite (yellow_led, LOW);
          digitalWrite (pedestrian_green, LOW);
        }

        else
        {
          digitalWrite (yellow_led, HIGH);
          digitalWrite (pedestrian_green, HIGH);
        }
        current_time = millis();
        
      i++;
    }
    button = 0;
    i = 0;
  }
}

the serialmonitor bit is the pin numbers of the lit leds

even after it disconnected everything but the serialmonitor, upload button on my computer, and button worked

i am using a keyes 5.1 shield to accommodate the 4 5v pins

i am 99% sure i have not plugged it into a reset socket

its ok, the button now works, but it is stuck on

 current_time = millis();
    digitalWrite (yellow_led, HIGH);
    digitalWrite (green_led, LOW);
    digitalWrite (red_led, LOW);
    digitalWrite (pedestrian_red, LOW);
    digitalWrite (pedestrian_green, HIGH);
    Serial.println(57);
    while (i < 10)
    {
      red_start_time = current_time;
      while ((current_time - red_start_time) <= 400)
      {
      }
        if(yellow_led == HIGH)
        {
          digitalWrite (yellow_led, LOW);
          digitalWrite (pedestrian_green, LOW);
        }

        else
        {
          digitalWrite (yellow_led, HIGH);
          digitalWrite (pedestrian_green, HIGH);
        }
        current_time = millis();
        
      i++;
    }

and not flashing

it was a wiring problem i think

i have added a current_time = millis(); in the 2nd while loop so it doesn't freeze but it still doesn't flash like i want it to.

I like your blog.

       red_start_time = current_time;
      while ((current_time - red_start_time) <= 400)
      {
      }

Now = then. While now - then is less than 400, do nothing. Since now - then is 0, the likely of this loop ending before the sun burns out is pretty small.

oh whoops, sorted that - it doesn't freeze but still doesn't flash. it is this bit that is the problem

while (i < 10)
    {
      red_start_time = current_time;
      while ((current_time - red_start_time) <= 400)
      {
        current_time = millis();
        val = analogRead(potpin);
        val = map(val, 0, 1023, 1, 179);
        myservo.write(val);
      }
      if(yellow_led, HIGH) //todo: sort out odd behaviour
      {
        digitalWrite (yellow_led, LOW);
        digitalWrite (pedestrian_green, LOW);
        Serial.println(0);
      }
      else
      {
        digitalWrite (yellow_led, HIGH);
        digitalWrite (pedestrian_green, HIGH);
        Serial.println(57); // test if on
      }
      current_time = millis();

      i++;
    }

all i get on the serial monitor is 0000000000 for this bit

if(yellow_led, HIGH)

Comma is not a logical operator, unless you include "ignore"

what should i substitute for the comma? should i use a local variable?

i tried == now all i get is leds on and 57 on the serial monitor, no flashing

fishnchips2:
i tried == now all i get is leds on and 57 on the serial monitor, no flashing

You mean you tried

if (yellow_led == HIGH)

?

Considering you assign the value of 5 to yellow_led, how will 5 ever equal HIGH? You need to separate pin variables from state variables. This is why it pays to name variables properly:

const int yellowLEDPin = 5;
int yellowLEDState = HIGH;

yellowLEDPin is then only used in calls to pinMode, digitalRead and digitalWrite.

yellowLEDState is assigned values or used in selection statements.

thanks, thats sorted