Robot Art Show

Evidence of Work

The goal of this project was to build a circuit with coding and an arduino to do something cool (like a robot). I decided to build a random dice roll game. This would randomly pick a number between 1 and 6 and display it using LED lights.

I started by researching different projects online. There were multiple random dice roll projects. I picked the project where the circuit design seemed the most clean. The project had a code that was already written. I looked over the code to make sure I could mostly understand it, and then wrote explanations of what each line of code meant.


Robot Art Show
Dice Game Code for Arduino
 
#define DEBUG 0 
// Debugging switches and macros
// Switches debug output on and off by 1 or 0

// 6 consecutive digital pins for the LEDs

int first = 2; //integer 1 = pin 2

int second = 3; //integer 2 = pin 3

int third = 4; //integer 3 = pin 4

int fourth = 5; //integer 4 = pin 5

int fifth = 6; //integer 5 = pin 6

int sixth = 7; //integer 6 = pin 7

// pin for the button switch

int button = 12; integer or variable button = pin 12

// value to check state of button switch

int pressed = 0; //variable for reading the pin status

void setup() { //sets program up

for (int i=first; i<=sixth; i++) {

pinMode(i, OUTPUT); // sets all LED pins to OUTPUT

}

pinMode(button, INPUT); // sets button pin to INPUT

randomSeed(analogRead(0)); // initialize random seed from analog pin 0,

// initializes the pseudo-random number generator

// starts at an arbitrary point in its random sequence

#ifdef DEBUG // if we're debugging, connect to serial

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps 
 

#endif //specifies end of the debugging directive

}

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++) {

// loop that activates code block for every time that i is smaller than 6

//Until i reaches 6

//After each loop, i increases by 1 (i++)

// the loop will eventually stop when i (i becomes 6, so no longer is smaller than 6

if (i!=first) { //if this condition is met

digitalWrite(i-1, LOW); //assigns a low value to digital pin

}

digitalWrite(i, HIGH);

//assigns a high value to digital pin

delay(100); // Pauses the program for the amount of time (in milliseconds)

}

// right to left

for (int i=sixth; i>=first; i--) {

// loop that activates code block for every time that i is greater than 1

// Until i reaches 1

//After each loop, i decreases by 1 (i--)

if (i!=sixth) { //If this condition is met

digitalWrite(i+1, LOW); //assigns a low value to digital pin

}

digitalWrite(i, HIGH); //assigns a high value to digital pin

delay(100);

// Pauses the program for the amount of time (in milliseconds)

}

}

void showNumber(int number) {

//display the number corresponding to each digit

digitalWrite(first, HIGH);

// assigns a high value to digital pin 2,

//(integer 1)

if (number >= 2) {

digitalWrite(second, HIGH); n

// assigns a high value to digital pin 3 if the integer = 2 or greater

)

}

if (number >= 3) {

digitalWrite(third, HIGH);

// assigns a high value to digital pin 4 if the integer = 3 or greater

}

if (number >= 4) {

digitalWrite(fourth, HIGH);

// assigns a high value to digital pin 5 if the integer = 4 or greater

}

if (number >= 5) {

digitalWrite(fifth, HIGH);

// assigns a high value to digital pin 6 if the integer = 5 or greater

}

if (number == 6) {

digitalWrite(sixth, HIGH);

// assigns a high value to digital pin 7 if the integer = 6 or greater

}

}

int throwDice() {

// get a random number in the range [1,6]

int randNumber = random(1,7);

//creates a random number between 1 and 6

#ifdef DEBUG // if we're debugging, connect to serial

Serial.println(randNumber); // sends generated number to the serial port

#endif //specifies end of the debugging directive

return randNumber; // terminate a function

}

void setAllLEDs(int value) { //assigns all LED’s an integer value

for (int i=first; i<=sixth; i++) {

// loop that activates code block for every time that i is smaller than //Until i reaches 6

//After each loop, i increases by 1 (i++)

// the loop will eventually stop when i (i becomes 6, so no longer is smaller than 6

digitalWrite(i, value); //assigns integer to pin

}

}

void loop() {

// if button is pressed - throw the dice

pressed = digitalRead(button); // reads the value

if (pressed == HIGH) {

// remove previous number

setAllLEDs(LOW); //assigns low value to all LEDs

buildUpTension();

// light LEDs from left to right and back to build up tension

// while waiting for the dice to be thrown

// left to right

int thrownNumber = throwDice(); //the thrown integer = the dice number

showNumber(thrownNumber); //LEDs display the thrown number

}

}

Then, the next step was to build the device. We were supposed to do this on TinkerCAD, but I decided decided to use a physical set up. I though this would be more fun and a bit easier to understand. I bought an arduino and some materials online and was able to build the dice roller below. I also used TinkerCAD to make a diagram of the circuit, since it is a bit easier to understand as a diagram without the overlapping wires and you can represent the wires as different colors easily.

Content

The random dice roller was a parallel circuit with multiple resistors and LED lights. The circuit branches each with a separate path for flow of charge / electrons. I used both a 9 volt battery and the computer battery as power sources and both worked well. I used multiple resisters in the circuit.

Circuit - A complete loop of conductive material from one side of a power source

Series Circuit - Circuit with single path and multiple components one after another all in the same path

Parallel Circuit - Circuit with branches each with a separate path for flow of charge / electrons

Resistance - Amount current is slowed or resisted through an obstacle

Voltage - Potential energy difference from one side of component to the other side

Current - Flow of charge / electricity through the circuit

Power - Rate of transferring electrical energy through a circuit

Ohms Law = V=IR

Resistance in a series circuit is calculated by adding the resistance of different components r=r1=r2=r3....

Resistance in a parallel circuit is inverse 1/r=1/r1+1/r2+1/r3

I found coding a bit difficult to understand at first, but it was interesting to see the interaction between the computer and the circuit.


Reflection


Two things that went well were building the circuit and finding the code. I also think I was able to understand the physics involved (ohm's law) and building the circuit helped that. I had a hard time explaining the code. I think I need more practice before that would go well. I think it might have been a good idea to pick something with a simpler code since I had no experience with this.