PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit Beaglebone.org.

Wednesday, March 26, 2014

On 9:52 AM by Richard St-Pierre

Photo of electronic dice on protocape

Overview

This project demonstrates a simple dice / game application using the BeagleBone Black. Simple and fun. Pressing the button, you can randomly generate a number, displayed like a dice, with colorful LEDs. Try your luck!

What you will need

  • BeagleBone Black
  • BeagleBone expandable case Orange or Black (recommended)
  • AC adapter 5 Volts, 2 Amp (optional)
  • Proto Cape
  • Proto Parts Kit
  • Soldering iron, solder and insulated wires

  • These components are included in the parts kit (PTK-027)
    • (3) Green LEDs
    • (3) Red LEDs
    • (1) Three-color RYG LED
    • (1) Push-button
    • (10) Resistors 330 Ohms

Instructions

Step 1

Place the circuit components as shown in the photo above.
(Note: The flat edge of the LEDs are placed away from the push-button)
Alternately you can use a solder-less breadboard and jumper wire jumper pack

Step 2

Wire the components (soldering is required) using clipped off wire leads from the resistors and LEDs.
Use insulated wire to jump over connections, install the dual-row header pins.

Step 3

Verify, double-check your connections, carefully.

Step 4

With power-off, install the prototype cape into the BeagleBone P8 and P9 connectors.

Step 5

Download the Bonescript java software program into the Cloud9 development environment.
Download ElectronicDice .js File

Step 6

Click the Run button in Cloud9.

Step 7

Press the black push button on your circuit, then release to view the dice roll.

Schematic Wiring Diagram

The CBB-proto cape and PTK-027 parts kit are used for this project. The LEDs are shown in the position required to make the dice pattern. The three center LEDs are part of the tri-color RYG LED.
The connection flags(eg. P9-21) represents the BeagleBone GPIO pin location (21) on Connector P9. Remember to connect the ground (GND) return to P9-1. The resistors limit the LED current to a safe level.

schematic electronic dice

Tri-color R-Y-G LED Pin information Details

The common cathode pin is connected to ground.
RYG LED pin diagram

Circuit Operation

When the push-button is pressed the program generates random numbers and quickly updates the LED dice pattern. Once the button is released, the roll is finalized and displayed using the LEDs. Ten BeagleBone input/output pins are used. A “switch-case” programming statement creates the LED dice pattern.

Project Software Program

Download ElectronicDice .js File

The code should look like this:


// Logic Supply Inc 
// Electronic Dice Demo Program
// Led cathodes are connected to ground. The LED anodes are connected via a 330 ohms resistor.  Active High 3.3V signal turn ON the LEDs.
// Version  : 0.0   Jan 7, 2014  Initial creation using prototyping cape (CBB-proto) and  parts kit (PTK-027).
//

var b = require('bonescript');

// define variables

var LED_G1  = "P9_21";  // left side
var LED_G2  = "P9_22";  // left side
var LED_G3  = "P9_42";  // left side
var LED_R1  = "P8_14";  // right side
var LED_R2  = "P8_15";  // right side
var LED_R3  = "P8_16";  // right side
var LED_G   = "P9_14";  // center LED
var LED_Y   = "P8_13";  // center LED
var LED_R   = "P9_16";  // center LED
var PB_SW   = "P8_26";  // Push Button

var dice    = 1;
var idle    = 0;

// initialize pins 

b.pinMode(LED_G1, b.OUTPUT);
b.pinMode(LED_G2, b.OUTPUT);
b.pinMode(LED_G3, b.OUTPUT);
b.pinMode(LED_R1, b.OUTPUT);
b.pinMode(LED_R2, b.OUTPUT);
b.pinMode(LED_R3, b.OUTPUT);
b.pinMode(LED_G, b.OUTPUT);
b.pinMode(LED_Y, b.OUTPUT);
b.pinMode(LED_G, b.OUTPUT);
b.pinMode(PB_SW, b.INPUT);

setDice(0); // clear display (LEDs OFF)

setInterval(inputHandler, 50);

