Skip to main content

Electronic Dice with Arduino

With my Arduino and a button from a slot machine, I realized an electronic dice for my severely multi-disabled daughter. Now she can always play along with the "Mensch-ärgere-Dich-nicht". Here is the program for controlling the 8x8 Colorduino LED Shield.


#include colorduino.h
#include bounce.h


/*************************************************************/
// Patterns for matrix
byte matrix[][8] = {
  {    
    0,0,0,0,0,0,0,0            } 
  , //leer
  {    
    0,0,0,24,24,0,0,0            } 
  , //1
  {    
    192,192,0,0,0,0,3,3            } 
  ,//2
  {    
    192,192,0,24,24,0,3,3            } 
  ,//3
  {    
    195,195,0,0,0,0,195,195            } 
  ,//4
  {    
    195,195,0,24,24,0,195,195            } 
  ,//5
  {    
    219,219,0,0,0,0,219,219          } //6
};

// farben definition
typedef struct {
  int red;
  int green;
  int blue;
} 
RGB;
/*************************************************************/
//global
/*************************************************************/
boolean taste = false;
Bounce bouncer = Bounce(A5,20);

RGB aus = {
  0,0,0};
RGB farbeZahl = {
  0,0,0}; 

/*************************************************************/
/**
/* Set one pixel of Matrix
 */
void pixel(int col, int row,bool an){
  RGB farbe = aus;    
  if (an == 1){
    farbe=farbeZahl;
  }

  Colorduino.SetPixel(col, row, farbe.red, farbe.green, farbe.blue);
  Colorduino.FlipPage();
  Colorduino.SetPixel(col, row, farbe.red, farbe.green, farbe.blue);
}
/**
/* Setzt die matrix des Colorduino
/* anhand der Zeile gemäss nummer der 
 */
void show(int nummer){
  farbeZahl.red=255;
  farbeZahl.green=0;
  farbeZahl.blue=0;
  for (int col = 0; col < 8; col++){ //Spalten

    for (int row = 0; row < 8; row++){//zeilen
      int bitZeiger=7-row;
      int muster=matrix[nummer][col];//00001100
      bool an = bitRead(muster,bitZeiger);
      pixel (col,row,an);

    }
  }
}  
// role dice
void wuerfeln(){
  int zufallszahl = random(1,7);
  show(zufallszahl);
  delay(20);
  //Serial.println("ich wuerfle ");
}

void checkDigIn(){
  bouncer.update();
  if(bouncer.read() == LOW){
    taste = true;
  }
  else{
    taste = false;
  }
}

void setTasteBySerial(){
  char received=' ';
  if (Serial.available() > 0) {
    // read the incoming byte:
    received = Serial.read();
    // say what you got:
    Serial.print("I received: ");
    Serial.println( received);

    if(received == 'A'){
      taste = true;
    }
    else{
      taste = false;
    }

  }
}
/***********************************************************************/
/* Main 
/***********************************************************************/
// init Hook
void setup() {
  Serial.begin(9600);
  pinMode(A5,INPUT);
  digitalWrite(A5, HIGH);  // set pullup on analog pin 5 
  Colorduino.Init();
  unsigned char whiteBalVal[3] = {
    33,63,63            };
  Colorduino.SetWhiteBal(whiteBalVal);
  Colorduino.ColorFill(0,0,0);
}

void loop(){

  //setTasteBySerial();
  checkDigIn();

  if (taste){  
    wuerfeln();

  }

}

Comments