ElectroPeak
Published © GPL3+

Smart Dice Tower with Arduino [Board Game Assistant]

Have you ever played board games? Do you like to play with dices? We use dices in many board games, but sometimes they cause problems.

AdvancedFull instructions provided5 hours2,274

Things used in this project

Hardware components

Arduino UNO R3
×1
ElectroPeak 2.4" TFT LCD Display Shield
×1
ElectroPeak KY-037 Sound Detection Sensor Module
×1
5mm RGB Tri-color 4Pin LED - 20 Pcs
×3
3 X 1.5V AAA Battery Box Holder with cover and ON-OFF switch
×1
1.5V AAA Battery
×3
Resistor 330 ohm
Resistor 330 ohm
×3

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Custom parts and enclosures

Dice Tower Drawing

Dice Tower 3D View

Code

Dice Tower Final Arduino Code

Arduino
/*
  Smart Dice Tower - main code
  modified on 25 May 2019
  by Arash Abarghooei
  https://electropeak.com/learn/
*/
 
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
 
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define YP A3  
#define XM A2  
#define YM 9   
#define XP 8 

#define rLED 5
#define gLED 6
#define bLED 7
#define ss 13

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

//find these number through "SDT_TS_calibration.ino" code
#define TSx_dices         600
#define TSy_1st_dice      825
#define TSy_2nd_dice      625
#define TSy_3th_dice      425
#define TSy_4th_dice      225
#define TSy_virtual_dice  760

#define BLACK       0x0000
#define BLUE        0x001F
#define RED         0xF800
#define GREEN       0x07E0
#define CYAN        0x07FF
#define MAGENTA     0xF81F
#define YELLOW      0xFFE0
#define WHITE       0xFFFF
#define ORANGE      0xFD20
#define GREENYELLOW 0xAFE5
#define NAVY        0x000F
#define DARKGREEN   0x03E0
#define DARKCYAN    0x03EF
#define MAROON      0x7800
#define PURPLE      0x780F
#define OLIVE       0x7BE0
#define LIGHTGREY   0xC618
#define DARKGREY    0x7BEF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

int n=1,a=1,t=0,count=0;
int c[4]; 
bool pres=false,vd=false;
 
void setup() {
 
  Serial.begin(9600);
  Serial.println(F("TFT LCD test"));

 tft.reset();
 
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
  Serial.println(F("Using Adafruit 2.4\" TFT Arduino Shield Pinout"));
#else
  Serial.println(F("Using Adafruit 2.4\" TFT Breakout Board Pinout"));
#endif
  Serial.print("TFT size is ");
  Serial.print(tft.width());
  Serial.print("x");
  Serial.println(tft.height());
 
  tft.reset();
 delay(1000);
  uint16_t identifier = tft.readID();
 
  if (identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if (identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if (identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if (identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if (identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Adafruit 2.4\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    return;
  }

  pinMode(13, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  
  tft.begin(identifier);
 
  Serial.println(F("Benchmark                Time (microseconds)"));
//-------------------------------------------------------------------------------------
//LCD starting
  tft.fillScreen(PURPLE);
  tft.setTextColor(YELLOW);
  tft.setRotation(1);
  tft.setCursor(30, 100);
  tft.setTextSize(4);
  tft.println("Electropeak");
  tft.setCursor(60, 150);
  tft.setTextSize(2);
  tft.println("Smart Dice Tower");
  delay(1000);
//-------------------------------------------------------------------------------------
//asking number of player
  tft.fillScreen(PURPLE);
  tft.setRotation(1); 
  tft.setCursor(5, 50);
  tft.setTextSize(3);
  tft.println("number of player?");
  delay(200);
  tft.fillRoundRect(20,110,60,60,5,WHITE);
  tft.fillCircle(50,140,10,RED);
  
  tft.fillRoundRect(90,110,60,60,5,WHITE);
  tft.fillCircle(120,125,8,BLACK);
  tft.fillCircle(120,155,8,BLACK);

  tft.fillRoundRect(160,110,60,60,5,WHITE);
  tft.fillCircle(170,120,8,BLACK);
  tft.fillCircle(190,140,8,BLACK);
  tft.fillCircle(210,160,8,BLACK);

  tft.fillRoundRect(230,110,60,60,5,WHITE);
  tft.fillCircle(245,125,8,BLACK);
  tft.fillCircle(275,125,8,BLACK);
  tft.fillCircle(245,155,8,BLACK);
  tft.fillCircle(275,155,8,BLACK);

while(!pres){
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  if(p.z > ts.pressureThreshhold){
  pres= true;
   
    if (p.x>TSx_dices-100 && p.x<TSx_dices+100){
      if(p.y>TSy_1st_dice-75 && p.y<TSy_1st_dice+75)
      n=1;
      else if(p.y>TSy_2nd_dice-75 && p.y<TSy_2nd_dice+75)
      n=2;
      else if(p.y>TSy_3th_dice-75 && p.y<TSy_3th_dice+75)
      n=3;
      else if(p.y>TSy_4th_dice-75 && p.y<TSy_4th_dice+75)
      n=4;
    }
  }
}
//-------------------------------------------------------------------------------------
//asking players colors
for(int j=0;j<n;j++){
  pres=false;
  tft.fillScreen(PURPLE);
  tft.setRotation(1); 
  tft.setCursor(20, 50);
  tft.setTextSize(3);
  tft.print("player");tft.print(j+1);tft.print(" color?");
  delay(200);
  tft.fillRoundRect(20,110,60,60,5,WHITE);
  tft.fillCircle(50,140,10,BLACK);
  
  tft.fillRoundRect(90,110,60,60,5,RED);
  tft.fillCircle(120,140,10,BLACK);

  tft.fillRoundRect(160,110,60,60,5,GREEN);
  tft.fillCircle(190,140,10,BLACK);

  tft.fillRoundRect(230,110,60,60,5,BLUE);
  tft.fillCircle(260,140,10,BLACK);

  while(!pres){
    digitalWrite(13, HIGH);
    TSPoint p = ts.getPoint();
    digitalWrite(13, LOW);
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    if(p.z > ts.pressureThreshhold){
      pres= true;
    // Serial.print(p.x); Serial.print(" "); Serial.println(p.y);
       
      if (p.x>TSx_dices-100 && p.x<TSx_dices+100){
        if(p.y>TSy_1st_dice-75 && p.y<TSy_1st_dice+75)
        c[j]=WHITE;
        else if(p.y>TSy_2nd_dice-75 && p.y<TSy_2nd_dice+75)
        c[j]=RED;
        else if(p.y>TSy_3th_dice-75 && p.y<TSy_3th_dice+75)
        c[j]=GREEN;
        else if(p.y>TSy_4th_dice-75 && p.y<TSy_4th_dice+75)
        c[j]=BLUE;
      }
    }
  }
}
  for(int i=0;i<n;i++){
    tft.fillScreen(c[i]);
    delay(200);
  }
}

int j=1;

void loop() {
  //calculation of players turn
  t=count%n;
  tft.fillScreen(c[t]);
  tft.setTextColor(YELLOW);
    tft.setRotation(1); 
    tft.setCursor(0, 50);
    tft.setTextSize(3);
    tft.print("it's player");tft.print(t+1);tft.print(" turn");
  
  //showing number of dice fallen
  tft.fillRoundRect(225,110,60,60,5,BLACK);
    tft.setTextColor(YELLOW);
    tft.setRotation(1); 
    tft.setTextSize(2);
    tft.setCursor(245, 135);
    tft.print(count);
  
  //Virtual Dice option
  if(vd){
    tft.fillRoundRect(35,110,60,60,5,YELLOW);
    tft.setTextColor(BLACK);
    tft.setRotation(1); 
    tft.setTextSize(1);
    tft.setCursor(45, 130);
    tft.print("Virtual");
    tft.setCursor(55, 145);
    tft.print("Dice");}
  else{
    tft.fillRoundRect(35,110,60,60,5,BLACK);
    tft.setTextColor(YELLOW);
    tft.setRotation(1); 
    tft.setTextSize(1);
    tft.setCursor(45, 130);
    tft.print("Virtual");
    tft.setCursor(55, 145);
    tft.print("Dice");
    }
 /* pres=false;
  while(!pres){
    digitalWrite(13, HIGH);
    TSPoint p = ts.getPoint();
    digitalWrite(13, LOW);
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    if(p.z > ts.pressureThreshhold){
    pres= true; a=0;
    if(p.x>TSx_dices-100 && p.x<TSx_dices+100 && p.y>TSy_virtual_dice-90 && p.y<TSy_virtual_dice+90){vd=!vd;}
    }
  }
  if(vd){
    tft.fillRoundRect(35,110,60,60,5,YELLOW);
    tft.setTextColor(BLACK);
    tft.setRotation(1); 
    tft.setTextSize(1);
    tft.setCursor(45, 130);
    tft.print("Virtual");
    tft.setCursor(55, 145);
    tft.print("Dice");}
  else{
    tft.fillRoundRect(35,110,60,60,5,BLACK);
    tft.setTextColor(YELLOW);
    tft.setRotation(1); 
    tft.setTextSize(1);
    tft.setCursor(45, 130);
    tft.print("Virtual");
    tft.setCursor(55, 145);
    tft.print("Dice");
    }
    */
//sensing with microphone 
 while(j==0){
  pinMode(13,INPUT);
  int a=digitalRead(ss);
  //Serial.println(a);
   if(a==0)j=1;

   pinMode(13,OUTPUT);
   digitalWrite(13, HIGH);
    TSPoint p = ts.getPoint();
    digitalWrite(13, LOW);
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    if(p.z > ts.pressureThreshhold){
   
    if(p.x>TSx_dices-100 && p.x<TSx_dices+100 && p.y>TSy_virtual_dice-90 && p.y<TSy_virtual_dice+90){vd=!vd; pres=true;}
    }
    
    //Virtual Dice option
    if(pres){
      if(vd){
    tft.fillRoundRect(35,110,60,60,5,YELLOW);
    tft.setTextColor(BLACK);
 //   tft.setRotation(1); 
 //   tft.setTextSize(1);
    tft.setCursor(45, 130);
    tft.print("Virtual");
    tft.setCursor(55, 145);
    tft.print("Dice");}
  else{
    tft.fillRoundRect(35,110,60,60,5,BLACK);
    tft.setTextColor(YELLOW);
 //   tft.setRotation(1); 
 //   tft.setTextSize(1);
    tft.setCursor(45, 130);
    tft.print("Virtual");
    tft.setCursor(55, 145);
    tft.print("Dice");
    }
pres=false;
}
  //showing the color of player whose turn is
  if(c[t]==RED){
  digitalWrite(rLED,HIGH);digitalWrite(gLED,LOW);digitalWrite(bLED,LOW);}
  
  if(c[t]==BLUE){
  digitalWrite(rLED,LOW);digitalWrite(gLED,LOW);digitalWrite(bLED,HIGH);}
  
  if(c[t]==GREEN){
  digitalWrite(rLED,LOW);digitalWrite(gLED,HIGH);digitalWrite(bLED,LOW);}
  
  if(t==WHITE){
  digitalWrite(rLED,HIGH);digitalWrite(gLED,HIGH);digitalWrite(bLED,HIGH);}
  }
  
  //LED equalizer
  if(j==1){
  for(int i=0;i<6;i++){

  digitalWrite(rLED,HIGH);digitalWrite(gLED,LOW);digitalWrite(bLED,LOW);
  delay(100);
  digitalWrite(rLED,LOW);digitalWrite(gLED,HIGH);digitalWrite(bLED,LOW);
  delay(100);
  digitalWrite(rLED,LOW);digitalWrite(gLED,LOW);digitalWrite(bLED,HIGH);
  delay(100);
  digitalWrite(rLED,HIGH);digitalWrite(gLED,HIGH);digitalWrite(bLED,HIGH);
  delay(100);
  
  //LCD dice animation
  tft.setTextColor(YELLOW);
    tft.setRotation(1); 
    tft.setCursor(20, 200);
    tft.setTextSize(2);
    tft.print("Dice is falling...");
  tft.fillRoundRect(130,110,60,60,5,WHITE);
  delay(200);
  Dice(random(6)+1);
  }
  if(vd){
//    tft.fillRect(10,180,250,60,c[t]);
    tft.setCursor(250, 200);
    tft.print("fell!");
    delay(2000);
    }
  a=1;
  j=0;
  }
  
  count++;
}
//-------------------------------------------------------------------------------------
//dice drawing function
void Dice(int d){
  if(d==1){
  tft.fillRoundRect(130,110,60,60,5,WHITE);
  tft.drawRoundRect(130,110,60,60,5,BLACK);
  tft.fillCircle(160,140,10,RED);
  }
  else if(d==2){
  tft.fillRoundRect(130,110,60,60,5,WHITE);
  tft.drawRoundRect(130,110,60,60,5,BLACK);
  tft.fillCircle(160,125,8,BLACK);
  tft.fillCircle(160,155,8,BLACK);
  }
  else if(d==3){
  tft.fillRoundRect(130,110,60,60,5,WHITE);
  tft.drawRoundRect(130,110,60,60,5,BLACK);
  tft.fillCircle(140,120,8,BLACK);
  tft.fillCircle(160,140,8,BLACK);
  tft.fillCircle(180,160,8,BLACK);
  }
  else if(d==4){  
  tft.fillRoundRect(130,110,60,60,5,WHITE);
  tft.drawRoundRect(130,110,60,60,5,BLACK);
  tft.fillCircle(145,125,8,BLACK);
  tft.fillCircle(175,125,8,BLACK);
  tft.fillCircle(145,155,8,BLACK);
  tft.fillCircle(175,155,8,BLACK);
  }
  else if(d==5){
  tft.fillRoundRect(130,110,60,60,5,WHITE);
  tft.drawRoundRect(130,110,60,60,5,BLACK);
  tft.fillCircle(145,125,8,BLACK);
  tft.fillCircle(175,125,8,BLACK);
  tft.fillCircle(145,155,8,BLACK);
  tft.fillCircle(175,155,8,BLACK);
  tft.fillCircle(160,140,8,BLACK);
  }
  else if(d==6){
  tft.fillRoundRect(130,110,60,60,5,WHITE);
  tft.drawRoundRect(130,110,60,60,5,BLACK);
  tft.fillCircle(145,125,6,BLACK);
  tft.fillCircle(175,125,6,BLACK);
  tft.fillCircle(145,140,6,BLACK);
  tft.fillCircle(175,140,6,BLACK);
  tft.fillCircle(145,155,6,BLACK);
  tft.fillCircle(175,155,6,BLACK);
  }
}

Credits

ElectroPeak
57 projects • 743 followers
At ElectroPeak we want to teach you to enjoy electronics more. We offer Top-notch guides and worry-free shopping experience.
Contact

Comments

Please log in or sign up to comment.