#include <SPI.h> // Include the SPI library for communication
#include <Wire.h> // Include the Wire library for I2C communication
#include <Adafruit_GFX.h> // Include the Adafruit GFX library for graphics
#include <Adafruit_SSD1306.h> // Include the Adafruit SSD1306 library for the OLED display
// screen size
#define Width 128 // Define the width of the screen
#define Height 64 // Define the height of the screen
// I2C Address of Screen
#define Address 0x3C // Define the I2C address of the OLED display
// Screen reset pin
#define OLED_RESET 1 // Define the reset pin for the OLED display
const int rollPin = 3; // Define the pin for the roll button
const int changeDiePin = 2; // Define the pin for the change die button
int randomNum; // Variable to store the random number
int currDie = 2; // Variable to store the current die index
int diceArray[] = {4, 6, 8, 10, 12, 20}; // Array of dice sides
unsigned long lastInteractionTime = 0; // Variable to store the last interaction time
const unsigned long timeout = 10000; // 10 seconds timeout
bool screenOff = false; // Flag to track if the screen is off
Adafruit_SSD1306 oled(Width, Height); // Create an OLED display object
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
//reads random noise to generate a different sequence each time
randomSeed(analogRead(0)); // Seed the random number generator with analog noise
oled.begin(SSD1306_SWITCHCAPVCC, Address); // Initialize the OLED display
oled.fillScreen(BLACK); // Clear the screen
oled.setTextColor(WHITE); // Set the text color to white
oled.setTextSize(2); // Set the text size to 2
oled.println("e-Dice"); // Print "e-Dice" on the screen
oled.display(); // Display the text
delay(1000); // Wait for 1 second
oled.fillScreen(BLACK); // Clear the screen
oled.display(); // Update the display
pinMode(rollPin, INPUT); // Set the roll button pin as input
pinMode(changeDiePin, INPUT); // Set the change die button pin as input
}
void loop() {
if (digitalRead(changeDiePin) == HIGH) { // Check if the change die button is pressed
delay(50); // Debounce delay
if (digitalRead(changeDiePin) == HIGH) { // Check again if the change die button is pressed
currDie++; // Increment the current die index
randomNum = 0; // Reset the random number
if (currDie > 5) { // If the current die index exceeds the array size
currDie = 0; // Reset the current die index to 0
}
oled.fillScreen(BLACK); // Clear the screen
oled.setCursor(0, 0); // Set the cursor to the top-left corner
//showDie();
oled.setTextSize(4); // Set the text size to 4
oled.print("Dice: d"); // Print "Dice: d" on the screen
oled.print(diceArray[currDie]); // Print the current die sides on the screen
oled.display(); // Update the display
lastInteractionTime = millis(); // Reset the timer
screenOff = false; // Set the screen off flag to false
}
}
if (digitalRead(rollPin) == HIGH) { // Check if the roll button is pressed
delay(100); // Debounce delay
oled.setTextSize(4); // Set the text size to 4
oled.fillScreen(BLACK); // Clear the screen
oled.drawRect(32, 0, 64, 64, WHITE);
oled.display(); // Update the display
randomNum = random(1, diceArray[currDie] + 1); // Generate a random number based on the current die
lastInteractionTime = millis(); // Reset the timer
screenOff = false; // Set the screen off flag to false
}
if (randomNum != NULL && randomNum != 0) { // Check if a random number is generated
if (randomNum < 10) { // If the random number is less than 10
oled.setCursor(59, 23); // Set the cursor position for single-digit numbers
} else {
oled.setCursor(43, 23); // Set the cursor position for double-digit numbers
}
oled.println(randomNum); // Print the random number on the screen
oled.display(); // Update the display
}
// Check if timeout has occurred
if (millis() - lastInteractionTime > timeout && !screenOff) { // If the timeout has occurred and the screen is not off
oled.fillScreen(BLACK); // Clear the screen
oled.display(); // Update the display
screenOff = true; // Set the screen off flag to true
}
}