Sunday, January 10, 2010

Sunday Roundup

First: I did get the button controls added to the random number generator. The left-hand button selects the number of faces desired on the die (4,6,8,10,12,20,99) and the right hand button rolls the die. The min and max values for each face (0-99 for 99, 1 to n for all the others) are stored in arrays. It rolls and increments reliably, but the randomness of the number is sketchy and highly dependent on noise on the board. I'm not posting video yet because of the embarassing number of repeated rolls.

Second: in spite of the problem with the randomness, I am building a permanent version of the board on a prototype board. This is my first time building out a prototype board, especially one of my own design. I had a moment this week where I realized it was a lot like writing a program... You can't just expect to *know* how to get everything to work the first time you try. You have to layer your steps so that you can see and fix problems before they become permanent. I'm currently having a problem with the insulation melting on the jumpers I'm using, so the build is on pause until I can pick up some teflon tubing to use as insulation.

Third: One of the things my Dad tried to teach me -- which we had much conflict over -- is the principle of beginning where you are. I had hoped to get a couple of xBee's in SparkFun's giveaway this week, but I was one of the 69,000 some odd folks who clicked and didn't win. So instead I'm working on interfacing a keyboard with my Arduino. The principle of starting where you are comes into play because while there is an ideal keyboard out there for something like this, I don't want to spend $40 in an auction or $80 new for it. So then I was going to go to a thrift store and pick up a used keyboard. And then I realized I have used keyboards already -- in particular a cheap Microsoft one which I gummed up with a Coke and then replaced with an ergonomic keyboard. So my new project is going to be learning about serial communication over a USB interface and building a lookup table so I can do keyboard and/or keypad input for Arduino projects. I'm using this Instructable for guidance.

Labels: , ,

Sunday, December 27, 2009

Project: Dice

I ran across this project for a single 6-sided die at the Electronics Club website and I was intrigued by their use of a grouped logic for presenting the results. I wanted to use my Duemilanova for both the randomizer and the LED driver though. Here are my results.

The first step was to set up the breadboard circuit. I'm using a solderless breadboard so I ended up with the seven pips on a diagonal.

7 pip die circuit

The logic of the circuit is this: every time an odd number is rolled on a die, the center pip is included. Every number other than one has pairs of pips showing, with or without the center.

So the center has a pin to itself (13), and the other pips are paired by the white jumpers. Each pair gets a pin, which allows me to represent 6 (actually 7) numbers using 4 pins -- and no decoder.

My first sketch computes a random number (using pin 0 as a seed as suggested in the randomize() documentation) from 1 to 6, turns on the appropriate pips, waits a second, then does it again.

Here's video:



And here's the sketch:

/*
Die

computes a random number from 1 to 6 and displays it using 7 LEDs as a die face

The circuit:
* LED connected from digital pin 13 to ground.
* 2 LEDs in serial connected from digital pin 12 to ground. (2s place)
* 2 LEDs in serial connected from digital pin 11 to ground. (4s place)
* 2 LEDs in serial connected from digital pin 10 to ground. (6s place)


Created 26 Dec 2009
By Anne Speck

*/

int die1; // container for current value of die1 (possible future expansion)
int basket; // variable for processing die values

int ledPin13 = 13; // LED connected to digital pin 13
int ledPin12 = 12; // 2 LEDs connected to digital pin 12
int ledPin11 = 11; // 2 LEDs connected to digital pin 11
int ledPin10 = 10; // 2 LEDs connected to digital pin 10

int one = LOW;
int two = LOW;
int four = LOW;
int six =LOW;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin10, OUTPUT);

//use serial output for debugging
Serial.begin(9600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
// generate random number
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
die1 = random(1, 7);


// translate number into pins for display
basket = die1;
if (basket%2) {
//If there's a modulo, this is an odd number.
basket--;
one=HIGH;
}
if (basket == 6) {
//If six, turn on sixes place and subtract two
basket--;
basket--;
six=HIGH;
}
if (basket == 4) {
//If four, turn on fours place and subtract two
basket--;
basket--;
four=HIGH;
}
if (basket == 2) {
//If this has a value it's two.
basket=0;
two=HIGH;
}


digitalWrite(ledPin13, one); // set the LED on
digitalWrite(ledPin12, two); // set the LED on
digitalWrite(ledPin11, four); // set the LED on
digitalWrite(ledPin10, six); // set the LED on

//serial output for debugging
Serial.print("The number is: ");
Serial.println(die1);
Serial.print(one);
Serial.print(two);
Serial.print(four);
Serial.println(six);


delay(1000); // wait for a second

//Reset all pins
one = LOW;
two = LOW;
four = LOW;
six =LOW;

}

Labels: , , , , ,