The Halal Dice — Arduino Mega simulation

An interactive Arduino Mega circuit simulation you can run free in your browser on Velxio, by tareq-neoaz.

Components

Sketch code

#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>

// --- PIN DEFINITIONS ---
#define PIN_MATRIX1 2
#define PIN_MATRIX2 3
#define BTN_ROLL 4
#define BTN_MODE 5

// --- 32x32 MATRIX SETUP ---
Adafruit_NeoMatrix matrix1 = Adafruit_NeoMatrix(32, 32, PIN_MATRIX1,
  NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
  NEO_MATRIX_ROWS    + NEO_MATRIX_PROGRESSIVE,
  NEO_GRB            + NEO_KHZ800);

Adafruit_NeoMatrix matrix2 = Adafruit_NeoMatrix(32, 32, PIN_MATRIX2,
  NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
  NEO_MATRIX_ROWS    + NEO_MATRIX_PROGRESSIVE,
  NEO_GRB            + NEO_KHZ800);

int currentMode = 1; 
int currentDice1 = 1; 
int currentDice2 = 1;

// Fast HSV to RGB converter for the neon border
uint16_t ColorHSV(Adafruit_NeoMatrix &mat, uint16_t hue, uint8_t sat, uint8_t val) {
  uint8_t r, g, b;
  uint8_t region = hue / 43;
  uint8_t remainder = (hue - (region * 43)) * 6; 
  uint8_t p = (val * (255 - sat)) / 255;
  uint8_t q = (val * (255 - ((sat * remainder) / 255))) / 255;
  uint8_t t = (val * (255 - ((sat * (255 - remainder)) / 255))) / 255;

  switch (region) {
    case 0: r = val; g = t; b = p; break;
    case 1: r = q; g = val; b = p; break;
    case 2: r = p; g = val; b = t; break;
    case 3: r = p; g = q; b = val; break;
    case 4: r = t; g = p; b = val; break;
    default: r = val; g = p; b = q; break;
  }
  return mat.Color(r, g, b);
}

void drawDice(Adafruit_NeoMatrix &mat, int num, uint16_t color) {
  mat.fillScreen(0); // Black background
  
  // 1. Draw the Neon Rainbow Outer Border (Row 0, Row 31, Col 0, Col 31)
  uint16_t hueOffset = millis() / 10;
  for (int i = 0; i < 32; i++) {
    mat.drawPixel(i, 0, ColorHSV(mat, (i * 8 + hueOffset) % 255, 255, 255)); // Top
    mat.drawPixel(i, 31, ColorHSV(mat, (i * 8 + hueOffset) % 255, 255, 255)); // Bottom
    mat.drawPixel(0, i, ColorHSV(mat, (i * 8 + hueOffset) % 255, 255, 255)); // Left
    mat.drawPixel(31, i, ColorHSV(mat, (i * 8 + hueOffset) % 255, 255, 255)); // Right
  }

  // 2. Draw the Solid Inner Border (Inset by 2 pixels)
  mat.drawRect(2, 2, 28, 28, color); 

  // 3. Draw the Pips
  int p1_x = 8,  p1_y = 8;   
  int p2_x = 24, p2_y = 8;   
  int p3_x = 8,  p3_y = 24;  
  int p4_x = 24, p4_y = 24;  
  int p5_x = 16, p5_y = 16;  
  int r = 3; 

  if (num == 1) {
    mat.fillCircle(p5_x, p5_y, r, color);
  } 
  else if (num == 2) {
    mat.fillCircle(p1_x, p1_y, r, color);
    mat.fillCircle(p4_x, p4_y, r, color);
  } 
  else if (num == 3) {
    mat.fillCircle(p1_x, p1_y, r, color);
    mat.fillCircle(p5_x, p5_y, r, color);
    mat.fillCircle(p4_x, p4_y, r, color);
  } 
  else if (num == 4) {
    mat.fillCircle(p1_x, p1_y, r, color);
    mat.fillCircle(p2_x, p2_y, r, color);
    mat.fillCircle(p3_x, p3_y, r, color);
    mat.fillCircle(p4_x, p4_y, r, color);
  } 
  else if (num == 5) {
    mat.fillCircle(p1_x, p1_y, r, color);
    mat.fillCircle(p2_x, p2_y, r, color);
    mat.fillCircle(p5_x, p5_y, r, color);
    mat.fillCircle(p3_x, p3_y, r, c

More projects by tareq-neoaz