DANIEL DWULATKA BORCATH JESUS
Published

DAIO - Die, All In One - The only die you'll ever need

DAIO is an unique die that contains all the dice needed to play RPG. This project was made for a college project study in UTFPR-PG, Brazil.

BeginnerWork in progress8 hours73
DAIO - Die, All In One - The only die you'll ever need

Things used in this project

Story

Read more

Schematics

Circuit - simulation

Code

Code

Arduino
#include <U8g2lib.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
const byte BUTTON_RODA = 10;
const byte BUTTON_SELECT = 7;
const byte SOUND_PIN = 6;

#define NOTE_C4  262
#define NOTE_E4  330
#define NOTE_G4  392
#define NOTE_C5  523

int lados=20;
int numero=1;

long timerR=0;
long timerS=0;

bool botaoEstadoAnteriorR = HIGH;  
bool botaoEstadoAnteriorS = HIGH;  


void setup() {
  pinMode(BUTTON_RODA, INPUT_PULLUP);
  pinMode(BUTTON_SELECT, INPUT_PULLUP);

  u8g2.begin();
  u8g2.clearBuffer();
  u8g2.drawRFrame(16,16,100,28,5);
  u8g2.setFont(u8g2_font_ncenB08_tr);  
  u8g2.drawStr(32, 32, "Roll the die!");
  u8g2.sendBuffer();

  Serial.begin(9600);
  randomSeed(analogRead(0));  
}

void loop() {
  
  

  if (digitalRead(BUTTON_RODA)==LOW) {
    timerR=millis();
    while(digitalRead(BUTTON_RODA)==LOW){}
    if(millis()-timerR<1000){

      rolar();

      desenharDado();

         
    }
    delay(100);
  }
    

if (digitalRead(BUTTON_SELECT)==LOW) {
    timerS=millis();
    while(digitalRead(BUTTON_SELECT)==LOW){}
    if(millis()-timerS<1000){
      mudar_dado();
    }

    desenharDado();

    delay(100);    
  }

}

void rolar(){
  for(int i=0;i<10;i++){
    numero=random(1,lados+1);
    if(numero==100){ numero=0; }
    desenharDado();         

    u8g2.sendBuffer();
    tone(SOUND_PIN, 800, 30);
    delay(i*50);
  }
  tocarSom(); 
}

void mudar_dado(){
  tone(SOUND_PIN, 1200, 100);
  if(lados==100){ lados=4; }
  else if(lados==4){ lados=6;}
  else if(lados==6){ lados=8; }
  else if(lados==8){ lados=10; }
  else if(lados==10){ lados=12; }
  else if(lados==12){ lados=20; }
  else if(lados==20){ lados=100; }
}

void desenharDado(){
  String str_num=String(numero);
  String str_lados="D"+String(lados);

  u8g2.clearBuffer();
  u8g2.drawRFrame(36,8,56,46,5);
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(0, 8, str_lados.c_str()); 
  u8g2.setFont(u8g2_font_ncenB24_tr);

  if(numero<=9){
      u8g2.drawStr(55, 43, str_num.c_str());
    }
    else{
      u8g2.drawStr(45, 43, str_num.c_str());
    }     

  u8g2.sendBuffer();
}

void tocarSom(){
  delay(100);
  if(numero==lados){
    tone(SOUND_PIN,NOTE_C4, 200);
    delay(250);
    tone(SOUND_PIN,NOTE_E4, 200);
    delay(250);
    tone(SOUND_PIN,NOTE_G4, 200);
    delay(250);
    tone(SOUND_PIN,NOTE_C5,400);
  }
  else if(numero==1){
    tone(SOUND_PIN,NOTE_C5, 300);
    delay(400);
    tone(SOUND_PIN,NOTE_G4, 300);
    delay(400);
    tone(SOUND_PIN,NOTE_E4, 300);
    delay(400);
    tone(SOUND_PIN,NOTE_C4,600);
  }
  else{
    tone(SOUND_PIN, 1000, 300);
  }
}

Credits

DANIEL DWULATKA BORCATH JESUS
1 project • 2 followers
Contact

Comments

Please log in or sign up to comment.