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...

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maxint 1:38fcbf88615a 1 /*
maxint 1:38fcbf88615a 2 ** mBuino_Dice
maxint 1:38fcbf88615a 3 **
maxint 1:38fcbf88615a 4 ** This electronic dice application allows the user to throw an electronic dice by the press of a button
maxint 1:38fcbf88615a 5 ** Setup requirements: connect any type of switch between P0.4 and GND.
maxint 1:38fcbf88615a 6 ** On mBuino P0.4 is close to GND and the two pitch distance is exactly the same as a mini-switch!
maxint 1:38fcbf88615a 7 **
maxint 1:38fcbf88615a 8 ** Made for mBuino on mbed.org by maxint on 1-sep-2014
maxint 1:38fcbf88615a 9 **
maxint 1:38fcbf88615a 10 ** Feel free to use this code anyway you want, but please acknowledge my work by mentioning my name.
maxint 1:38fcbf88615a 11 **
maxint 1:38fcbf88615a 12 */
maxint 1:38fcbf88615a 13
maxint 1:38fcbf88615a 14
Architect 0:5d9ccbe9d49d 15 #include "mbed.h"
maxint 1:38fcbf88615a 16
maxint 1:38fcbf88615a 17 DigitalOut LED[] = {(P0_7), (P0_8), (P0_2), (P0_20), (P1_19), (P0_17), (P0_23)};// declare 7 LEDs
maxint 1:38fcbf88615a 18 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
maxint 1:38fcbf88615a 19 AnalogIn RandomIn = (P0_14); // use the random noise on this analog input to seed the random generator
maxint 1:38fcbf88615a 20
maxint 1:38fcbf88615a 21
maxint 1:38fcbf88615a 22 bool DicePattern[6][7]=
maxint 1:38fcbf88615a 23 {
maxint 1:38fcbf88615a 24 {0, 0, 0, 1, 0, 0, 0 },
maxint 1:38fcbf88615a 25 {0, 1, 0, 0, 0, 1, 0 },
maxint 1:38fcbf88615a 26 {0, 1, 0, 1, 0, 1, 0 },
maxint 1:38fcbf88615a 27 {1, 0, 1, 0, 1, 0, 1 },
maxint 1:38fcbf88615a 28 {1, 0, 1, 1, 1, 0, 1 },
maxint 1:38fcbf88615a 29 {1, 1, 1, 0, 1, 1, 1 }
maxint 1:38fcbf88615a 30 };
Architect 0:5d9ccbe9d49d 31
Architect 0:5d9ccbe9d49d 32 float delayTime = .05;
Architect 0:5d9ccbe9d49d 33
maxint 1:38fcbf88615a 34
maxint 1:38fcbf88615a 35 void SwitchAllLeds(bool fOn)
maxint 1:38fcbf88615a 36 { // switch all leds on or off
maxint 1:38fcbf88615a 37 for(int x = 0; x < 7; x++)
maxint 1:38fcbf88615a 38 {
maxint 1:38fcbf88615a 39 LED[x] = fOn?1:0; // turn on or off
maxint 1:38fcbf88615a 40 }
maxint 1:38fcbf88615a 41 }
maxint 1:38fcbf88615a 42
maxint 1:38fcbf88615a 43 void BlinkAllLeds(float flDelay=0.2)
maxint 1:38fcbf88615a 44 { // blink all leds
maxint 1:38fcbf88615a 45 SwitchAllLeds(true);
maxint 1:38fcbf88615a 46 wait(flDelay);
maxint 1:38fcbf88615a 47 SwitchAllLeds(false);
maxint 1:38fcbf88615a 48 wait(flDelay);
maxint 1:38fcbf88615a 49 }
maxint 1:38fcbf88615a 50
maxint 1:38fcbf88615a 51 void BlinkOneLed(int nLed=0, float flDelayOn=0.2, float flDelayOff=0.2)
maxint 1:38fcbf88615a 52 {
maxint 1:38fcbf88615a 53 LED[nLed] = 1; // turn on
maxint 1:38fcbf88615a 54 wait(flDelayOn); // delay
maxint 1:38fcbf88615a 55
maxint 1:38fcbf88615a 56 LED[nLed] = 0; // turn off
maxint 1:38fcbf88615a 57 wait(flDelayOff); // delay
maxint 1:38fcbf88615a 58 }
maxint 1:38fcbf88615a 59
maxint 1:38fcbf88615a 60 void SweepSingleLed(bool fLeftToRight=true, float flDelay=0.2)
maxint 1:38fcbf88615a 61 { // sweep a single led from left to rigth or vise-versa
maxint 1:38fcbf88615a 62 for(int n=0; n<7; n++)
maxint 1:38fcbf88615a 63 BlinkOneLed(fLeftToRight?n:6-n, flDelay, 0);
maxint 1:38fcbf88615a 64 }
maxint 1:38fcbf88615a 65
maxint 1:38fcbf88615a 66 void SweepAllLeds(bool fLeftToRight=true, float flDelay=0.1)
maxint 1:38fcbf88615a 67 { // light all leds up from left to rigth or vise-versa and then switch them of from reverse direction
maxint 1:38fcbf88615a 68 // leds on, left to right
maxint 1:38fcbf88615a 69 for(int n=0; n<7; n++)
maxint 1:38fcbf88615a 70 {
maxint 1:38fcbf88615a 71 LED[fLeftToRight?n:6-n] = 1; // turn on
maxint 1:38fcbf88615a 72 wait(flDelay); // delay
maxint 1:38fcbf88615a 73 }
maxint 1:38fcbf88615a 74 // leds off, right to left
maxint 1:38fcbf88615a 75 for(int n=0; n<7; n++)
maxint 1:38fcbf88615a 76 {
maxint 1:38fcbf88615a 77 LED[!fLeftToRight?n:6-n] = 0; // turn off
maxint 1:38fcbf88615a 78 wait(flDelay); // delay
maxint 1:38fcbf88615a 79 }
maxint 1:38fcbf88615a 80 }
maxint 1:38fcbf88615a 81
maxint 1:38fcbf88615a 82 void SwitchDiceLed(int nNumber, bool fOn=true)
maxint 1:38fcbf88615a 83 { // switch the leds of a particular dice-number on or off
maxint 1:38fcbf88615a 84 if(nNumber<1) nNumber=1;
maxint 1:38fcbf88615a 85 if(nNumber>6) nNumber=6;
maxint 1:38fcbf88615a 86
maxint 1:38fcbf88615a 87 for(int n=0; n<7; n++)
maxint 1:38fcbf88615a 88 if(DicePattern[nNumber-1][n])
maxint 1:38fcbf88615a 89 LED[n]=fOn;
maxint 1:38fcbf88615a 90 }
maxint 1:38fcbf88615a 91
maxint 1:38fcbf88615a 92 void BlinkDiceLed(int nNumber, float flDelayOn=0.6, float flDelayOff=0.1)
maxint 1:38fcbf88615a 93 { // blink a particular dice value
maxint 1:38fcbf88615a 94 SwitchDiceLed(nNumber, true);
maxint 1:38fcbf88615a 95 wait(flDelayOn);
maxint 1:38fcbf88615a 96 SwitchDiceLed(nNumber, false);
maxint 1:38fcbf88615a 97 wait(flDelayOff);
maxint 1:38fcbf88615a 98 }
maxint 1:38fcbf88615a 99
maxint 1:38fcbf88615a 100 int RollDice(int nRolls=10, int nBlinks=2, float flDelayRoll=0.2)
maxint 1:38fcbf88615a 101 { // roll the dice and show the outcome value
maxint 1:38fcbf88615a 102 int nDiceValue;
maxint 1:38fcbf88615a 103
maxint 1:38fcbf88615a 104 // first roll the dice
maxint 1:38fcbf88615a 105 for(int n=0; n<nRolls; n++)
maxint 1:38fcbf88615a 106 {
maxint 1:38fcbf88615a 107 nDiceValue=rand()%6+1;
maxint 1:38fcbf88615a 108 BlinkDiceLed(nDiceValue, flDelayRoll, 0);
maxint 1:38fcbf88615a 109 }
maxint 1:38fcbf88615a 110
maxint 1:38fcbf88615a 111 // then show the result
maxint 1:38fcbf88615a 112 nDiceValue=rand()%6+1;
maxint 1:38fcbf88615a 113 for(int n=0; n<nBlinks; n++)
maxint 1:38fcbf88615a 114 BlinkDiceLed(nDiceValue, flDelayRoll, 0);
maxint 1:38fcbf88615a 115 BlinkDiceLed(nDiceValue, true);
maxint 1:38fcbf88615a 116
maxint 1:38fcbf88615a 117 return(nDiceValue);
maxint 1:38fcbf88615a 118 }
maxint 1:38fcbf88615a 119
maxint 1:38fcbf88615a 120 bool fButtonPressed=false; // if the button is pressed, mBuino will switch from demo mode to play mode.
maxint 1:38fcbf88615a 121 bool fDemoDone=false;
maxint 1:38fcbf88615a 122
maxint 1:38fcbf88615a 123 void interruptButtonPressed()
maxint 1:38fcbf88615a 124 {
maxint 1:38fcbf88615a 125 fButtonPressed=true;
maxint 1:38fcbf88615a 126 }
maxint 1:38fcbf88615a 127
maxint 1:38fcbf88615a 128 void setup(void)
maxint 1:38fcbf88615a 129 { // perform initialisations
maxint 1:38fcbf88615a 130 srand(RandomIn.read_u16()); // seed the random generator with the background noise of an analog input
maxint 1:38fcbf88615a 131
maxint 1:38fcbf88615a 132 ActionButton.fall(interruptButtonPressed);
maxint 1:38fcbf88615a 133 }
maxint 1:38fcbf88615a 134
maxint 1:38fcbf88615a 135 void loop(void)
maxint 1:38fcbf88615a 136 { // run forever
maxint 1:38fcbf88615a 137 if(!fDemoDone && !fButtonPressed)
maxint 1:38fcbf88615a 138 { // Run an entertaining demo show until button is pressed
maxint 1:38fcbf88615a 139 // For all parts of the show we check to see if the actionbutton was pressed to stop the show asap
maxint 1:38fcbf88615a 140 if(!fButtonPressed)
maxint 1:38fcbf88615a 141 SweepAllLeds(true);
maxint 1:38fcbf88615a 142 if(!fButtonPressed)
maxint 1:38fcbf88615a 143 SweepAllLeds(false);
maxint 1:38fcbf88615a 144 if(!fButtonPressed)
maxint 1:38fcbf88615a 145 SweepAllLeds(true, 0.05);
maxint 1:38fcbf88615a 146 if(!fButtonPressed)
maxint 1:38fcbf88615a 147 SweepAllLeds(false, 0.05);
maxint 1:38fcbf88615a 148
maxint 1:38fcbf88615a 149 // show the dice numbers from one to six
maxint 1:38fcbf88615a 150 for(int n=1; n<=6; n++)
maxint 1:38fcbf88615a 151 if(!fButtonPressed)
maxint 1:38fcbf88615a 152 BlinkDiceLed(n);
maxint 1:38fcbf88615a 153
maxint 1:38fcbf88615a 154 if(!fButtonPressed)
maxint 1:38fcbf88615a 155 SweepSingleLed(true, 0.2);
maxint 1:38fcbf88615a 156 if(!fButtonPressed)
maxint 1:38fcbf88615a 157 SweepSingleLed(false, 0.1);
maxint 1:38fcbf88615a 158
maxint 1:38fcbf88615a 159 // show three random dice throws
maxint 1:38fcbf88615a 160 for(int n=0; n<3; n++)
maxint 1:38fcbf88615a 161 {
maxint 1:38fcbf88615a 162 if(!fButtonPressed)
maxint 1:38fcbf88615a 163 {
maxint 1:38fcbf88615a 164 RollDice();
maxint 1:38fcbf88615a 165 wait(1);
maxint 1:38fcbf88615a 166 SwitchAllLeds(false);
maxint 1:38fcbf88615a 167 }
maxint 1:38fcbf88615a 168 }
maxint 1:38fcbf88615a 169
maxint 1:38fcbf88615a 170 // blink all leds as the same time at increasing and decreasing speeds
maxint 1:38fcbf88615a 171 for(float flDelay=0.3; flDelay>0; flDelay-=0.05)
maxint 1:38fcbf88615a 172 if(!fButtonPressed)
maxint 1:38fcbf88615a 173 BlinkAllLeds(flDelay);
maxint 1:38fcbf88615a 174 for(float flDelay=0.05; flDelay<0.4; flDelay+=0.05)
maxint 1:38fcbf88615a 175 if(!fButtonPressed)
maxint 1:38fcbf88615a 176 BlinkAllLeds(flDelay);
maxint 1:38fcbf88615a 177
maxint 1:38fcbf88615a 178 if(!fButtonPressed)
maxint 1:38fcbf88615a 179 wait(1);
maxint 1:38fcbf88615a 180 }
maxint 1:38fcbf88615a 181 else
maxint 1:38fcbf88615a 182 { // demo mode is ended, roll the dice upon each button press
maxint 1:38fcbf88615a 183 fDemoDone=true;
maxint 1:38fcbf88615a 184 fButtonPressed=false;
maxint 1:38fcbf88615a 185
maxint 1:38fcbf88615a 186 while(!fButtonPressed)
maxint 1:38fcbf88615a 187 { // flash the LEDS to show we wait for a roll call
maxint 1:38fcbf88615a 188 SweepAllLeds(true, 0.01);
maxint 1:38fcbf88615a 189 SweepAllLeds(false, 0.01);
maxint 1:38fcbf88615a 190 }
maxint 1:38fcbf88615a 191 fButtonPressed=false;
maxint 1:38fcbf88615a 192
maxint 1:38fcbf88615a 193 Timer tWait;
maxint 1:38fcbf88615a 194
maxint 1:38fcbf88615a 195 int nDiceValue=RollDice();
maxint 1:38fcbf88615a 196 wait(1);
maxint 1:38fcbf88615a 197 tWait.start();
maxint 1:38fcbf88615a 198 fButtonPressed=false;
maxint 1:38fcbf88615a 199 while(!fButtonPressed && tWait.read_ms()<10000)
maxint 1:38fcbf88615a 200 BlinkDiceLed(nDiceValue, 0.2, 0.01); // hmmmm, something switched off the leds after the wait? Strange, let's just blink them...
maxint 1:38fcbf88615a 201 SwitchAllLeds(false);
maxint 1:38fcbf88615a 202 }
maxint 1:38fcbf88615a 203 }
Architect 0:5d9ccbe9d49d 204
Architect 0:5d9ccbe9d49d 205 int main()
maxint 1:38fcbf88615a 206 { // implement Arduino-style setup() and loop()
maxint 1:38fcbf88615a 207 setup();
maxint 1:38fcbf88615a 208 while(true)
maxint 1:38fcbf88615a 209 loop();
Architect 0:5d9ccbe9d49d 210 }