Ece 5 Lab 5 1
Ece 5 Lab 5 1
Simon Says -
Color Memory Game | 5
Machinery:
● Computer/Laptop
Software:
● Arduino Software (IDE)
Challenge #1a: Button Circuit
Objective
In this challenge, we will create a simple button circuit with Arduino. These buttons will represent each
of the colors later in the lab.
Components
● Arduino MEGA 2560
● 1 breadboard
● 4 pushbuttons
● 10 jumper wires (1 black for GND, 5 orange/red for 5v, and 4 for pins)
● 4 resistors (330 ohms)
Circuit Diagram
Note: In this diagram of a pushbutton to the left, 1&3 are connected and
2&4 are connected. The pushbutton is automatically set to the open
position where 1&3 are not connected with 2&4. Once the button is
pressed, all four corners are connected by the switch.
Wiring
Using the circuit diagram provided, wire the Arduino
Code
int buttonPins[4] = {8,___,___,___};
void setup() {
for (int i = 0; _____; i++)//complete for loop (i represents array index)
{
pinMode(_____, _____); // define button pinmode for each button
}
}
void loop() {
<...>
}
Task
● Build the circuit. Make sure that each button has its own resistor and
is connected to the pins shown on the Arduino.
● Read through the code and fill in the blanks.
● Upload the code to your Arduino and make sure there are no
compilation errors
Components
● Button circuit from challenge 1a
● 4 LEDs(1 blue, 1 red, 1 green, 1 yellow)
● Piezo Buzzer
● 5 resistors (330 ohm)
● 9 jumper wires
Circuit Diagram
Task
● Build the circuit using the fritzing diagram. Make sure that each LED
and the buzzer has its own resistor and is connected to the pins
shown on the Arduino.
● Read through the code and fill in the blanks.
● Upload the code to your Arduino and make sure there are no
compilation errors
Code
<...> //code from challenge 1a
int ledPins[4] = {2,___,___,___};
int buzzer = _____;
void setup() {
<...> //code from challenge 1a
for (int i = 0; _____; i++) { // complete for loop (same as before)
pinMode(_____, _____); // define LED pinmode for each LED
}
pinMode(buzzer, _____);
//This can be deleted after the buzzer is confirmed to have worked
tone(buzzer, 1000, 1000); //causes the buzzer to emit 1000Hz for 1 second
}
void loop() {
/*testing the hardware
Whenever a button is pressed one of the LEDs will light up. Make sure
every LED works and that it lights up when a button is pressed*/
//Delete after testing
if(digitalRead(8) == 1)
{
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
}
if(digitalRead(9) == 1)
{
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
}
if(digitalRead(10) == 1)
{
digitalWrite(3, HIGH);
delay(500);
digitalWrite(3, LOW);
}
if(digitalRead(11) == 1)
{
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
}
}
Challenge #2: PCB Design with Eagle CAD
Objective
In this challenge, we will be transferring our circuit into an actual PCB (printed circuit board) by using
Eagle CAD. Refer back to the fritzing diagrams in challenge 1 for a clear image of all circuitry. Please see
the appendix for additional information on Eagle CAD.
Components
● Software: Eagle CAD
● Fritzing Diagrams from Ch. 1
Schematics makes our lives easier because a lot of information is condensed down to a legible
diagram. It completely gets rid of the breadboard and all pictorial designs. These designs are
replaced with symbols. Some of these common symbols are shown below
For a full and thorough explanation of Eagle CAD make sure to check out pre-labs 5.2, 6.1, and
6.2. Make sure to also check out additional websites such as learn.sparks.com (link in appendix)
When using Eagle CAD make sure to always do ERC/DRC after done designing schematics and
boards.
When designing the schematic remember to terminal/header pins for inputs/outputs to the
outside of the board.
Important Terminology
LED Light Emitting Diode that allows the flow of electricity in one direction
This is an example of a board design that works for the circuit we constructed from challenge 1.
Make your own board design that shows off your schematic. Again it comes down to both
creativity and effectiveness of your design. Try to use as little space as possible to avoid
unnecessary cost in production.
Task
● Use Eagle CAD to design the schematic and board design of the
circuit from challenge 1
● Personalize your board by printing your first and last name
somewhere on the board
Challenge #3a: LED Coding
Objective
In this challenge we will code in the LED portion of the game. The LEDs will light up in the same order,
with every iteration adding a new random LED.
Components
● Circuit from previous challenges
● Code from previous challenges
● Arduino IDE
Extra Tips/Resources
By setting the parameter of randomSeed to an empty pin, different seeds will be used every time
because the empty pin will generate random noise. This will then allow every time the game is played to
use a different seed so the orders won’t be the same from game to game. To see more on how the
randomSeed function works and relates to the random function, go to the Arduino documentation page
here: https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/
Task
● Read through the code below and fill in the blanks.
● Upload the code to the arduino.
● Make sure that the LEDs are lighting up in the same order for every
iteration
Code
<...> //code from previous challenges
#define ARRAY_MAX 1080
int lightArr[ARRAY_MAX] = {0}, level = 1;
void flash()
{
for(int x = 0; x < ARRAY_MAX; x++)
{
switch (lightArr[x]) //this will display each LED in the array
{
case 0:
return; //terminate function
case 1:
digitalWrite(ledPins[0], HIGH);
delay(800);
digitalWrite(__________, LOW);
delay(100);
break;
case 2:
digitalWrite(__________, HIGH);
delay(800);
digitalWrite(__________, LOW);
delay(100);
break;
case 3:
digitalWrite(__________, HIGH);
delay(800);
digitalWrite(__________, LOW);
delay(100);
break;
case 4:
digitalWrite(__________, HIGH);
delay(800);
digitalWrite(__________, LOW);
delay(100);
break;
}
}
}
void light()
{
lightArr[level - 1] = random(1, ___); //sets the next index in the array
//with a random LED(1, 2, 3, or 4)
level++;
flash(); //calls upon the flash function to display the LEDs
}
void setup() {
<...> //code from previous challenges
randomSeed(analogRead(0)); //different random seeds will be used
}
void loop() {
//testing the functions
//this can be deleted after the functions are confirmed to have worked
light();
delay(500); /*Every LED currently in the array should light up one at a
time, so at the beginning one LED will light up then 500 ms
will pass. Then the same LED will light up followed by the
next random LED in the array. This will continue 1080
times, but if it is working as expected, it can be stopped.
*/
}
Challenge #3b: Buttons Coding
Objective
In this challenge we will code the button part of the game. Each of the buttons will correspond to a
certain LED. After every iteration of the LEDs blinking, the player will have the opportunity to press the
buttons in the correct order.
Components
● Circuit from previous challenges
● Code from previous challenges
● Arduino IDE
Extra Tips/Resources
To access more information on how arrays work and how they can be utilized visit the arduino
documentation page for arrays:
https://www.arduino.cc/reference/en/language/variables/data-types/array/
Task
● Read through the code below and fill in the blanks.
● Upload the code to the arduino.
● Make sure that the correct button presses are correctly identified and
an incorrect press will end the game.
Code
<...> //code from previous challenges
bool buttonCheck(int i)
{
while(true)
{
if(digitalRead(__________) == 1) //checks if button is being pressed
{
if(lightArr[i] == __) //checks if current button press corresponds to
{ //the correct LED(red) in the array
delay(200);
return true;
}
else
{
delay(200);
return false;
}
}
if(digitalRead(__________) == 1) //checks if button is being pressed
{
if(_________________) //checks if current button press corresponds to
{ //the correct LED(yellow) in the array
delay(200);
return true;
}
else
{
delay(200);
return false;
}
}
if(digitalRead(__________) == 1) //checks if button is being pressed
{
if(_________________) //checks if current button press corresponds to
{ //the correct LED(green) in the array
delay(200);
return true;
}
else
{
delay(200);
return false;
}
}
if(digitalRead(__________) == 1) //checks if button is being pressed
{
if(_________________) //checks if current button press corresponds to
{ //the correct LED(blue) in the array
delay(200);
return true;
}
else
{
delay(200);
return false;
}
}
}
}
int buttonOrder()
{
for(int i = 0; i < _____ - 1; i++) //checks if the order of buttons
{ //pressed matches the order of LEDs
if(!buttonCheck(i)) //lighting up, using the current level
return 0; //if incorrect order, 0 is returned
}
return 1; //if the order is correct, 1 is returned
}
void setup() {
<...> //code from previous challenges
light();
while(true) //This loop runs the buttonOrder function so
{ //that if the order is correct then the game
if(buttonOrder() == 0) //will continue and if not then it will end.
break;
else
light();
}
}
void loop() {
}
Challenge #4: Scoring System and Buzzer
Objective
Create code in the arduino IDE that will keep track of a player’s current score and display it in the serial
monitor. Sound effects will also be coded using the piezo buzzer after a player gets a sequence correct
and if they get it incorrect.
Components
● Circuit from previous challenges
● Code from previous challenges
● Arduino IDE
Extra Tips/Resources
Piezo buzzers use PWM to generate the noises that you hear. To learn more about how these buzzers
work visit: https://learn.adafruit.com/using-piezo-buzzers-with-circuitpython-arduino?view=all
If you want to learn more about what PWM is look at the arduino documentation page:
https://www.arduino.cc/en/Tutorial/Foundations/PWM
To use the piezo buzzer with arduino we use the tone function, which takes in three parameters(the pin
the buzzer is connected to, the frequency, and the duration in ms). To learn more about tone go to the
arduino documentation: https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/
To experience and experiment with what frequencies correspond to what musical notes visit:
https://pages.mtu.edu/~suits/notefreqs.html
Task
● Read through the code below and fill in the blanks.
● Upload the code to the arduino.
● Make sure that the score that pops up is the correct score.
● Make sure that the correct tones are played at the right times.
void loop() {
}
Challenge #5: Designing your own housing system
using Onshape
Objective
Create a 3-D CAD model by using Onshape. Your goal is to use and get a better understanding of the
revolve, extrude, sweep, and many more features that you can use to develop a 3-D model.
Components
● Onshape
● Computer/Laptop
● 1 resistor(330 ohm)
● 1 button
● 1 LED (any color)
● 1 Piezo Buzzer
● 1 jumper wire
There are multiple ways to create models by using different features. Some features make completing
certain tasks easier, it all depends on your task. So make sure to explore multiple features to find the
feature that you find most comfortable and efficient in using.
Task
● Recreate your hardware components used on the breadboard by
using onshape.
● Combine those components to each other with the breadboard
● Finally, create a casing to house your components. Be creative.
Instructions
1. Obtain/Remove 1 of each component from your Lab Kit or if you have it attached to your
breadboard remove it for the time being.
● Button
● LED
● Piezo Buzzer
● 330 ohm resistor
● Jumper Wire
2. Recreate these components by using the features available to you on Onshape. The actual
resistor is on the right and the Onshape created model of a resistor is on the left. Do this for all parts
listed in step 1.
Actual resistor
Resistor created by Onshape CAD
IMPORTANT: Once you finish creating all components make sure to download the
breadboard import which you can find on
https://grabcad.com/library/half-breadboard-1
If you have any questions on how to import a file to Onshape make sure to watch
this video https://www.youtube.com/watch?v=6aWHf7dD3nM
3. Once you have all your parts created,
Assemble all the different components
onto the breadboard on Onshape. It
should look very similar to the picture to
the left. Or very different depending on
how you set up your breadboard.
Housing System
Both the case and circuit Another angle of both case and circuit
Appendix:
Resources
Button diagram - ECE_5_Lab_0.pdf