ELECTRO MOTIF
Published

Normal Dice vs Smart Dice 😎 (ESP32 Build)

Traditional dice are replaced by Smart Dice, which utilizes ESP32, OLED animation, and buIt simulates real rolling using embedded logic.

BeginnerWork in progress65
Normal Dice vs Smart Dice 😎 (ESP32 Build)

Things used in this project

Story

Read more

Code

DrawDice.ino

C/C++
Renders dice face (1–6) on OLED display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Pins
#define BUTTON_PIN 32
#define BUZZER_PIN 4

// Dice position
#define DICE_SIZE 40
#define DICE_X 44
#define DICE_Y 12

bool lastButtonState = HIGH;

// ---------------- DRAW FUNCTIONS ----------------
void drawDot(int x, int y) {
  display.fillCircle(x, y, 3, SSD1306_WHITE);
}

void drawDice(int num) {
  display.drawRect(DICE_X, DICE_Y, DICE_SIZE, DICE_SIZE, SSD1306_WHITE);

  int cx = DICE_X + DICE_SIZE / 2;
  int cy = DICE_Y + DICE_SIZE / 2;
  int offset = 10;

  switch(num) {
    case 1: drawDot(cx, cy); break;

    case 2:
      drawDot(cx - offset, cy - offset);
      drawDot(cx + offset, cy + offset);
      break;

    case 3:
      drawDot(cx, cy);
      drawDot(cx - offset, cy - offset);
      drawDot(cx + offset, cy + offset);
      break;

    case 4:
      drawDot(cx - offset, cy - offset);
      drawDot(cx + offset, cy - offset);
      drawDot(cx - offset, cy + offset);
      drawDot(cx + offset, cy + offset);
      break;

    case 5:
      drawDot(cx, cy);
      drawDot(cx - offset, cy - offset);
      drawDot(cx + offset, cy - offset);
      drawDot(cx - offset, cy + offset);
      drawDot(cx + offset, cy + offset);
      break;

    case 6:
      drawDot(cx - offset, cy - offset);
      drawDot(cx + offset, cy - offset);
      drawDot(cx - offset, cy);
      drawDot(cx + offset, cy);
      drawDot(cx - offset, cy + offset);
      drawDot(cx + offset, cy + offset);
      break;
  }
}

// ---------------- ROLL FUNCTION ----------------
void rollDice() {
  int finalNumber = random(1, 7);

  for (int i = 0; i < 20; i++) {

    display.clearDisplay();

    int randomNum = random(1, 7);
    drawDice(randomNum);

    display.display();

    // Buzzer ON (tone effect)
    digitalWrite(BUZZER_PIN, HIGH);
    delay(50);
    digitalWrite(BUZZER_PIN, LOW);
    delay(50);
  }

  // Final result
  display.clearDisplay();
  drawDice(finalNumber);
  display.display();

  // Ensure buzzer OFF
  digitalWrite(BUZZER_PIN, LOW);
}

// ---------------- SETUP ----------------
void setup() {
  Wire.begin(21, 22);

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    while(true);
  }

  display.clearDisplay();
  randomSeed(analogRead(0));
}

// ---------------- LOOP ----------------
void loop() {
  bool buttonState = digitalRead(BUTTON_PIN);

  // Detect button press (falling edge)
  if (lastButtonState == HIGH && buttonState == LOW) {
    rollDice();
  }

  lastButtonState = buttonState;
}

Credits

ELECTRO MOTIF
6 projects β€’ 0 followers
Electronics enthusiast focused on PCB design, power electronics, and embedded hardware. I design circuits and share hardware projects.

Comments