Tinkering with microcontrollers has been on my “to try” list for quite some time. I recently stumbled upon Creation Crate. Creation Crate is a monthly tech education subscription box. Each month you receive a Arduino UNO R3 and parts to complete a project. The instruction book includes the source code for the project. The source code is licensed under the GNU General Public License. Each month the projects increase in difficulty. I thought Creation Crate would be a wonderful way to get my feet with wet with Arduino and programming electronics.
I was on the fence about signing up for the subscription box because I was not sure what to expect. I intend to write a blog post for each Creation Crate I receive in case there is a reader out there on the fence like I was.
The Project
The project for Month 4 is a LED dice game. The Arduino project simulates the roll of a 6-sided dice.
What’s in the box?
Inside of Month four’s box we have:
- Arduino UNO R3
- Breadboard
- USB Cable
- Yellow LEDs
- Resistors
- Jumper wires
- Push button with cap
Building the Hardware
Assembly for Month 4’s project was pretty easy.
Programming
Last month, steps were omitted for assembling the hardware. This month, the challenge is on the software side. Sections of code are omitted from the instruction book. It is not a difficult exercise, but it will encourage you to pay more attention to which pins the inputs and outputs are plugged into.
// License: GNU General Public License | |
// Creation Crate Month 4 - LED Dice Game | |
#define button 3 | |
#define ledSet1 4 | |
#define ledSet2 5 | |
#define ledSet3 6 | |
#define ledSet4 7 | |
// tracks if the button is on or off | |
int buttonState; | |
// stores random number between 1 and 6 | |
int roll; | |
// 2 seconds | |
int time = 2000; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(ledSet1, OUTPUT); | |
pinMode(ledSet2, OUTPUT); | |
pinMode(ledSet3, OUTPUT); | |
pinMode(ledSet4, OUTPUT); | |
pinMode(button, INPUT); | |
randomSeed(analogRead(0)); | |
Serial.begin(9600); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
buttonState = digitalRead(button); | |
if(buttonState == HIGH) { | |
roll = random(1, 7); | |
if(roll == 1) { | |
digitalWrite(ledSet3, HIGH); | |
} | |
else if(roll == 2) { | |
digitalWrite(ledSet1, HIGH); | |
} | |
else if(roll == 3) { | |
digitalWrite(ledSet1, HIGH); | |
digitalWrite(ledSet3, HIGH); | |
} | |
else if(roll == 4) { | |
digitalWrite(ledSet2, HIGH); | |
digitalWrite(ledSet4, HIGH); | |
} | |
else if(roll == 5) { | |
digitalWrite(ledSet2, HIGH); | |
digitalWrite(ledSet3, HIGH); | |
digitalWrite(ledSet4, HIGH); | |
} | |
else if(roll == 6) { | |
digitalWrite(ledSet1, HIGH); | |
digitalWrite(ledSet2, HIGH); | |
digitalWrite(ledSet4, HIGH); | |
} | |
} | |
Serial.print("You rolled a "); | |
Serial.println(roll); | |
delay(time); | |
// turn off all the LEDs | |
digitalWrite(ledSet1, LOW); | |
digitalWrite(ledSet2, LOW); | |
digitalWrite(ledSet3, LOW); | |
digitalWrite(ledSet4, LOW); | |
} |
Final Thoughts
The LED dice game is a really simple Arduino project. I hope next month we’ll see a nice increase in difficulty.
Dave Beck says
Cool project! Great to see the little guys helping out.