Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Declare the necessary variables
- int numStates=3;
- int states[6];
- int stateDelays[6];
- int currentState;
- bool A;//marks which set of lights are being cycled through currently
- int MAX;//index for the 3rd light in the current set of lights
- int MIN;//index for the 1st light in the current set of lights
- void setup() {
- states[0] = 36;//red 1 + red 2
- states[1] = 34;//green 1 + red 2
- states[2] = 33;//amber 1 + red 2
- states[3] = 36;//red 2 + red 1
- states[4] = 20;//green 2 + red 1
- states[5] = 12;//amber 2 + red 1
- //setup the first set of lights (index 0 to 2 in the states array)
- A = true;
- MAX = 2;
- MIN = 0;
- stateDelays[0] = 2;//red and red combo
- stateDelays[1] = 10;//green and red combo
- stateDelays[2] = 2;//amber and red combo
- stateDelays[3] = 2;//red and red combo
- stateDelays[4] = 10;//red and green combo
- stateDelays[5] = 2;//red and amber combo
- DDRB = DDRB | B00111111; //set Arduino pins 8 to 13 as output
- int currentState = 0;//set the first state to 0
- PORTB = states[currentState];
- }
- void loop() {
- //assign the current state to port B
- PORTB = states[currentState];
- //wait in the current state for specified time
- delay(stateDelays[currentState]*1000);
- //call function to get the new state
- currentState = transition(currentState);
- }
- int transition(int currentState){
- //if the current state is the last index in the current set of lights
- if(currentState == MAX){
- if(A){
- //switch to the second set of lights
- MAX = 5;
- MIN = 3;
- A = false;
- }
- else{
- //switch to the first set of lights
- MAX = 2;
- MIN = 0;
- A = true;
- }
- //assign the current state to the first index in the next set of lights
- currentState=MIN;
- }else{
- currentState++;//otherwise transition normally
- }
- return currentState;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement