here’s some text
Code:
const int LED_BIT_1 = 12; // I designated the LEDS to each of the respective PINS:
const int LED_BIT_2 = 11; // Feel free to change the PINS to whatever of your choosing!
const int LED_BIT_4 = 10; //
const int LED_BIT_6 = 9; //
const int PAUSE = 2000; // 2000ms, or 2 seconds, a good representation as a length of time to show the Face of the dice.
const int BUTTON_PIN = 7; // Again, I designated the Button to be on PIN 7. Feel free to change to whatever is easiest for you.
int currentButtonValue = 0;
int previousButtonValue = 0;
void setup() {
pinMode(LED_BIT_1, OUTPUT); // Center PIP lights up
pinMode(LED_BIT_2, OUTPUT); // UR + DL PIPS light up
pinMode(LED_BIT_4, OUTPUT); // UL + DR PIPS light up
pinMode(LED_BIT_6, OUTPUT); // CL + CR PIPS light up
pinMode(BUTTON_PIN, INPUT); // This the button, used as an input.
// Seed the random number generator with analogRead
randomSeed(analogRead(A0));
}
void loop() {
currentButtonValue = digitalRead(BUTTON_PIN); // Read the Button state
switch (currentButtonValue) {
case LOW:
// Button is not pressed, turn off all the LEDS
digitalWrite(LED_BIT_1, LOW);
digitalWrite(LED_BIT_2, LOW);
digitalWrite(LED_BIT_4, LOW);
digitalWrite(LED_BIT_6, LOW);
break;
case HIGH:
// Button is pressed, control LEDs based on switch-case
int randomNumber = random(1, 7); // Generate a random number between 1 and 6
switch (randomNumber) {
case 1:
// Dice rolls a "1": Center PIP lights up
digitalWrite(LED_BIT_1, HIGH);
digitalWrite(LED_BIT_2, LOW);
digitalWrite(LED_BIT_4, LOW);
digitalWrite(LED_BIT_6, LOW);
break;
case 2:
// Dice rolls a "2": Upper Right and Bottom Left PIPS light up
digitalWrite(LED_BIT_1, LOW);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, LOW);
digitalWrite(LED_BIT_6, LOW);
break;
case 3:
// Dice rolls a "3": Upper Right and Bottom Left PIPS light up, as well as the Center PIP.
digitalWrite(LED_BIT_1, HIGH);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, LOW);
digitalWrite(LED_BIT_6, LOW);
break;
case 4:
// Dice rolls a "4": Upper Left and Bottom Right PIPS light up, in addition to case 2: (Upper Right and Bottom Left PIPS light up.)
digitalWrite(LED_BIT_1, LOW);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, HIGH);
digitalWrite(LED_BIT_6, LOW);
break;
case 5:
// Dice rolls a "5": Upper Left and Bottom Right PIPS light up, in addition to case 2: (Upper Right and Bottom Left PIPS light up.)
// the Center PIP lights up, too.
digitalWrite(LED_BIT_1, HIGH);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, HIGH);
digitalWrite(LED_BIT_6, LOW);
break;
case 6:
// Dice rolls a "6": Upper Left and Bottom Right PIPS light up, in addition to case 2: (Upper Right and Bottom Left PIPS light up.)
// the Center Left/Right PIPS light up, too.
digitalWrite(LED_BIT_1, LOW);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, HIGH);
digitalWrite(LED_BIT_6, HIGH);
break;
}
delay(PAUSE); // Delay for PAUSE value listed above (default: 2000 ms, or 2 seconds)
}
}
/* Visual LOGIC for 1D6 rolls: 1, 2, 3, 4, 5, 6
// The code below was used for TESTING/logic purposes, only, to make sure a cycled 1-6 would work!
void loop() {
// LED pattern for case 1
digitalWrite(LED_BIT_1, HIGH);
digitalWrite(LED_BIT_2, LOW);
digitalWrite(LED_BIT_4, LOW);
digitalWrite(LED_BIT_6, LOW);
delay(PAUSE);
// LED pattern for case 2
digitalWrite(LED_BIT_1, LOW);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, LOW);
digitalWrite(LED_BIT_6, LOW);
delay(PAUSE);
// LED pattern for case 3
digitalWrite(LED_BIT_1, HIGH);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, LOW);
digitalWrite(LED_BIT_6, LOW);
delay(PAUSE);
// LED pattern for case 4
digitalWrite(LED_BIT_1, LOW);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, HIGH);
digitalWrite(LED_BIT_6, LOW);
delay(PAUSE);
// LED pattern for case 5
digitalWrite(LED_BIT_1, HIGH);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, HIGH);
digitalWrite(LED_BIT_6, LOW);
delay(PAUSE);
// LED pattern for case 6
digitalWrite(LED_BIT_1, LOW);
digitalWrite(LED_BIT_2, HIGH);
digitalWrite(LED_BIT_4, HIGH);
digitalWrite(LED_BIT_6, HIGH);
delay(PAUSE);
}
*/