Advertisement
Combreal

arduino_wheeloffortunev2.ino

Jan 19th, 2020
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. //Wheel of Fortune
  2. //Version : 02
  3. //MJ - 19-01-2020
  4.  
  5. int delayTime = 50;
  6. int LEDS[10];
  7. int buttonpin = 13;
  8. bool startRolling = false;
  9. bool firstBatch = true;
  10. long randNumber;
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.   pinMode(buttonpin, INPUT_PULLUP);
  16.   randomSeed(analogRead(0));
  17.   randNumber = random(0, 9);
  18.   for (int i = 0; i <= 10; i++)
  19.   {
  20.     LEDS[i] = 2 + i;
  21.   }
  22.   shiftTab(LEDS, 10, randNumber);
  23.   for (int j = 0; j <= 10; j++)
  24.   {
  25.     pinMode(LEDS[j], OUTPUT);
  26.     digitalWrite(LEDS[j], LOW);
  27.   }
  28. }
  29.  
  30. void loop()
  31. {
  32.   startRolling = false;
  33.   delay(delayTime / 2);
  34.   if (digitalRead(buttonpin) == LOW)
  35.   {
  36.     if(!firstBatch)
  37.     {
  38.       resetDraw();
  39.     }
  40.     startRolling = true;
  41.   }
  42.   else
  43.   {
  44.     startRolling = false;
  45.   }
  46.   if (startRolling)
  47.   {
  48.     rollLoop();
  49.   }
  50. }
  51.  
  52. void shiftTab(int tab[], int tabSize, int pos)
  53. {
  54.   int teb[10];
  55.   int counter = 0;
  56.   int counterB = 0;
  57.   for (int i = pos; i < tabSize; i++)
  58.   {
  59.     teb[counter] = tab[i];
  60.     counter++;
  61.   }
  62.   if (counter != tabSize)
  63.   {
  64.     counterB = counter;
  65.     for (int i = 0; i < tabSize - counter; i++)
  66.     {
  67.       teb[counterB] = tab[i];
  68.       counterB++;
  69.     }
  70.   }
  71.   for (int i = 0; i < tabSize; i++)
  72.   {
  73.     tab[i] = teb[i];
  74.   }
  75. }
  76.  
  77. void resetDraw()
  78. {
  79.   shutALlDels();
  80.   randNumber = random(0, 9);
  81.   for (int i = 0; i <= 10; i++)
  82.   {
  83.     LEDS[i] = 2 + i;
  84.   }
  85.   shiftTab(LEDS, 10, randNumber);
  86.   for (int j = 0; j <= 10; j++)
  87.   {
  88.     pinMode(LEDS[j], OUTPUT);
  89.     digitalWrite(LEDS[j], LOW);
  90.   }
  91.   delayTime = 50;
  92. }
  93.  
  94. void rollLoop()
  95. {
  96.   int lastDel = 0;
  97.   while (delayTime <= 650)
  98.   {
  99.     int x;
  100.     delay(delayTime / 2);
  101.     digitalWrite(LEDS[9], LOW);
  102.     digitalWrite(LEDS[0], HIGH);
  103.     for (x = 1; x <= 9; x++)
  104.     {
  105.       delay(delayTime);
  106.       digitalWrite(LEDS[x - 1], LOW);
  107.       digitalWrite(LEDS[x], HIGH);
  108.       if(x == 5)
  109.       {
  110.         delayTime = delayTime + 200;
  111.       }
  112.     }
  113.     if (delayTime == 650)
  114.     {
  115.       delay(delayTime / 2);
  116.       digitalWrite(LEDS[x - 1], LOW);
  117.       delay(delayTime / 2);
  118.       digitalWrite(LEDS[x - 1], HIGH);
  119.       delay(delayTime / 2);
  120.       digitalWrite(LEDS[x - 1], LOW);
  121.       delay(delayTime / 2);
  122.       digitalWrite(LEDS[x - 1], HIGH);
  123.       firstBatch = false;
  124.       break;
  125.     }
  126.   }
  127. }
  128.  
  129. void shutALlDels()
  130. {
  131.   for (int i = 0; i <= 10; i++)
  132.   {
  133.     digitalWrite(LEDS[i], LOW);
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement