The Secret Word Game - Make: Basic Arduino Projects - 26 Experiments with Microcontrollers and Electronics (2014)

Make: Basic Arduino Projects - 26 Experiments with Microcontrollers and Electronics (2014)

Chapter 26. The Secret Word Game

In the mid-1970s, a company named Tiger Electronics made a variety of handheld electronic sporting games like baseball, basketball, and football. These portable handheld games used LEDs, seven-segment LED displays, and speakers to provide visual and audio effects for the units. The pushbuttons on these portable gaming devices allowed total interaction between the player and the electronic handheld unit. In other words, while they weren’t a Wii, these little electronic units were the first really popular interactive sports games.

Imagine being able to make your own retro portable electronic game using components from the Ultimate Microcontroller Pack. The Secret Word game is a cross between Jeopardy! and Charades, in which players attempt to guess the message programmed into the Arduino microcontroller within a certain amount of time. The rules of the game are quite simple and will be discussed later in this final chapter of the book. The assembled Secret Word Game is shown in Figure 26-1.

Parts List

§ Arduino microcontroller

§ Full-size breadboard

§ R1: 330Ω resistor (orange, orange, brown stripes)

§ R2: photocell (light sensor)

§ R3: 1KΩ resistor (brown, black, red stripes)

§ LCD1: 16x2 liquid crystal display (LCD), part number JHD 162A

§ LED1: RGB (red, green, blue) LED

§ S1: pushbutton switch

The Secret Word Game

Figure 26-1. The Secret Word Game

Let’s Build a Secret Word Game

The Secret Word Game is a little tricky to build because of the wiring. Therefore, you’ll have to use the full breadboard that comes with the Ultimate Microcontroller Pack to adequately space the parts, as shown in Figure 26-1. Use the Fritzing wiring diagram shown in Figure 26-2 to build the Secret Word Game on the full-size breadboard.

Pin 1 of the LCD is the leftmost input at the base of the screen. Pins 2 to 16 continue to the right. (Another way to identify pin 1 is by the small circle placed on the PCB right next to pin 1.)

The photocell and RGB LED should be placed on the breadboard so that they are easily visible and accessible; you need to clearly see the LED, and easily shine a light on the photocell.

The Secret Word Game Fritzing wiring diagram

Figure 26-2. The Secret Word Game Fritzing wiring diagram

In past projects, we used the smaller sized MakerShield to keep the projects compact. But with this many electronic components and wires in the Secret Word Game, the full-size breadboard is the best choice for this project. And as always, be sure to recheck your wiring against the Fritzing diagram, to catch any possible errors.

TECH NOTE

Before you upload the Arduino sketch to the Secret Word Game, have a set of fresh eyes (like a friend or parent) look at the electrical wiring on the full-size breadboard to catch any mistakes you might have missed.

Upload the Secret Word Game Sketch

With the Secret Word Game wiring on the breadboard completed, now it’s time to upload the Arduino sketch. Here are the steps you’ll need to follow:

1. Attach the Arduino microcontroller to your computer using a USB cable.

2. Open the Arduino software and type Example 26-1 into the software’s text editor.

3. Upload the sketch to the Arduino microcontroller.

The Arduino microcontroller is now programmed with the Secret Word Game sketch. The LCD will be blank and the RGB LED turned off. When you press the “Start Game” pushbutton, the RGB LED will light up. The red, green, and blue LEDs will sequence five times before turning off.Figure 26-3 shows the RGB LED sequencing after the Start Game pushbutton has been pressed.

The Secret Word Game starting its timing sequence using the RGB LED

Figure 26-3. The Secret Word Game starting its timing sequence using the RGB LED

Once the RGB LED has turned off, shining a light on the photocell will reveal the secret word on the LCD (Figure 26-4). Removing the light from the photocell will erase the secret word on the LCD. New secret words can easily be uploaded to the Arduino by changing one line of instruction in the sketch.

The secret word “Cat” being revealed on the LCD

Figure 26-4. The secret word “Cat” being revealed on the LCD

Example 26-1. The Secret Word Game sketch

/*

Demonstrates the use of a 16x2 LCD. A brief press of the Start Game

pushbutton will turn on the RGB LED timing sequencing. The RGB LED turns

off and the secret word can be revealed by a shining light on a photocell.

25 August 2013

by Don Wilcher

*/

// include the library code:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int buttonPin = 6; // the number of the Start Game pushbutton pin

int RPin = 7; // select the pin for the red LED

int GPin = 8; // select the pin for the green LED

int BPin = 9; // select the pin for the blue LED

// variables will change:

int buttonStatus = 0; // variable for reading the Start Game

// pushbutton status

void setup() {

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

// declare the LED pins as outputs:

pinMode(RPin, OUTPUT);

pinMode(GPin, OUTPUT);

pinMode(BPin, OUTPUT);

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

}

void loop() {

// read the state of the pushbutton value:

buttonStatus = digitalRead(buttonPin);

// check if the pushbutton is pressed

// if it is, the buttonState is HIGH:

if (buttonStatus == HIGH) {

lcd.clear();

delay(500);

for (int i=0; i <= 5; i++){

lcd.setCursor(8,0);

lcd.print(i);

// turn the red LED on:

digitalWrite(BPin, HIGH);

digitalWrite(RPin, LOW);

// delay red LED for 1/2 second:

delay(500);

// turn the green LED on:

digitalWrite(RPin, HIGH);

digitalWrite(GPin, LOW);

// delay green LED for 1/2 second:

delay(500);

// turn the blue LED on:

digitalWrite(GPin, HIGH);

digitalWrite(BPin, LOW);

//delay blue LED for 1/2 second:

delay(500);

}

} else {

//turn red, green, and blue LEDs off:

digitalWrite(RPin, HIGH);

digitalWrite(GPin, HIGH);

digitalWrite(BPin, HIGH);

// print a Secret Word to the LCD:

lcd.setCursor(0,0);

lcd.print("Secret Word is:");

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

lcd.setCursor(0, 1);

// print the number of seconds since reset:

lcd.print("Cat"); // change secret word or phrase here!

}

}

TECH NOTE

An LCD can also print mini messages in addition to words.

The Secret Word Game’s block diagram is shown in Figure 26-5. The Fritzing circuit diagram is shown in Figure 26-6. Circuit schematic diagrams allow you to build electronic devices quickly. Electrical/electronic engineers and technicians use them to design, build, and test cool interactive electronic products for games, testing equipment, robots, and automobiles.

The Secret Word Game block diagram

Figure 26-5. The Secret Word Game block diagram

Rules for the Secret Word Game

The objective of the game is to guess the mystery word programmed into the Arduino microcontroller within a certain amount of time. Three hints will be given to the players by the game host, after which a pushbutton switch is pressed to start the RGB LED timing sequence. The players will write their answers on a sheet of paper. Once the RGB LED timing sequence is completed, the game host reveals the secret word to the players by shining a flashlight on the photocell. The player who has the winning word will shout out, “I’m a Winner,” or some other suitably cool phrase! The game host starts a new game by changing the secret word using the lcd.print() instruction of the Secret Word Game sketch. The new sketch is then uploaded to the Arduino microcontroller for the next round of electronic gaming fun!

Something to Think About

How can a mystery phrase be programmed for the game?

The Secret Word Game Fritzing circuit schematic diagram

Figure 26-6. The Secret Word Game Fritzing circuit schematic diagram