function inputHandler() {     // detect switch push
    b.digitalRead(PB_SW, activate);
    function activate(x){
       if (x.value == '0')
          {
           dice = Math.round(1+ Math.random()*5);
           console.log(dice);
           setDice(dice);
           }  
    }
}


function setDice (dice){
    switch(dice){
    case(0):          // all LEDs OFF
        b.digitalWrite(LED_G1, b.LOW);
        b.digitalWrite(LED_G2, b.LOW);
        b.digitalWrite(LED_G3, b.LOW);
        b.digitalWrite(LED_R1, b.LOW);
        b.digitalWrite(LED_R2, b.LOW);
        b.digitalWrite(LED_R3, b.LOW);
        b.digitalWrite(LED_G, b.LOW);
        b.digitalWrite(LED_R, b.LOW);
        b.digitalWrite(LED_Y, b.LOW);
        break;

    case(1):
        b.digitalWrite(LED_G1, b.LOW);
        b.digitalWrite(LED_G2, b.LOW);
        b.digitalWrite(LED_G3, b.LOW);
        b.digitalWrite(LED_R1, b.LOW);
        b.digitalWrite(LED_R2, b.LOW);
        b.digitalWrite(LED_R3, b.LOW);
        b.digitalWrite(LED_G, b.HIGH);
        b.digitalWrite(LED_R, b.LOW);
        b.digitalWrite(LED_Y, b.LOW);
       break;

    case(2):
        b.digitalWrite(LED_G1, b.HIGH);
        b.digitalWrite(LED_G2, b.LOW);
        b.digitalWrite(LED_G3, b.LOW);
        b.digitalWrite(LED_R1, b.LOW);
        b.digitalWrite(LED_R2, b.LOW);
        b.digitalWrite(LED_R3, b.HIGH);
        b.digitalWrite(LED_G, b.LOW);
        b.digitalWrite(LED_R, b.LOW);
        b.digitalWrite(LED_Y, b.LOW);
       break;

     case(3):
        b.digitalWrite(LED_G1, b.HIGH);
        b.digitalWrite(LED_G2, b.LOW);
        b.digitalWrite(LED_G3, b.LOW);
        b.digitalWrite(LED_R1, b.LOW);
        b.digitalWrite(LED_R2, b.LOW);
        b.digitalWrite(LED_R3, b.HIGH);
        b.digitalWrite(LED_G, b.LOW);
        b.digitalWrite(LED_R, b.LOW);
        b.digitalWrite(LED_Y, b.HIGH);
       break;   

    case(4):
        b.digitalWrite(LED_G1, b.HIGH);
        b.digitalWrite(LED_G2, b.LOW);
        b.digitalWrite(LED_G3, b.HIGH);
        b.digitalWrite(LED_R1, b.HIGH);
        b.digitalWrite(LED_R2, b.LOW);
        b.digitalWrite(LED_R3, b.HIGH);
        b.digitalWrite(LED_G, b.LOW);
        b.digitalWrite(LED_R, b.LOW);
        b.digitalWrite(LED_Y, b.LOW);
       break;   

    case(5):
        b.digitalWrite(LED_G1, b.HIGH);
        b.digitalWrite(LED_G2, b.LOW);
        b.digitalWrite(LED_G3, b.HIGH);
        b.digitalWrite(LED_R1, b.HIGH);
        b.digitalWrite(LED_R2, b.LOW);
        b.digitalWrite(LED_R3, b.HIGH);
        b.digitalWrite(LED_G, b.LOW);
        b.digitalWrite(LED_R, b.HIGH);
        b.digitalWrite(LED_Y, b.LOW);
       break;    

    case(6):
        b.digitalWrite(LED_G1, b.HIGH);
        b.digitalWrite(LED_G2, b.HIGH);
        b.digitalWrite(LED_G3, b.HIGH);
        b.digitalWrite(LED_R1, b.HIGH);
        b.digitalWrite(LED_R2, b.HIGH);
        b.digitalWrite(LED_R3, b.HIGH);
        b.digitalWrite(LED_G, b.LOW);
        b.digitalWrite(LED_R, b.LOW);
        b.digitalWrite(LED_Y, b.LOW);
       break;
    }
}


Sample LED dice output


Electronic Dice in action