Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. //Declare the necessary variables
  2.  
  3. int numStates=3;
  4. int states[6];
  5. int stateDelays[6];
  6. int currentState;
  7.  
  8. bool A;//marks which set of lights are being cycled through currently
  9. int MAX;//index for the 3rd light in the current set of lights
  10. int MIN;//index for the 1st light in the current set of lights
  11.  
  12. void setup() {
  13. states[0] = 36;//red 1 + red 2
  14. states[1] = 34;//green 1 + red 2
  15. states[2] = 33;//amber 1 + red 2
  16. states[3] = 36;//red 2 + red 1
  17. states[4] = 20;//green 2 + red 1
  18. states[5] = 12;//amber 2 + red 1
  19.  
  20. //setup the first set of lights (index 0 to 2 in the states array)
  21. A = true;
  22. MAX = 2;
  23. MIN = 0;
  24.  
  25. stateDelays[0] = 2;//red and red combo
  26. stateDelays[1] = 10;//green and red combo
  27. stateDelays[2] = 2;//amber and red combo
  28. stateDelays[3] = 2;//red and red combo
  29. stateDelays[4] = 10;//red and green combo
  30. stateDelays[5] = 2;//red and amber combo
  31.  
  32. DDRB = DDRB | B00111111; //set Arduino pins 8 to 13 as output
  33. int currentState = 0;//set the first state to 0
  34. PORTB = states[currentState];
  35. }
  36.  
  37. void loop() {
  38. //assign the current state to port B
  39. PORTB = states[currentState];
  40. //wait in the current state for specified time
  41. delay(stateDelays[currentState]*1000);
  42. //call function to get the new state
  43. currentState = transition(currentState);
  44. }
  45.  
  46. int transition(int currentState){
  47. //if the current state is the last index in the current set of lights
  48. if(currentState == MAX){
  49. if(A){
  50. //switch to the second set of lights
  51. MAX = 5;
  52. MIN = 3;
  53. A = false;
  54. }
  55. else{
  56. //switch to the first set of lights
  57. MAX = 2;
  58. MIN = 0;
  59. A = true;
  60. }
  61. //assign the current state to the first index in the next set of lights
  62. currentState=MIN;
  63. }else{
  64. currentState++;//otherwise transition normally
  65. }
  66. return currentState;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement