farhansadaf

Traffic light system

Dec 9th, 2020
617
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int delayMins = 2  // Delay in minuites between states
  2.  
  3. /*
  4.   INDEXES
  5.     0 -> North
  6.     1 -> East
  7.     2 -> South
  8.     3 -> West
  9. */
  10. int dataPins[] = [0, 3, 6, 9];  
  11. int latchPins[] = [1, 4, 7, 10];  
  12. int clockPins[] = [2, 5, 8, 11];  
  13.  
  14. byte leds[] = [0, 0, 0, 0];
  15.  
  16. void setup()
  17. {
  18.   for (int i = 0; i < 4; i++)
  19.   {
  20.     pinMode(latchPins[i], OUTPUT);
  21.   }
  22.  
  23.   for (int i = 0; i < 4; i++)
  24.   {
  25.     pinMode(dataPins[i], OUTPUT);
  26.   }
  27.  
  28.   for (int i = 0; i < 4; i++)
  29.   {
  30.     pinMode(clockPins[i], OUTPUT);
  31.   }
  32.  
  33. }
  34.  
  35. void updateShiftRegister(int dir)
  36. {
  37.   digitalWrite(latchPins[dir], LOW);
  38.   shiftOut(dataPins[dir], clockPins[dir], LSBFIRST, leds[dir]);
  39.   digitalWrite(latchPins[dir], HIGH);
  40. }
  41.  
  42. void loop()
  43. {
  44.   // State 1
  45.   leds[0] = B00110010;  // North
  46.   updateShiftRegister(0);
  47.   leds[1] = B00001000;  // East
  48.   updateShiftRegister(1);
  49.   leds[2] = B00001000;  // South
  50.   updateShiftRegister(2);
  51.   leds[3] = B00001000;  // West
  52.   updateShiftRegister(3);
  53.   delay(delayMins * 60 * 1000); // Desired time delay
  54.  
  55.   // State 2
  56.   leds[0] = B00110110;  // North
  57.   updateShiftRegister(0);
  58.   leds[1] = B00001000;  // East
  59.   updateShiftRegister(1);
  60.   leds[2] = B00001100;  // South
  61.   updateShiftRegister(2);
  62.   leds[3] = B00001000;  // West
  63.   updateShiftRegister(3);
  64.   delay(delayMins * 60 * 1000); // Desired time delay
  65.  
  66.   // State 3
  67.   leds[0] = B00001000;  // North
  68.   updateShiftRegister(0);
  69.   leds[1] = B00001000;  // East
  70.   updateShiftRegister(1);
  71.   leds[2] = B00110010;  // South
  72.   updateShiftRegister(2);
  73.   leds[3] = B00001000;  // West
  74.   updateShiftRegister(3);
  75.   delay(delayMins * 60 * 1000); // Desired time delay
  76.  
  77.   // State 4
  78.   leds[0] = B00001000;  // North
  79.   updateShiftRegister(0);
  80.   leds[1] = B00001100;  // East
  81.   updateShiftRegister(1);
  82.   leds[2] = B00110110;  // South
  83.   updateShiftRegister(2);
  84.   leds[3] = B00001000;  // West
  85.   updateShiftRegister(3);
  86.   delay(delayMins * 60 * 1000); // Desired time delay
  87.  
  88.   // State 5
  89.   leds[0] = B00001000;  // North
  90.   updateShiftRegister(0);
  91.   leds[1] = B00110010;  // East
  92.   updateShiftRegister(1);
  93.   leds[2] = B00001000;  // South
  94.   updateShiftRegister(2);
  95.   leds[3] = B00001000;  // West
  96.   updateShiftRegister(3);
  97.   delay(delayMins * 60 * 1000); // Desired time delay
  98.  
  99.   // State 6
  100.   leds[0] = B00001000;  // North
  101.   updateShiftRegister(0);
  102.   leds[1] = B00110110;  // East
  103.   updateShiftRegister(1);
  104.   leds[2] = B00001000;  // South
  105.   updateShiftRegister(2);
  106.   leds[3] = B00001100;  // West
  107.   updateShiftRegister(3);
  108.   delay(delayMins * 60 * 1000); // Desired time delay
  109.  
  110.   // State 7
  111.   leds[0] = B00001000;  // North
  112.   updateShiftRegister(0);
  113.   leds[1] = B00001000;  // East
  114.   updateShiftRegister(1);
  115.   leds[2] = B00001000;  // South
  116.   updateShiftRegister(2);
  117.   leds[3] = B00110010;  // West
  118.   updateShiftRegister(3);
  119.   delay(delayMins * 60 * 1000); // Desired time delay
  120.  
  121.   // State 8
  122.   leds[0] = B00001100;  // North
  123.   updateShiftRegister(0);
  124.   leds[1] = B00001000;  // East
  125.   updateShiftRegister(1);
  126.   leds[2] = B00001100;  // South
  127.   updateShiftRegister(2);
  128.   leds[3] = B00110110;  // West
  129.   updateShiftRegister(3);
  130.   delay(delayMins * 60 * 1000); // Desired time delay
  131.  
  132.   // State 9
  133.   leds[0] = B00100010;  // North
  134.   updateShiftRegister(0);
  135.   leds[1] = B00001000;  // East
  136.   updateShiftRegister(1);
  137.   leds[2] = B00100010;  // South
  138.   updateShiftRegister(2);
  139.   leds[3] = B00001000;  // West
  140.   updateShiftRegister(3);
  141.   delay(delayMins * 60 * 1000); // Desired time delay
  142.  
  143.   // State 10
  144.   leds[0] = B00100110;  // North
  145.   updateShiftRegister(0);
  146.   leds[1] = B00001100;  // East
  147.   updateShiftRegister(1);
  148.   leds[2] = B00100110;  // South
  149.   updateShiftRegister(2);
  150.   leds[3] = B00001100;  // West
  151.   updateShiftRegister(3);
  152.   delay(delayMins * 60 * 1000); // Desired time delay
  153.  
  154.   // State 11
  155.   leds[0] = B00001000;  // North
  156.   updateShiftRegister(0);
  157.   leds[1] = B00100010;  // East
  158.   updateShiftRegister(1);
  159.   leds[2] = B00001000;  // South
  160.   updateShiftRegister(2);
  161.   leds[3] = B00100010;  // West
  162.   updateShiftRegister(3);
  163.   delay(delayMins * 60 * 1000); // Desired time delay
  164.  
  165.   // State 12
  166.   leds[0] = B00001100;  // North
  167.   updateShiftRegister(0);
  168.   leds[1] = B00100110;  // East
  169.   updateShiftRegister(1);
  170.   leds[2] = B00001100;  // South
  171.   updateShiftRegister(2);
  172.   leds[3] = B00100110;  // West
  173.   updateShiftRegister(3);
  174.   delay(delayMins * 60 * 1000); // Desired time delay
  175.  
  176.   // State 13
  177.   leds[0] = B00010010;  // North
  178.   updateShiftRegister(0);
  179.   leds[1] = B00001000;  // East
  180.   updateShiftRegister(1);
  181.   leds[2] = B00010010;  // South
  182.   updateShiftRegister(2);
  183.   leds[3] = B00001000;  // West
  184.   updateShiftRegister(3);
  185.   delay(delayMins * 60 * 1000); // Desired time delay
  186.  
  187.   // State 14
  188.   leds[0] = B00010110;  // North
  189.   updateShiftRegister(0);
  190.   leds[1] = B00001100;  // East
  191.   updateShiftRegister(1);
  192.   leds[2] = B00010110;  // South
  193.   updateShiftRegister(2);
  194.   leds[3] = B00001100;  // West
  195.   updateShiftRegister(3);
  196.   delay(delayMins * 60 * 1000); // Desired time delay
  197.  
  198.   // State 15
  199.   leds[0] = B00001000;  // North
  200.   updateShiftRegister(0);
  201.   leds[1] = B00010010;  // East
  202.   updateShiftRegister(1);
  203.   leds[2] = B00001000;  // South
  204.   updateShiftRegister(2);
  205.   leds[3] = B00010010;  // West
  206.   updateShiftRegister(3);
  207.   delay(delayMins * 60 * 1000); // Desired time delay
  208.  
  209.   // State 16
  210.   leds[0] = B00001100;  // North
  211.   updateShiftRegister(0);
  212.   leds[1] = B00010110;  // East
  213.   updateShiftRegister(1);
  214.   leds[2] = B00001000;  // South
  215.   updateShiftRegister(2);
  216.   leds[3] = B00010110;  // West
  217.   updateShiftRegister(3);
  218.   delay(delayMins * 60 * 1000); // Desired time delay
  219.  
  220.   // State 17
  221.   leds[0] = B00010011;  // North
  222.   updateShiftRegister(0);
  223.   leds[1] = B00001000;  // East
  224.   updateShiftRegister(1);
  225.   leds[2] = B00001000;  // South
  226.   updateShiftRegister(2);
  227.   leds[3] = B00001000;  // West
  228.   updateShiftRegister(3);
  229.   delay(delayMins * 60 * 1000); // Desired time delay
  230.  
  231.   // State 18
  232.   leds[0] = B00010111;  // North
  233.   updateShiftRegister(0);
  234.   leds[1] = B00001000;  // East
  235.   updateShiftRegister(1);
  236.   leds[2] = B00001100;  // South
  237.   updateShiftRegister(2);
  238.   leds[3] = B00001000;  // West
  239.   updateShiftRegister(3);
  240.   delay(delayMins * 60 * 1000); // Desired time delay
  241.  
  242.   // State 19
  243.   leds[0] = B00001000;  // North
  244.   updateShiftRegister(0);
  245.   leds[1] = B00001000;  // East
  246.   updateShiftRegister(1);
  247.   leds[2] = B00010011;  // South
  248.   updateShiftRegister(2);
  249.   leds[3] = B00001000;  // West
  250.   updateShiftRegister(3);
  251.   delay(delayMins * 60 * 1000); // Desired time delay
  252.  
  253.   // State 20
  254.   leds[0] = B00001000;  // North
  255.   updateShiftRegister(0);
  256.   leds[1] = B00001100;  // East
  257.   updateShiftRegister(1);
  258.   leds[2] = B00010111;  // South
  259.   updateShiftRegister(2);
  260.   leds[3] = B00001000;  // West
  261.   updateShiftRegister(3);
  262.   delay(delayMins * 60 * 1000); // Desired time delay
  263.  
  264.   // State 21
  265.   leds[0] = B00001000;  // North
  266.   updateShiftRegister(0);
  267.   leds[1] = B00010011;  // East
  268.   updateShiftRegister(1);
  269.   leds[2] = B00001000;  // South
  270.   updateShiftRegister(2);
  271.   leds[3] = B00001000;  // West
  272.   updateShiftRegister(3);
  273.   delay(delayMins * 60 * 1000); // Desired time delay
  274.  
  275.   // State 22
  276.   leds[0] = B00001000;  // North
  277.   updateShiftRegister(0);
  278.   leds[1] = B00010111;  // East
  279.   updateShiftRegister(1);
  280.   leds[2] = B00001000;  // South
  281.   updateShiftRegister(2);
  282.   leds[3] = B00001100;  // West
  283.   updateShiftRegister(3);
  284.   delay(delayMins * 60 * 1000); // Desired time delay
  285.  
  286.   // State 23
  287.   leds[0] = B00001000;  // North
  288.   updateShiftRegister(0);
  289.   leds[1] = B00001000;  // East
  290.   updateShiftRegister(1);
  291.   leds[2] = B00001000;  // South
  292.   updateShiftRegister(2);
  293.   leds[3] = B00010011;  // West
  294.   updateShiftRegister(3);
  295.   delay(delayMins * 60 * 1000); // Desired time delay
  296.  
  297.   // State 24
  298.   leds[0] = B00001100;  // North
  299.   updateShiftRegister(0);
  300.   leds[1] = B00001000;  // East
  301.   updateShiftRegister(1);
  302.   leds[2] = B00001000;  // South
  303.   updateShiftRegister(2);
  304.   leds[3] = B00010111;  // West
  305.   updateShiftRegister(3);
  306.   delay(delayMins * 60 * 1000); // Desired time delay
  307.  
  308.   }
  309.  
  310.  
  311.  
RAW Paste Data