In yesterday’s handout, you were challenged to create a game with a joystick controller. In today’s lab activity, we’re going to work through the steps of creating one such possible game.

engineering

Description

Lab 1                                                                                                                                      Name:

Objective:

-          Joystick project

-          Review from EE 333

 

Introduction:

In yesterday’s handout, you were challenged to create a game with a joystick controller. In today’s lab activity, we’re going to work through the steps of creating one such possible game.

 

Procedure:

Step 1:

In this next part of today’s activity, let’s consider how you can use the joystick to build a simple game in which different colored lights will flash in a certain order; the player will then try to reproduce the pattern by using the joystick to highlight an LED and then press a button to select this as an entry for the sequence that is being reproduced.

Set up the following circuit for this game.

Note: although 10 kOhm resistors are shown in series with the LEDs, you will likely have better visibility with these LEDs using a lower value resistor in series with them – maybe something like 220 Ohm.

Start a new Arduino sketch.

Like last time, let’s start with variable declarations.

Declare two integers called JoyPinX and JoyPinY; they should have values of 0 and 1, respectively. Next, declare two integers called CenterX and CenterY; they should have initial values of zero. Then, declare two integers called X and Y and set their initial values as zero. These six integers will be similarly to how they were in the previous code.

Next, declare an integer value called ButtonPin and set it to have a value of 7. This represents the push button that is connected to pin 7.

Then, declare an integer called PreviousButtonPress and set it to have a value of 0. This will be used to record if the button was pressed during the previous reading of the button – it will have a value of 1 if it was a and a value of 0 if it wasn’t.

Next, set up 4 integer values, LEDPin1, LEDPin2, LEDPin3 and LEDPin4, which will represent the pin numbers on the Arduino to which each of the LEDs is connected.

Then, declare a constant integer called NumberLEDS and set it to have a value of 4. This represents the number of LEDs available to the game.

Next, declare a constant integer called MaxEntries and set it to have a value of 5. This represent the maximum number of entries for a sequence.

Next, declare an integer called NumberEntriesRequired and set it to have a value of 4. This represents the number of entries that a player must enter for a sequence to be valid.

Then, declare an integer array called LEDNum2PinMap which will be used to map the LED number, such as 0 through 3, to the pin of the Arduino that is connected to that LED. The array holds a maximum of NumberLEDS elements.

Next, delare an integer array called ComputerEntries array holds the entries of the sequence generated by the Arduino that the player must try to match. The sequence is MaxEntries long.

 

Then, declare a Boolean variable called ComputerEntryGenerated which is true if a new Arduino-generated sequence has been created and false otherwise. Set its default value to false.

 

Next, declare the integer variable NumberUserEntries which will be used to hold the current number of selections the player has entered toward matching the Arduino-generated sequence. Set its initial value as 0.

 

Then, declare an integer array called UserEntries which will hold LED identification numbers such as 0 for LED number 0 that the user has selected so far in order to match the Arduino-generated sequence. Its max value should be MaxEntries.

 


Related Questions in engineering category