Project showcase
Arduino Dice roller

Arduino Dice roller © GPL3+

    Designed to be low cost, Arduino Pro Mini Very cheap compact postage size Arduino. driving I2C OLED Display

  • 3,168 views
  • 0 comments
  • 12 respects

Components and supplies

OLED display LED module I2C 128x64
$10 on Ebay, but make sure you get the I2C version
×1
11114 01a
SparkFun Arduino Pro Mini 328 - 3.3V/8MHz
×1
Adafruit industries ada1536 image
Buzzer
Any piezo buzzer will do
×1
Tens70
9V battery (generic)
×1
Keystone 233 image 75px
9V Battery Clip
×1
Tactile switches (buttons)
×3
Resistors
One each: 1k, 2.2k, and 3kOhm.
×3

About this project

Designed to be low cost, Key features

  • Arduino Pro Mini
  • Very cheap compact postage size Arduino. Powered direct by 9v battery to on-board VCC regulater.
  • OLED display using U8glib, on I2C 128X64 OLED  Display
  • “Resistor ladder”  to take three button inputs using one pin.
  • Piezo buzzer to give dice roll sound effect
  • Uses three resistors in resistor ladder to allow all buttons to be read by one Analogue pin (See code for detail)
  • Could do with better case

Code

Arduino Dice Roller CodeArduino
Uses Arduino IDE Language. Display is OLED I2C using U8glib library , uses resistor ladder to allow 3 buttons via one pin.
/*
Final working version on Nano and Pro mini
Rolls 1 to three dice on OLED display
Dice roll ound
Start up screen
Sleep mode for screen and arduino
button input uses one pin resistor ladder of  2.2k, 1k, 3.3k  
 Memory: Membase fremem nano=1828 ; this prog 1517  used = 311
 
 */

#include "U8glib.h"
#include "MemoryFree.h";
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST);	// Fast I2C / TWI 
char button = A0;  // pushbutton  pin
byte ndice;
int band1 = 100;
int band2 =350;
int band3 =700;
byte piezo =13; // piezo buzzer
byte fsize;
byte foffset;
byte foffset2;
byte ccent;
byte cdown;
byte coff;
byte cdot;
byte ran[4];
byte j;
byte screensleep=20; // screen sleeptime in seconds
unsigned long ntime;  // time of last display
void setup() {
  randomSeed(analogRead(1));

  // memory check
   Serial.begin(9600);
 Serial.print(F("freeMemory()="));  // reports free memory
Serial.println(freeMemory());
  ntime=millis();
  welcome();
}

void draw2(void) {
  // graphic commands to redraw the complete screen should be placed here  
   u8g.setFont(u8g_font_6x13);  // 2 lines 10 pixels 
  u8g.drawFrame(foffset2,16,fsize,fsize); 
 
  u8g.drawStr( 0, 30, "Welcome to Dice Roll");
  u8g.drawStr(0, 60, "Press a Button");

}
void sleep1()
 {
 // picture loop
  u8g.firstPage();  
  do {
   u8g_SleepOn; //  put screen to sleep
  } while( u8g.nextPage() );
  
 }


void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g_SleepOff; // if a button is pressed wake up screen
   u8g.setFont(u8g_font_6x13);  // 2 lines 10 pixels 
  u8g.drawFrame(foffset2,16,fsize,fsize); 
if (ran[j+1]== 1 || ran[j+1]== 3 || ran[j+1]== 5) u8g.drawDisc(ccent, cdown, cdot);  // centre dot
if (ran[j+1] > 1) { u8g.drawDisc(ccent-coff, cdown+coff, cdot);  u8g.drawDisc(ccent+coff, cdown-coff, cdot); } // diagonal dots for 2
if (ran[j+1] > 3) {u8g.drawDisc(ccent+coff, cdown+coff, cdot);  u8g.drawDisc(ccent-coff, cdown-coff, cdot); } // diagonal dots for 4
 if (ran[j+1] > 5) { u8g.drawDisc(ccent+coff, cdown, cdot);  u8g.drawDisc(ccent-coff, cdown, cdot); } // diagonal dots for 6
 
  //u8g.drawStr( 0, 20, "Hello World!");
}

void welcome()
{

 // picture loop
  u8g.firstPage();  
  do {
    draw2();
  } while( u8g.nextPage() );
  
  // rebuild the picture after some delay
  delay(1000);
}

void loop() {


 if ((millis()-ntime) > (1000*screensleep)) sleep1(); // after 20 secs put screen to sleep
 
  //check which button is pressed
  int butin = analogRead(button);
//int button = (analogRead(A0)*5*1.5+512)/1024;
if (butin < band1) ndice=1;
else if (butin < band2) ndice=2;
else if (butin < band3) ndice=3;
else ndice=0;

Serial.print(F("button value "));  // reports free memory
Serial.print(butin);
Serial.print(F("button "));  // reports free memory
//delay(1000);
Serial.println(ndice);

 
if (ndice > 0) 
{

ntime=millis(); // and reset sleep timer

 for (byte rr = 0; rr < 20; rr++) { // generate 10 dice to show spin  
  for (byte kk = 0; kk < ndice+1; kk++) { ran[kk] = int (random(1, 7));}
 
// dice roll sound?? 
 for (byte tt = 0; tt < 1; tt++) {
                                  tone (piezo,65,10); // orig tone (3,65,10);(3,120,10);
                                   delay (90);
                                   digitalWrite (piezo,LOW);
                                   }
                                   
       // picture loop      
   // dice generation
            u8g.firstPage();  
            do {
           
                     fsize = 48;
                     if (ndice==3) fsize=44;
                     foffset = (127-(ndice*fsize)-ndice)/2;            
                     for ( j = 0; j < ndice; j++)
                         {   // set up dice parameters
                           foffset2=4+(foffset+fsize)*j; 
                          if (ndice==1) foffset2=24;      //foffset2*2;  
                          ccent=foffset2+(fsize/2); 
                          cdown=16+(fsize/2);
                          coff=11;  if (ndice==3) coff=10;
                          cdot=4;   if (ndice==3) cdot=3;  
                          draw();  
                         }  
                 
          } while( u8g.nextPage() );
            //  delay(30);
    
 }
 } // end of rr


} // end loop

Schematics

Dice Roller Schematic
Note values of resistors are 2.2k, 1k and 3.3k
Dice%203 bb
Dice Roller Schematic
Note values of resistors are 2.2k, 1k and 3.3k
Dice%203 bb

Comments

Similar projects you might like

LED Dice

Project showcase by EvdS

  • 14,276 views
  • 11 comments
  • 49 respects

Settlers Of Catan Dice

Project tutorial by joshi

  • 5,841 views
  • 13 comments
  • 28 respects

LED Dice

Project showcase by Team ONYX

  • 7,463 views
  • 1 comment
  • 17 respects

Digital Dice

Project showcase by SKYMDev

  • 1,598 views
  • 0 comments
  • 0 respects

Smart Dice of Ludo

Project tutorial by msr048

  • 3,035 views
  • 3 comments
  • 11 respects

LED Dice

Project showcase by radekw8733

  • 2,255 views
  • 2 comments
  • 12 respects
Add projectSign up / Login