3 way traffic lights, programming trouble

Hi, im new to arduino uno and programming it, ive been asked by a friend to help him make 3 way traffic lights and program the arduino. We have the traffic lights built. We are just having some difficulites with the code. Could any one correct it or fix it for me please?
There will be 2 sets of traffic lights on a main road, there will be a secondary road joining the main road. When there is car detected at secondary road (Position B on diagram below) the lights on main road should turn RED. . As soon as there is no car underneath the sensor the light on secondary read should switch to red and main road to green. It should stay that way until there is car detected at secondary road.
The sequance should be Green-yellown then red. When car goes the light should change back straight to red skipping yellow.

const int red = 11; // 11,12,13 main road
const int yellow = 12;
const int green = 13;
int swt = 2; // switch instead of sensor for now
const int red1 = 8; // 8,9,10 secondary road
const int yellow1 = 9;
const int green1 = 10;
// sets the pins 8-13 as outputs // sets pin 2 as input
void setup(){
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(yellow1, OUTPUT);
  pinMode(green1, OUTPUT);
  pinMode(swt, INPUT);
}
// creates the loop that your Arduino will repeat over & over //
void loop(){
  changeLights();
}
// Function that will change lights in Irish traffic lights system
void changeLights()
{
 while(swt = HIGH)
  {
    digitalWrite(green, HIGH);
    digitalWrite(yellow, LOW);
    digitalWrite(red, LOW);
    delay(2000);
   digitalWrite(green1, LOW);
   digitalWrite(yellow1, LOW);
   digitalWrite(red1,  HIGH);  
   // turn off green, then turn yellow on for 5 seconds
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay(5000);
  // turn off yellow, then turn red on
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  //turn off red1, then turn on green1
  digitalWrite(green1, HIGH);
  digitalWrite(red1, LOW);
  }
  while(swt = LOW);
  {
  //turns red off, turns green on
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
   // turn off green1, then turn yellow1 on for 5 seconds
  digitalWrite(green1, LOW);
  digitalWrite(yellow1, HIGH);
  delay(5000);
  // turn off yellow1, then turn red1 on
  digitalWrite(yellow1, LOW);
  digitalWrite(red1, HIGH);
  }
 
}

I once had this as a homework question too, although on a completely different processor...

I would suggest two things:

  1. A wiring diagram. A photo of the test set up even?
  2. An explanation of what your code does now, and how it differs from what you expect it to do.

Here you go:

// Simple traffic lights

const int red = 11; // 11,12,13 main road
const int yellow = 12;
const int green = 13;
int swt = 2; // switch instead of sensor for now
const int red1 = 8; // 8,9,10 secondary road
const int yellow1 = 9;
const int green1 = 10;

// sets the pins 8-13 as outputs, pin 2 as input, switch goes to ground
// Initialise safe state
void setup(){
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(yellow1, OUTPUT);
  pinMode(green1, OUTPUT);
  pinMode(swt, INPUT_PULLUP);
  digitalWrite(green, LOW);
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  digitalWrite(green1, LOW);
  digitalWrite(yellow1, LOW);
  digitalWrite(red1,  HIGH);  
}

// creates the loop that your Arduino will repeat over & over //
void loop(){
  changeLights();
}

// Function that will change lights in Irish traffic lights system
void changeLights()          // to be sure, to be sure, to be sure
{
  digitalWrite(red, LOW);
  digitalWrite(yellow, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(green1, LOW);
  digitalWrite(yellow1, LOW);
  digitalWrite(red1,  HIGH);  
  delay(10000);

  while(swt = HIGH)
  {  
  }

  // Changing to favour side road
  // turn off green, then turn yellow on for 5 seconds
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay(5000);
  // turn off yellow, then turn red on
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  delay(2000);

  //turn off red1, then turn on green1
  digitalWrite(red1, LOW);
  digitalWrite(green1, HIGH);
  delay(10000);

  while(swt = LOW);
  {
  }

  // turn off green1, then turn yellow1 on for 5 seconds
  digitalWrite(green1, LOW);
  digitalWrite(yellow1, HIGH);
  delay(5000);
  // turn off yellow1, then turn red1 on
  digitalWrite(yellow1, LOW);
  digitalWrite(red1, HIGH);
  delay(2000);
}

There are a couple of problems with it, but let's leave that for a further exercise ...

Thanks lads for the reply,
@Dannable
Hi Dannable,
Thanks for the reply.
The code is changing the lights every 5 seconds seconds and in wrong order. The green does not come on at main road and red does not come on on secondary road. Do you want me to tell you like that ? or put comments beside code??

Thanks
@Paul__B
Hi Paul,
Thanks for the code, it is still not working. It does exactly same thing as it did before. I did some changes added “digitalRead(2);”
It did some change but still lights are changing in a loop of 5 seconds. I would like the lights on main and secondary road to stay constant until the switch goes high.

Thanks

Wasn't thinking about the input!

// Simple traffic lights

const int red = 11; // 11,12,13 main road
const int yellow = 12;
const int green = 13;
int swt = 2; // switch instead of sensor for now
const int red1 = 8; // 8,9,10 secondary road
const int yellow1 = 9;
const int green1 = 10;

// sets the pins 8-13 as outputs, pin 2 as input, switch goes to ground
// Initialise safe state
void setup(){
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(yellow1, OUTPUT);
  pinMode(green1, OUTPUT);
  pinMode(swt, INPUT_PULLUP);
  digitalWrite(green, LOW);
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  digitalWrite(green1, LOW);
  digitalWrite(yellow1, LOW);
  digitalWrite(red1,  HIGH);  
}

// creates the loop that your Arduino will repeat over & over //
void loop(){
  changeLights();
}

// Function that will change lights in Irish traffic lights system
void changeLights()          // to be sure, to be sure, to be sure
{
  digitalWrite(red, LOW);
  digitalWrite(yellow, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(green1, LOW);
  digitalWrite(yellow1, LOW);
  digitalWrite(red1,  HIGH);  
  delay(10000);

  while(digitalRead(swt) == HIGH)
  {  
  }

  // Changing to favour side road
  // turn off green, then turn yellow on for 5 seconds
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay(5000);
  // turn off yellow, then turn red on
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  delay(2000);

  //turn off red1, then turn on green1
  digitalWrite(red1, LOW);
  digitalWrite(green1, HIGH);
  delay(10000);

  while(digitalRead(swt) == LOW);
  {
  }

  // turn off green1, then turn yellow1 on for 5 seconds
  digitalWrite(green1, LOW);
  digitalWrite(yellow1, HIGH);
  delay(5000);
  // turn off yellow1, then turn red1 on
  digitalWrite(yellow1, LOW);
  digitalWrite(red1, HIGH);
  delay(2000);
}

That might work a little better! Other coding errors corrected, notably change to "=="

Without your rig however, I have not actually tried it.