Advertisement
Benjamin_Loison

DICE

Dec 26th, 2017
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. const byte PIN_INPUT = 2, PIN_WARN = 8, PIN_RUN = 9, PIN_INIT = 10;
  2. const bool DEBUG = true;
  3. #define PIN_LED_SIZE 5
  4. byte PIN_LED[PIN_LED_SIZE];
  5. bool printed;
  6.  
  7. void setup()
  8. {
  9.     pinMode(PIN_INIT, OUTPUT);
  10.  
  11.         digitalWrite(PIN_INIT, HIGH);
  12.    
  13.     Serial.begin(9600);
  14.     Serial.println("INITIALIZING...");
  15.    
  16.     pinMode(PIN_INPUT, INPUT_PULLUP);
  17.    
  18.     for(byte led = 0; led < PIN_LED_SIZE; led++)
  19.     {
  20.         PIN_LED[led] = led + 3;
  21.         pinMode(PIN_LED[led], OUTPUT);
  22.     }
  23.     pinMode(PIN_WARN, OUTPUT);
  24.     pinMode(PIN_RUN, OUTPUT);
  25.    
  26.     printed = false;
  27.  
  28.     randomSeed(analogRead(0));
  29.  
  30.         delay(3000);
  31.         digitalWrite(PIN_INIT, LOW);
  32.        
  33.     Serial.println("INITIALIZED !");
  34. }
  35.  
  36.  
  37. void loop()
  38. {
  39.     if(DEBUG)
  40.     {
  41.         delay(50);
  42.         digitalWrite(PIN_RUN, HIGH);
  43.         delay(50);
  44.         digitalWrite(PIN_RUN, LOW);
  45.     }
  46.    
  47.     if(!digitalRead(PIN_INPUT))
  48.     {
  49.         if(!printed)
  50.         {
  51.             loading();
  52.             printed = true;
  53.             Serial.println("PUSHED");
  54.             light(random(1, 6));
  55.         }
  56.     }
  57.     else if(printed)
  58.         printed = false;
  59. }
  60.  
  61. void setAll(bool state)
  62. {
  63.     for(byte led = 0; led < PIN_LED_SIZE; led++)
  64.         digitalWrite(PIN_LED[led], state);
  65. }
  66.  
  67. void loading()
  68. {
  69.     for(byte load = 1; load < 75; load++)
  70.     {
  71.         light(random(1, 6));
  72.         delay(1500 / load);
  73.     }
  74.     setAll(LOW);
  75.     delay(500);
  76. }
  77.  
  78. void light(byte number)
  79. {
  80.     setAll(LOW);
  81.     if(number == 1)
  82.         digitalWrite(PIN_LED[2], HIGH);
  83.     else if(number == 2)
  84.     {
  85.         digitalWrite(PIN_LED[1], HIGH);
  86.         digitalWrite(PIN_LED[4], HIGH);
  87.     }
  88.     else if(number == 3)
  89.     {
  90.         light(2);
  91.         digitalWrite(PIN_LED[2], HIGH);
  92.     }
  93.     else if(number == 4)
  94.     {
  95.         light(2);
  96.         digitalWrite(PIN_LED[0], HIGH);
  97.         digitalWrite(PIN_LED[3], HIGH);
  98.     }
  99.     else if(number == 5)
  100.         setAll(HIGH);
  101.     else
  102.         warn();
  103. }
  104.  
  105. void warn()
  106. {
  107.     while(1)
  108.     {
  109.         digitalWrite(PIN_WARN, HIGH);
  110.         delay(50);
  111.         digitalWrite(PIN_WARN, LOW);
  112.         delay(50);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement