This
week, we were supposed to build a circuit that digitally simulates
rolling a die. The last time I attempted this, I was able to get the
thing working, but not correctly. It would show some numbers
perfectly, but others (4, if I remember correctly) would display
random LEDs. This time, I started out with that in mind. For my first
step, I decided to find an online tutorial demonstrating how to
achieve the desired results. I found several, including some that
used an LED numerical display. Unfortunately, I don’t have that
numeric display. So, I eliminated those immediately. Then, I settled
on one that the maker seemed very confident about. As it turns out,
that confidence was either unfounded, or the instructions presented
were too confusing. So, I found another tutorial that seemed more put
together, and actually had a schematic that I could work from. I
bypassed nearly all the instructions and pictures, and focused almost
entirely on the schematic. Built the circuit, downloaded and verified
the code, uploaded said code to my Arduino and had an LED that
wouldn’t light up. But, it was a simple matter of wire tracing. I
quickly discovered that I had two leads running to 1 LED and none to
the one at the opposite end of that string of LEDs. I moved that wire
and, voila! It worked perfectly. The saying: “The more you do it,
the easier it gets!” is true, especially in something like this.
The code
I used for this (without comments) is below:
i
nt
pinLeds1 = 10;
int pinLeds2 = 9;
int pinLeds3 = 7;
int pinLed4 = 8;
int buttonPin = 6;
int buttonState;
long ran;
int time = 2000;
void setup ()
{
pinMode (pinLeds1, OUTPUT);
pinMode (pinLeds2, OUTPUT);
pinMode (pinLeds3, OUTPUT);
pinMode (pinLed4, OUTPUT);
pinMode (buttonPin, INPUT);
randomSeed(analogRead(0));
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
ran = random(1, 7);
if (ran == 1){
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (ran == 2){
digitalWrite (pinLeds1, HIGH);
delay (time);
}
if (ran == 3){
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (ran == 4){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time);
}
if (ran == 5){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (ran == 6){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds2, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time);
}
}
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLed4, LOW);
}
int pinLeds2 = 9;
int pinLeds3 = 7;
int pinLed4 = 8;
int buttonPin = 6;
int buttonState;
long ran;
int time = 2000;
void setup ()
{
pinMode (pinLeds1, OUTPUT);
pinMode (pinLeds2, OUTPUT);
pinMode (pinLeds3, OUTPUT);
pinMode (pinLed4, OUTPUT);
pinMode (buttonPin, INPUT);
randomSeed(analogRead(0));
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
ran = random(1, 7);
if (ran == 1){
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (ran == 2){
digitalWrite (pinLeds1, HIGH);
delay (time);
}
if (ran == 3){
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (ran == 4){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time);
}
if (ran == 5){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (ran == 6){
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds2, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time);
}
}
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLed4, LOW);
}
The
circuit, itself, can be seen in the photo below. I found that the 2
most difficult parts of this week were: 1) keeping the wires from
pushing the LEDs out of alignment or covering them up, and 2) finding
a tutorial or instructional for this circuit that actually worked and
was easy enough to follow.
The
schematic that I used to build the above circuit is found below.
And,
here is the build in action.
This
week’s build was, at first, a bit difficult to find in the real
world. However, once I stopped thinking of purely physical aspects
and considered digital applications, as well as those that use a
numeric display, I realized that this is something that is used in
many different areas with some modifications. For example, we play a
game called Farkle. In the physical world, we use physical dice.
However, we have recently discovered digital versions of the game
which use various forms of digital dice. Also, our refrigerator uses
a similar circuit and coding to display temperatures and other
settings. The difference with the refrigerator is that it is not
displaying random numbers (we hope), but uses data from sensors in
the appliance to tell the numeric displays what areas to light up. I
think there may be a way to use this challenge build with some
modifications to display the speed on my “boosted board” project
that I want to build.


No comments:
Post a Comment