Arduino Binary Die

After buying a Nanode (an Arduino-compatible board with ethernet built-in) last weekend, we've been trying to work it out by making a couple of simple examples, the 'Binary Dice' is the first one with input and outputs.

A note, this code example is based on the one from the book Arduino: A Quick-Start Guide by Maik Schmidt.

You'll need (other than the Nanode/Arduino board):

  • 3 LEDs (ours were rated for 0.8mA-0.12mA at 6V)
  • 3 Resistors to bring the 5V down to a reason current for the LEDs (we used 680Ω)
  • 1 large resistor to flatten out the fluctuations in the ground rail (we used 17KΩ)
  • A push-to-make button

Schematic and Breadboard

Made with Fritzing, just to get used to how it works.

Breadboard layout

Circuit diagram

Arduino Sketch

You may need to change the constants at the beginning to match your board and where you plug things in.

const unsigned int LED_BIT0 = 4;
const unsigned int LED_BIT1 = 2;
const unsigned int LED_BIT2 = 3;
const unsigned int BUTTON_PIN = 5;
const unsigned int BAUD_RATE = 19200;

void setup() {
  pinMode(LED_BIT0, OUTPUT);
  pinMode(LED_BIT1, OUTPUT);
  pinMode(LED_BIT2, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
  Serial.begin(BAUD_RATE);
}

void loop() {
  const int CURRENT_BUTTON_STATE = digitalRead(BUTTON_PIN);
  
  int command = Serial.read();
  
  if (CURRENT_BUTTON_STATE == HIGH || command == '1') {
    reset_LEDs();
    delay(1000);
    randomSeed(analogRead(A0));
    long result = random(1, 7);
    output_result(result);
  }
  
}

void reset_LEDs() {
  digitalWrite(LED_BIT0, LOW);
  digitalWrite(LED_BIT1, LOW);
  digitalWrite(LED_BIT2, LOW);
}

void output_result(const long result) {
  digitalWrite(LED_BIT0, result & B001);
  digitalWrite(LED_BIT1, result & B010);
  digitalWrite(LED_BIT2, result & B100);
  Serial.print(result);
}






About the author

Portrait of the author

On weekdays I'm a Solution Architect at Nokia Siemens Networks, creating creative software solutions for mobile operators around the world.

In my spare time I'm an avid new technology fan, and constantly strive to find innovative uses for the new gadgets I manage to get my hands on. Most recently I've been investigating Mobile Codes, RFID and Home automation (mainly Z-Wave). With a keen eye for usability I'm attempting to create some cost-effective, DIY technology solutions which would rival even high-end retail products. The software I develop is usually released as Open Source.

I have a Finnish geek partner, so have begun the difficult task of learning Finnish.



Add me to your circles on Google+

The blog
Calendar
May 2012
MTWTFSS
30 01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 01 02 03
Mobile

Zap the link below with your qrcode enabled mobile to send this page to it
Mobile Code for this page
What's this?