0% found this document useful (0 votes)
185 views

Ece 5 Lab 5 1

This document provides instructions for building a Simon Says game using an Arduino, including wiring diagrams for circuits with buttons, LEDs, and a buzzer, and coding challenges to light the LEDs in a repeating random sequence like the popular game. Students are guided through each circuit building challenge and coding task to construct the electronic components and program the Arduino to replicate the Simon Says gameplay without purchasing the commercial version.

Uploaded by

api-599412806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views

Ece 5 Lab 5 1

This document provides instructions for building a Simon Says game using an Arduino, including wiring diagrams for circuits with buttons, LEDs, and a buzzer, and coding challenges to light the LEDs in a repeating random sequence like the popular game. Students are guided through each circuit building challenge and coding task to construct the electronic components and program the Arduino to replicate the Simon Says gameplay without purchasing the commercial version.

Uploaded by

api-599412806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

University of California, San Diego | Jacobs School of Engineering | Electrical and Computer Engineering

Simon Says -
Color Memory Game | 5

Electrical and Computer Engineering 5


<..>
Developed by Reuben Havlan, Michael Herrera, and Achyut Pillai
Overview
The objective of this lab is to build your very own Simon Says game without having to give Hasbro
your money. This game works just like Hasbro’s Simon game since there are 4 buttons that
correspond to their very own Led color of green, blue, yellow, and red. We will be able to complete
this lab by encompassing a variety of technical skills that ECE 5 has provided us with. In particular,
we are going to focus on Arduino C, our understanding of circuits, LEDs, button switches, Piezo
buzzers, and importing libraries for this project.
What You Will Need
Materials:
● 1 Arduino Mega (or Uno)
● 1 USB Cable A-B
● 1 Breadboard
● 4 Pushbuttons
● 4 LED’s (1 red, 1 yellow, 1 green, and 1 blue)
● 1 Piezo Buzzer
● 9 Resistors (330 ohms)
● 19 Jumper Wires

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

*Important: keep button circuit up for next challenge*


Challenge #1b: LED and Piezo Buzzer circuit
Objective
In this challenge we will create a simple LED circuit along with a piezo buzzer circuit. The LEDs
correspond to each of the buttons from the
previous challenge. The buzzer will be used
whenever a person loses.

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

Note: LEDs or Light Emitting DIodes have two legs. An


Anode leg(positive side) and Cathode leg(negative side).
When wiring the longer positive leg (Anode), connect it
to a pin. When wiring the shorter negative leg (Cathode),
connect it to GND (ground). Diodes allows the current to
pass in one direction. It’s important to distinguish which
leg is which since polarity matters in diodes.
Note: Piezo buzzers generate simple sounds such
as beeps and tones by using a piezo crystal. A Piezo
crystal works by changing shape whenever voltage
is applied. The crystal will push against a
diaphragm which causes a pressure wave that is
sound to human ears. Wiring, Connect the positive
leg to a pin, and connect the negative end to
GND(ground).

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

How to read a Schematic

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 additional information on this symbol, go to pre-lab 5.3


Reading a schematic might seem like a
difficult task; however it’s not so bad once
you understand some basic tips.
1. Know what the most general
components symbols are.
2. Break down the schematic into
multiple pieces
a. For example for the right
image look at the central
component and trace each
line coming from it
separately

Eagle CAD Tips and resources

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.

When designing the board


● Avoid leaving too much space between components. Try to make it as compact
as possible where the board will still function and no DRC errors will pop up.
Remember that we don’t want to pay extra for the unused portion of a board
that we don’t need.
● Avoid sharp corners and angles

Important Terminology

Power Will be a pin labeled 5V or 3.3V on Arduino

Ground Will be a pin labeled GND on Arduino

LED Light Emitting Diode that allows the flow of electricity in one direction

ERC Electric Rule Checking

DRC Design Rule Checking


Eagle CAD Schematic:

This is an example of a schematic that works


for the circuit we constructed in challenge 1.

DO NOT COPY THIS SCHEMATIC

Make your own schematic that resembles the


fritzing diagrams and circuit of the project.
This is where creativity comes into play on
how you want to arrange everything, and you
could improve upon this design in both its
efficiency and effectiveness to be read easily.

This is an example of a board design that works for the circuit we constructed from challenge 1.

DO NOT COPY THIS BOARD DESIGN

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.

*Important: make sure your serial monitor is at 115200 baud*


Code
<...> //code from previous challenges
void setup() {
<...> //code from previous challenges
Serial.begin(115200); //initializes the serial monitor to 115200 baud

light(); //code from challenge 3b


while(true) //code from challenge 3b
{
score = ________; //determines the score based off of last level passed
Serial.print("Current Score: ");
Serial.println(score);
if(buttonOrder() == 0) //code from challenge 3b
break; //code from challenge 3b
else //code from challenge 3b
{ //buzzer will sound if player gets sequence right before moving on
____(______, 1046, 100); //This function causes the buzzer to emit
delay(200); //1046 Hz for 100 ms. The delay timer and
____(______, 1046, 600); //the timer used for the buzzer are separate
delay(1100); //so amount of time played needs to be added
//to amount of time in between sounds.
light(); //code from challenge 3b
}
}
Serial.println("Wrong Order! Game Over!");
//if incorrect order this buzzer sequence will play
____(______, 466, 500);
delay(600);
____(______, 440, 500);
delay(600);
____(______, 415, 500);
delay(600);
for(int i = 0; i < 4; i++)
{
____(______, 392, 100);
delay(100);
____(______, 393, 100);
delay(100);
}

Serial.print("Your final score was: ");


Serial.println(score);
Serial.println("Click the reset button on your Arduino to try again");
}

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.

Example of OnShape CAD with Breadboard

4. Finally Create a casing to surround the


circuits and wires.

5. You can design your casing however you


like. As you can see in our design, we
tried to make it look like an actual Simon
Says Board with transparent buttons
which allows you to see the breadboard
within.

Housing System
Both the case and circuit Another angle of both case and circuit
Appendix:

Resources
Button diagram - ECE_5_Lab_0.pdf

LED diagram - obtained from Electronic Hobbyist


https://electronicshobbyists.com/arduino-blink-tutorial-interfacing-arduino-led-using-arduino-d
igitalwrite/led-diagram/

Piezo Buzzer diagram and info - obtained from learn.adafruit.com


https://learn.adafruit.com/using-piezo-buzzers-with-circuitpython-arduino?view=all

Frequencies of different notes


https://pages.mtu.edu/~suits/notefreqs.html

Electrical Symbol for Schematics - obtained from


https://removeandreplace.com/2016/03/23/electrical-schematic-symbols-names-and-identifica
tions/

Eagle CAD resources:


● Pre-Lab 5.2 - Into to PCB design with Eagle
● Pre-Lab 5.3 - Circuits, Symbols, and components
● Pre-Lab 6.1 - Eagle Part 1: Schematic
● Pre-Lab 6.2 - Eagle Part 2: Board
● https://learn.sparkfun.com/tutorials/using-eagle-schematic/all
Onshape CAD resources:
● Import Breadboard: https://grabcad.com/library/half-breadboard-1
● Explanation on how to Import files
https://www.youtube.com/watch?v=6aWHf7dD3nM
● Pre-Lab 13.1 - Intro to Onshape
ECE 5 Lab Template

You might also like