That is basically indeed simple 
void loop()
{
if(digitalRead(yourPin) == HIGH)
{
// your code for the one mode here
}
else
{
// your code for the other mode here
}
}
There are a few things to consider
1)
buttons bounce; with your code that is not an issue because you have massive delays
2)
floating inputs will cause erratic behaviour
3)
it depends on how your button or switch is wired if it's HIGH when you place it in the one position or press it or LOW
4)
you have to keep the button pressed
For (2), you can prevent this using a pullup or pulldown resistor. Without additional components, pullup is the easiest. Wire the button or switch between the Arduino pin and ground and use pinMode(yourPin, INPUT_PULLUP) as described by chummer1010.
int greenled = 8; //Led's and pins
int yellowled = 9;
int redled = 10;
const byte modeSwitch = 2;
void setup()
{
pinMode(greenled, OUTPUT); //Pinmodes of the leds
pinMode(yellowled, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(modeSwitch, INPUT_PULLUP);
}
Note the use of byte; if you change your led pins as well to byte, you save some bytes. Also note the use of the 'const' keyword. If you ever accidentally try to change the pin number, the compiler will complain.
In the next step, I suggest that you use functions for the two modes. Your loop() is far too long.
void modeOne()
{
// your code for mode 1 here
...
...
}
void modeTwo()
{
// your code for mode 2 here
...
...
}
and in loop()
void loop()
{
// if button not pressed or switch in position 1
if(digitalRead(modeSwitch) == HIGH)
{
modeOne();
}
// if button pressed or switch in position 2
else
{
modeTwo();
}
}
For (4), if you use a button and want to toggle between the two modes (so not keepin it pressed), you need to remember the state (as described by chummer1010). Also have a look at the StateChangeDetection example and the Debounce example that comes with the IDE.
For (1), I mentioned the massive delay (so debounce is not needed). Your first mode takes nearly 8 seconds (if I counted correctly
). This means that if you're in that first mode (and using above code) you will have to press the button for nearly 8 seconds (max) before the code will detect it. If you use a switch with two positions, you can flip it but the effect will still only be seen after those 8 seconds.
You will be better of to split the code for a mode into pieces using something like a state machine. For mode 1, there are 20 different states. E.g the first state is where the green led is on for 500ms, the second state is where the green led is off and the yellow one is on for 500ms and so on
void modeOne()
{
//1
digitalWrite(greenled, HIGH);
delay(500);
//2
digitalWrite(greenled, LOW);
digitalWrite(yellowled, HIGH);
delay(500);
...
...
//20
digitalWrite(greenled, LOW);
}
Now you can use a variable to keep track of what the code is doing / has to do. I call it currentState.
int greenled = 8; //Led's and pins
int yellowled = 9;
int redled = 10;
const byte modeSwitch = 2;
// variable for state machine; initial state 1
int currentState = 1;
void setup()
{
...
...
}
void loop()
{
...
...
}
void modeOne()
{
...
...
}
void modeTwo()
{
...
...
}
And we will use that variable in modeOne in a switch / case
void modeOne()
{
switch (currentState)
{
//1
case 1:
digitalWrite(greenled, HIGH);
delay(500);
// goto next state
currentState++;
break;
//2
case 2:
digitalWrite(greenled, LOW);
digitalWrite(yellowled, HIGH);
delay(500);
// goto next state
currentState++;
break;
do other cases here
...
...
//20
case 20:
digitalWrite(greenled, LOW);
// you might want to add a delay here
...
// reset currentState to 1 so we start all over
currentState = 1;
break;
}
}
Using this approach, you only have to wait a maximum of 500ms before the button or switch is checked again. Each time that a step (state) is finished, the currentState variable will be incremented and the next time that you call modeOne it will execute the next step. Problem with a button is that you have to keep it pressed to stay in mode 2. So have a look at the statechange example; with a minimum of 250ms delay, debouncing is probably still not required.
You can do the same for the second mode in the function modeTwo.
To solve the problem that you have to keep the button pressed to stay in mode 2, you can implement the below in loop(). This implements a 'toggle' where you have to press and release the button to switch between the two modes.
void loop()
{
// variable to remember the last state of the button
static byte lastState = HIGH;
// variable to remember selected mode
static byte selectedMode = 1;
// read the button
byte currState = digitalRead(modeSwitch);
// if it changed
if (currState != lastState)
{
// button was released if last state was LOW
if (lastState == LOW)
{
// change selected mode
if (selectedMode == 1)
selectedMode = 2;
else
selectedMode = 1;
// reset currentState so we start from the beginning
currentState = 1;
}
// remember last state
lastState = currState;
}
if (selectedMode == 1)
{
modeOne();
}
else
{
modeTwo();
}
}
Note the use of the 'static' keyword; a 'static' acts like a global variable (those that yoy declare outside any function) so the last value is remembered but only has local scope and hence can only be used inside the function where it is declared (loop() in this case).
Note1:
The one thing you might need to consider is to add a third mode so the traffic light does not suddenly changes from green to red but goes via orange.
Note2
This is not a perfect implementation (meaning things can be implemented in a better way); you also should eventually get rid of the use of delay and use millis() based timing (have a look at the BlinkWithoutDelay example that comes with the IDE).