Costume Chooser
As Halloween approaches, and people begin to brainstorm ideas of what to dress up as. Once searching for costumes, its hard to narrow and choose from all of the ideas that were come up with. To help assist the difficult decision, you can use this Costume Chooser. Just assign 6 costumes to numbers 1 to 6 and press the button. Whichever number it lands on thats the costume you should dress up as.
Setting Up
What you need:
1. Button
2. Light
3. Resister
4. Wires
5. Breadboard
6. Arduino
Writing the Code
Writing the code could take a lot of trials and errors, however this code is what we found to be most useful and successful.
This portion of the code makes and sets up the lights
1. // set to 1 if we're debugging
#define DEBUG 0
// 6 consecutive digital pins for the LEDs
int first = 2;
int second = 3;
int third = 4;
int fourth = 5;
int fifth = 6;
int sixth = 7;
// pin for the button switch int button = 12;
// value to check state of button switch
int pressed = 0;
Code 2
Void setup: the basic setup of the code
void setup() {
// set all LED pins to OUTPUT
for (int i=first; i<=sixth; i++)
{ pinMode(i, OUTPUT); } // set button pin to INPUT pinMode(button, INPUT); // initialize random seed by noise from analog pin 0 (should be unconnected) randomSeed(analogRead(0)); // if we're debugging, connect to serial #ifdef DEBUG Serial.begin(9600); #endif }
Code 3
This section of the code controls all of the lights
void buildUpTension() {
// light LEDs from left to right and back to build up tension
// while waiting for the dice to be thrown
// left to right
for (int i=first; i<=sixth; i++) {
if (i!=first) {
digitalWrite(i-1, LOW);
}
digitalWrite(i, HIGH);
delay(100);
}
// right to left
for (int i=sixth; i>=first; i--) {
if (i!=sixth) {
digitalWrite(i+1, LOW);
}
digitalWrite(i, HIGH);
delay(100);
}
}
void showNumber(int number) {
digitalWrite(first, HIGH);
if (number >= 2) {
digitalWrite(second, HIGH);
}
if (number >= 3) {
digitalWrite(third, HIGH);
}
if (number >= 4) {
digitalWrite(fourth, HIGH);
}
if (number >= 5) {
digitalWrite(fifth, HIGH);
}
if (number == 6) {
digitalWrite(sixth, HIGH);
}
}
int throwDice() {
int randNumber = random(1,6);
#ifdef DEBUG
Serial.println(randNumber);
#endif
return randNumber;
}
Code 4
Lastly, for the code, allow the computer to "throw the dice" to choose your costume
void setAllLEDs(int value) {
for (int i=first; i<=sixth; i++) {
digitalWrite(i, value);
}
}
void loop() {
// if button is pressed - throw the dice
pressed = digitalRead(button);
if (pressed == HIGH) {
// remove previous number
setAllLEDs(LOW);
buildUpTension();
int thrownNumber = throwDice();
showNumber(thrownNumber);
}
}