Electronic dice application for the mBuino platform. Notes: The mBuino starts in Demo mode. In Demo mode the LEDs are lighted, showing sweeps and demo rolls. Connect a button or tilt-switch between P0.4 and GND. No soldering required! When the switch is triggered mBuino goes into Roll mode. In Roll mode mBuino starts with rapid flickering LEDs. Each subsequent switch trigger will perform a roll of the dice, of which the result is shown for ten seconds unless the switch is triggered for another roll. To preserve power, Power down mode is entered after inactivity in Demo mode or Roll mode. Press any button to revive.

Dependencies:   mbed

Fork of mBuino_Dice by Valentin Ivanov

mBuino_Dice

The hardware: No soldering required

As you can see in the picture gallery below, this is a very simple mBuino project, requiring only one extra component: a switch such as a push button or a tilt switch.

Push button

The push-button I used fits neatly between P0.4 and GND. By just bending the legs flat to the PCB, the button attached sturdy enough to make a usable connection.

Tilt switch

The tilt switch version is much more fun. No pushing, just a shake to roll the dice! In the tilt switch version two short wires were used to connect the switch. The wires were twisted around the legs of the tilt switch without any soldering! By adjusting the horizontal level of the switch you can make the dice react more sensitively to small movement.

For a production version, I would of course make it all a bit more durable by soldering the connections and using hot glue to keep everything in place. But going that route, I would also want to make a nicely fitting box, perhaps use other LEDs in a traditional dice-eye position and add an on-off switch...

Files at this revision

API Documentation at this revision

Comitter:
maxint
Date:
Mon Sep 01 11:33:47 2014 +0000
Parent:
0:5d9ccbe9d49d
Child:
2:0d311a6cfb96
Commit message:
First working version of electronic Dice for mBuino.; NOTE: connect action button between P0.4 and GND.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jul 23 19:02:43 2014 +0000
+++ b/main.cpp	Mon Sep 01 11:33:47 2014 +0000
@@ -1,33 +1,210 @@
+/*
+** mBuino_Dice
+** 
+** This electronic dice application allows the user to throw an electronic dice by the press of a button
+** Setup requirements: connect any type of switch between P0.4 and GND.
+** On mBuino P0.4 is close to GND and the two pitch distance is exactly the same as a mini-switch! 
+**
+** Made for mBuino on mbed.org by maxint on 1-sep-2014
+**
+** Feel free to use this code anyway you want, but please acknowledge my work by mentioning my name.
+**
+*/
+
+
 #include "mbed.h"
-//#include "rtos.h"
+
+DigitalOut LED[] = {(P0_7), (P0_8), (P0_2), (P0_20), (P1_19), (P0_17), (P0_23)};// declare 7 LEDs
+InterruptIn ActionButton = (P0_4);  // declare the input for the button (or for other single switch, such as movement switch). On mBuino P0.4 is close to GND
+AnalogIn RandomIn = (P0_14); // use the random noise on this analog input to seed the random generator
+
+
+bool DicePattern[6][7]=
+{
+    {0, 0, 0, 1, 0, 0, 0 },
+    {0, 1, 0, 0, 0, 1, 0 },
+    {0, 1, 0, 1, 0, 1, 0 },
+    {1, 0, 1, 0, 1, 0, 1 },
+    {1, 0, 1, 1, 1, 0, 1 },
+    {1, 1, 1, 0, 1, 1, 1 }
+};
 
 float delayTime = .05;
 
-DigitalOut LED[] = {(P0_7), (P0_8), (P0_2), (P0_20), (P1_19), (P0_17), (P0_23)};// declare 7 LEDs
+
+void SwitchAllLeds(bool fOn)
+{   // switch all leds on or off
+    for(int x = 0; x < 7; x++)
+    {
+        LED[x] = fOn?1:0;   // turn on or off
+    }
+}
+
+void BlinkAllLeds(float flDelay=0.2)
+{   // blink all leds
+    SwitchAllLeds(true);
+    wait(flDelay);
+    SwitchAllLeds(false);
+    wait(flDelay);
+}
+
+void BlinkOneLed(int nLed=0, float flDelayOn=0.2, float flDelayOff=0.2)
+{
+    LED[nLed] = 1; // turn on
+    wait(flDelayOn); // delay
+
+    LED[nLed] = 0; // turn off
+    wait(flDelayOff); // delay
+}
+
+void SweepSingleLed(bool fLeftToRight=true, float flDelay=0.2)
+{   // sweep a single led from left to rigth or vise-versa
+    for(int n=0; n<7; n++)
+        BlinkOneLed(fLeftToRight?n:6-n, flDelay, 0);
+}
+
+void SweepAllLeds(bool fLeftToRight=true, float flDelay=0.1)
+{   // light all leds up from left to rigth or vise-versa and then switch them of from reverse direction
+    // leds on, left to right
+    for(int n=0; n<7; n++)
+    {
+        LED[fLeftToRight?n:6-n] = 1; // turn on
+        wait(flDelay); // delay
+    }
+    // leds off, right to left
+    for(int n=0; n<7; n++)
+    {
+        LED[!fLeftToRight?n:6-n] = 0; // turn off
+        wait(flDelay); // delay
+    }
+}
+
+void SwitchDiceLed(int nNumber, bool fOn=true)
+{   // switch the leds of a particular dice-number on or off
+    if(nNumber<1) nNumber=1;
+    if(nNumber>6) nNumber=6;
+    
+    for(int n=0; n<7; n++)
+        if(DicePattern[nNumber-1][n])
+            LED[n]=fOn;
+}
+
+void BlinkDiceLed(int nNumber, float flDelayOn=0.6, float flDelayOff=0.1)
+{   // blink a particular dice value
+    SwitchDiceLed(nNumber, true);
+    wait(flDelayOn);
+    SwitchDiceLed(nNumber, false);
+    wait(flDelayOff);
+}
+
+int RollDice(int nRolls=10, int nBlinks=2, float flDelayRoll=0.2)
+{   // roll the dice and show the outcome value
+    int nDiceValue;
+    
+    // first roll the dice
+    for(int n=0; n<nRolls; n++)
+    {
+        nDiceValue=rand()%6+1;
+        BlinkDiceLed(nDiceValue, flDelayRoll, 0);
+    }
+    
+    // then show the result
+    nDiceValue=rand()%6+1;
+    for(int n=0; n<nBlinks; n++)
+        BlinkDiceLed(nDiceValue, flDelayRoll, 0);
+    BlinkDiceLed(nDiceValue, true);        
+    
+    return(nDiceValue);
+}
+
+bool fButtonPressed=false;      // if the button is pressed, mBuino will switch from demo mode to play mode. 
+bool fDemoDone=false;
+
+void interruptButtonPressed()
+{
+    fButtonPressed=true;
+}
+
+void setup(void)
+{   // perform initialisations
+    srand(RandomIn.read_u16());     // seed the random generator with the background noise of an analog input
+    
+    ActionButton.fall(interruptButtonPressed);
+}
+
+void loop(void)
+{   // run forever
+    if(!fDemoDone && !fButtonPressed)
+    {   // Run an entertaining demo show until button is pressed
+        // For all parts of the show we check to see if the actionbutton was pressed to stop the show asap
+        if(!fButtonPressed)
+            SweepAllLeds(true);
+        if(!fButtonPressed)
+            SweepAllLeds(false);
+        if(!fButtonPressed)
+            SweepAllLeds(true, 0.05);
+        if(!fButtonPressed)
+            SweepAllLeds(false, 0.05);
+        
+        // show the dice numbers from one to six
+        for(int n=1; n<=6; n++)
+            if(!fButtonPressed)
+                BlinkDiceLed(n);
+
+        if(!fButtonPressed)
+            SweepSingleLed(true, 0.2);
+        if(!fButtonPressed)
+            SweepSingleLed(false, 0.1);
+        
+        // show three random dice throws
+        for(int n=0; n<3; n++)
+        {
+            if(!fButtonPressed)
+            {
+                RollDice();
+                wait(1);
+                SwitchAllLeds(false);
+            }
+        }
+
+        // blink all leds as the same time at increasing and decreasing speeds
+        for(float flDelay=0.3; flDelay>0; flDelay-=0.05)
+            if(!fButtonPressed)
+                BlinkAllLeds(flDelay);
+        for(float flDelay=0.05; flDelay<0.4; flDelay+=0.05)
+            if(!fButtonPressed)
+                BlinkAllLeds(flDelay);
+        
+        if(!fButtonPressed)
+            wait(1);
+    }
+    else
+    {   // demo mode is ended, roll the dice upon each button press
+        fDemoDone=true;
+        fButtonPressed=false;
+        
+        while(!fButtonPressed)
+        {   // flash the LEDS to show we wait for a roll call
+            SweepAllLeds(true, 0.01);
+            SweepAllLeds(false, 0.01);
+        }
+        fButtonPressed=false;
+
+        Timer tWait;
+        
+        int nDiceValue=RollDice();
+        wait(1);
+        tWait.start();
+        fButtonPressed=false;
+        while(!fButtonPressed && tWait.read_ms()<10000)
+            BlinkDiceLed(nDiceValue, 0.2, 0.01);  // hmmmm, something switched off the leds after the wait? Strange, let's just blink them...
+        SwitchAllLeds(false);
+    }
+}
 
 int main()
-{
-    int i = 0;
-    while( i < 10 ) {
-        for(int x = 0; x < 7; x++) {
-            LED[x] = 1; // turn on
-            wait(delayTime); // delay
-        }
-        for(int x = 0; x < 7; x++) {
-            LED[x] = 0; // turn off
-            wait(delayTime); // delay
-        }
-        for(int x = 6; x >= 0; x--) {
-            LED[x] = 1; // turn on
-            wait(delayTime); // delay
-        }
-        for(int x = 6; x >= 0; x--) {
-            LED[x] = 0; // turn off
-            wait(delayTime); // delay
-        }
-
-        i++;
-    }
-
-    //Thread::wait(osWaitForever);
+{   // implement Arduino-style setup() and loop()
+    setup();
+    while(true)
+        loop();
 }