Thomas Angielsky
Published © GPL3+

Special Functions For The Kid's Cube

The Arduino dice of my previous project are perfect for kids. But it's even better if you add some special functions.

BeginnerFull instructions provided1,899
Special Functions For The Kid's Cube

Things used in this project

Story

Read more

Code

wuerfel2_v3.ino

Arduino
// Program: LED Dice version 3 
 
// setup the pins
// pin number of the first LED
int PinErsteLED=2;
 
// pin number of the button
int PinTaster=9;
 
// LED order
// 1   2 
// 3 7 4
// 5   6
int wuerfelBild[8][7]=
 {{0,0,0,0,0,0,0}, //0 = all LED's off
  {0,0,0,0,0,0,1}, //1
  {0,1,0,0,1,0,0}, //2
  {1,0,0,0,0,1,1}, //3
  {1,1,0,0,1,1,0}, //4
  {1,1,0,0,1,1,1}, //5
  {1,1,1,1,1,1,0}, //6
  {1,1,1,1,1,1,1}}; //7 = all LED's on
 
int lauflichtmax=6; 
int lauflicht[6]=     {  1,  7,  6,  2,  7,  5}; //LED number
int lauflichtpause[6]={ 60,100,180,180,100, 60}; //time in ms
 
 
void setup() {
 
  // define all pins as output
  for (int i=PinErsteLED;i<=PinErsteLED+6;++i)
  pinMode(i,OUTPUT);
 
  // pin of the button
  pinMode(PinTaster,INPUT);
}
 
// function to show the result
void ZeigeZahl(int zahl) {   
  for (int i=0;i<=6;++i)     
    digitalWrite(i+PinErsteLED,wuerfelBild[zahl][i]?HIGH:LOW); 
} 

void loop() {   
//declare variables  
int i,j,zufall;
unsigned long spezial;

  ZeigeZahl(7); //all LED's on  
  delay(1000);  // wait 1 second     
  // start animation   
  i=0;   
  do {     
    i=i+1;     
    if (i>6) {i=1;}   
    delay(500);   
    ZeigeZahl(i); //show number 
  } while (digitalRead(PinTaster)==HIGH); //wait til key is pressed
 
  // start dicing 
  do { 
    //setup
    zufall=0; 
    i=0;
    spezial=millis();
    do {
      zufall=zufall+1; //used for random number
      i=i+1;
      if (i>=lauflichtmax) {i=0;} //if i is lauflichtmax, then set i to 0, so i is in 0..6
      delay(lauflichtpause[i]); //wait 
 
      ZeigeZahl(0); // all LED's off
      digitalWrite(PinErsteLED+lauflicht[i]-1,HIGH);
 
     } while (digitalRead(PinTaster)==LOW); //repeat until the button is releashed
 
     zufall=zufall%6; //result is number 0..5
     spezial=millis()-spezial;
     if ((spezial>2000) and (spezial<3000)) {
       if (zufall!=5) {zufall=millis()%6;} //dice again, if no 6!
     }
    ZeigeZahl(zufall+1); //show number 
 
    while (digitalRead(PinTaster)==HIGH); //wait til next keypress
  } while (1==1); //repeat endless
}

    

Credits

Thomas Angielsky

Thomas Angielsky

18 projects • 36 followers
Mechanical engineer, maker, love woodwork, like Lazarus

Comments