Advertisement
Combreal

arduino_wheeloffortune.ino

Jan 14th, 2020
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. //Wheel of Fortune
  2. //Version : 01
  3. //MJ - 14-01-2020
  4.  
  5. // Define Pins
  6. int delayTime = 50;
  7. int LEDS[10];
  8. int buttonpin = 13;
  9. bool startRolling = false;
  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.   //digitalWrite(LEDS[9], HIGH);
  34.   //Serial.println(millis()/500);
  35.   //delay(500);
  36.   delay(delayTime / 2);
  37.   //digitalWrite(LEDS[9], HIGH);
  38.   if (digitalRead(buttonpin) == LOW)
  39.   {
  40.     startRolling = true;
  41.   }
  42.   /*else
  43.   {
  44.     shutALlDels();
  45.     startRolling = false;
  46.     //Serial.print(LEDS[y]);
  47.     //Serial.println();
  48.   }*/
  49.   if (startRolling)
  50.   {
  51.     rollLoop();
  52.     startRolling = false;
  53.   }
  54. }
  55.  
  56. void shiftTab(int tab[], int tabSize, int pos)
  57. {
  58.   int teb[10];
  59.   int counter = 0;
  60.   int counterB = 0;
  61.   for (int i = pos; i < tabSize; i++)
  62.   {
  63.     teb[counter] = tab[i];
  64.     counter++;
  65.   }
  66.   if (counter != tabSize)
  67.   {
  68.     counterB = counter;
  69.     for (int i = 0; i < tabSize - counter; i++)
  70.     {
  71.       teb[counterB] = tab[i];
  72.       counterB++;
  73.     }
  74.   }
  75.   for (int i = 0; i < tabSize; i++)
  76.   {
  77.     tab[i] = teb[i];
  78.   }
  79. }
  80.  
  81. void rollLoop()
  82. {
  83.   //get random number 0 to 9 and set starting led for actually random result
  84.   int lastDel = 0;
  85.   while (delayTime <= 700)
  86.   {
  87.     int x;
  88.     delay(delayTime / 2);
  89.     digitalWrite(LEDS[9], LOW);
  90.     digitalWrite(LEDS[0], HIGH);
  91.     for (x = 1; x <= 9; x++)
  92.     {
  93.       delay(delayTime);
  94.       digitalWrite(LEDS[x - 1], LOW);
  95.       digitalWrite(LEDS[x], HIGH);
  96.     }
  97.     if (delayTime == 650)
  98.     {
  99.       delay(delayTime / 2);
  100.       digitalWrite(LEDS[x - 1], LOW);
  101.       delay(delayTime / 2);
  102.       digitalWrite(LEDS[x - 1], HIGH);
  103.       delay(delayTime / 2);
  104.       digitalWrite(LEDS[x - 1], LOW);
  105.       delay(delayTime / 2);
  106.       digitalWrite(LEDS[x - 1], HIGH);
  107.       delay(2500);
  108.     }
  109.     delayTime = delayTime + 200;
  110.   }
  111. }
  112.  
  113. void shutALlDels()//for some odd reasons using for loop here create strange behavior - last leds is half unpowered
  114. {
  115.   //need to fix with for loop
  116.   for (int i = 0; i <= 10; i++)
  117.   {
  118.     digitalWrite(LEDS[i], LOW);
  119.   }
  120.   /*digitalWrite(LEDS[0], LOW);
  121.   digitalWrite(LEDS[1], LOW);
  122.   digitalWrite(LEDS[2], LOW);
  123.   digitalWrite(LEDS[3], LOW);
  124.   digitalWrite(LEDS[4], LOW);
  125.   digitalWrite(LEDS[5], LOW);
  126.   digitalWrite(LEDS[6], LOW);
  127.   digitalWrite(LEDS[7], LOW);
  128.   digitalWrite(LEDS[8], LOW);
  129.   digitalWrite(LEDS[9], LOW);*/
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement