/*
* NOTE: This does not include the keyboard stuff because of wokwi limitations.
*/
#define R 1
#define G 5
#define B 9
#define BTN 12
int randomNum = 0;
int colorArr[] = {0, 127, 255};
bool hasSpun = false; // prevents repeating
void setup() {
Serial.begin(115200);
pinMode(BTN, INPUT_PULLUP); // use pull-up for stable reading
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
randomSeed(analogRead(A0)); // real randomness
}
void loop() {
// Button pressed (active LOW)
if (digitalRead(BTN) == LOW && !hasSpun) {
hasSpun = true; // mark that spin has started
randomNum = random(0, 7); // 7 possible outcomes
Serial.print("Spinning... randomNum = ");
Serial.println(randomNum);
cycleColors(randomNum);
stopOnColor(randomNum);
roulette(randomNum);
}
// Reset flag when button released
if (digitalRead(BTN) == HIGH && hasSpun) {
hasSpun = false; // ready for next press
}
}
void cycleColors(int finalColor) {
unsigned long delayTime = 20; // starting delay (fast)
unsigned long maxDelay = 400; // slowest delay
float slowdownFactor = 1.08; // realistic friction slowdown
int totalSteps = 15 + random(0, 20); // random spin length
for (int step = 0; step < totalSteps; step++) {
analogWrite(R, colorArr[random(0, 3)]);
analogWrite(G, colorArr[random(0, 3)]);
analogWrite(B, colorArr[random(0, 3)]);
delay(delayTime);
delayTime = min((unsigned long)(delayTime * slowdownFactor), maxDelay);
}
}
void stopOnColor(int n) {
int r = 0, g = 0, b = 0;
switch (n) {
case 0: r = 255; break; // Red
case 1: g = 255; break; // Green
case 2: b = 255; break; // Blue
case 3: r = 255; g = 255; break; // Yellow
case 4: g = 255; b = 255; break; // Cyan
case 5: r = 255; b = 255; break; // Magenta
case 6: r = 255; g = 255; b = 255; break; // White
}
// Flash final color 3 times
for (int i = 0; i < 3; i++) {
analogWrite(R, r);
analogWrite(G, g);
analogWrite(B, b);
delay(625);
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 0);
delay(250);
}
// Leave the final color on
analogWrite(R, r);
analogWrite(G, g);
analogWrite(B, b);
}
void roulette(int i) {
Serial.print("Result: ");
switch (i) {
case 0:
Serial.println("RED");
break;
case 1:
Serial.println("GREEN");
break;
case 2:
Serial.println("BLUE");
break;
case 3:
Serial.println("YELLOW");
break;
case 4:
Serial.println("CYAN");
break;
case 5:
Serial.println("MAGENTA");
break;
case 6:
Serial.println("WHITE");
break;
}
}