Laboratory 2: Timed Game: Color Match

Laboratory Description

In this lab, a simple game is built where a player must press a button to match a target color, presented in a sequence. A timer is to control the sequence display time and is also used to limit the answer window time and to provide program delays.

Laboratory Goals

This laboratory is designed to further a student’s understanding of:

  • Using state diagrams, flow charts, and/or pseudocode to aid in algorithm development,

  • The use of timers in producing delays and timed events, and

  • The implementation of interrupts.

  • Techniques to produce more accurate and efficient code.

Preparation

Students should complete Activities 6, 7, and 8 prior to starting the laboratory.

Hardware and Tools

  • TI-RSLK Robotic Car or Launchpad Board

  • Breadboard

  • Components (provided):

    • 1x Pushbutton

    • 1x BiColor LED

    • 3x Regular LEDs

    • Various Resistors (1x 1kΩ, 1x 510Ω, 3x 330Ω)

  • Wire (available in classroom)

Project Template

Game Description

Important

The description of this lab is done almost entirely through text and video demonstration; as opposed to providing a supplementary flowchart as was done in Lab 1. Future labs will generally have additional figures or pseudocode to aid in understanding and development.

The main game loop consists of the player attempting to press a button when the Launchpad Board RGB LED is lit to a specific target color. The RGB LED is cycled through 7 different colors in sequence [RED, GREEN, BLUE, YELLOW, MAGENTA, CYAN, WHITE], where the sequence display speed is increased after each correct entry, completing a “round”. The player attempts to answer correctly as many times as possible, with the number of correct answers (or completed rounds) entered being the player’s final score. The game ends when the player runs out of “lives”. The player starts with 3 lives and loses one after an incorrect answer or a round “timeout,” where the player takes too long to answer during a round. If either of these events happen, the round is restarted with a new target color.

The target color for each round is selected randomly by the program and is displayed on the terminal, along with the current round number. The game will not repeat a color until all colors are used at least once; and then not again until all colors are used at least twice, and so on. As such, the program must track what colors have been displayed.

The difficulty of each subsequent round is increased after a correct answer by increasing the speed at which the color sequence is displayed. During the first round, each color will be displayed for 750 ms. This time is reduced by 10% for each successfully completed round.

Three regular LEDs (e.g., RED) are used to indicate the player’s remaining lives. At the beginning of the game, all three LEDs are lit. If a life is lost, one of these LEDs are turned off. These LEDs should be wired such that they are in a row and easy to interpret. Additional LEDs may be found in the drawers on the center table.

As noted, a life may be lost to a round “timeout”. This is triggered when the player does not enter an answer for the current round within 10s. At 10s, the round should immediately end, a loss of life is indicated, and next round starts; assuming at least one life is remaining.

A BiColor LED (BiLED) is implemented to indicate the player’s success or failure. The BiLED is typically OFF but turns GREEN for 0.5s after a successful round and RED for 0.5s after an unsuccessful round. During this time, the RGB LED is off. After this diplay, the next round starts. After the game ends, the BiLED repeatedly flashes OFF and ON every 0.5s, where the ON state toggles between RED and GREEN each cycle. At any point during this cycle the user can start a new game.

A new game starts with the press and release of the pushbutton. At the start of a new game, the round counter and timings are reset and the RGB LED is lit white for 1s. After this 1s, the RGB LED is turned off and the first round starts (with the RGB LED immediately turning back on to start the cycle).

A demonstration of the laboratory functionality is provided in the video below without narration. Please note that this demonstration does not include the LEDs used to indicate remaining lives. Also note that before the game starts, the RGB LED sequence continuously displays. This display is not required functionality. Similarly, the terminal presents the next round timing after a successful answer (675…:code:607) and also displays a $ to indicate correct; neither of these are required.

Notes

  • The RGB LED is LED2 on the Launchpad Board and RSLK and is connected to P2.0 (RED), P2.1 (GREEN), and P2.2 (BLUE).

  • It is assumed that one of the RSLK bumpers is used to enter a guess. This is not required: groups may decide to use other means to enter a guess.

  • If using a button to indicate a guess, the guess should be registered on the button press, not the release.

  • Prior to starting a round, the program should ensure that the guess entry button is not pressed. If it is pressed, the program should wait until it is released.

Part A: Planning and Preparation

The first checkoff of this lab consists of producing a plan for implementing the game. The group should work together to build a comprehensive program design. This design must consist of one or more flow charts thoroughly describing the actions the program must take. The flow charts elements must describe single events or actions taken; they should not be summaries of multiple functionalities. For example:

  • Good flow chart items:
    • light RGB LED required color

    • check for bumper 0 press

    • wait 1 second

  • Bad flow chart items (without clarification):
    • adjust RGB LED with bumper presses

    • Display response

    • Read user’s sequence

Groups are encouraged to explore the possible use of state diagrams, though they are not required. Likewise, the program may be implemented using either a linear progression type implementation or a finite state machine; as described in the lecture; or any other means as viable.

Checkoff: The design produced by the group should be submitted to Gradescope. These designs will not be graded in-person. Do not expect feedback for these designs prior to working on the lab in earnest.

Part B: (Class 2) Preliminary Development

Download and use the TemplateProject.zip template project for this lab.

Groups are expected to build pieces of the full game. This includes finishing:

  • GPIO Initialization:
    • Bumpers, pushbutton, and LED all initialized, wired, and working.

  • Timer Initialization:
    • Program a Timer_A module to provide a useful period to your program: select a desired overflow time to allow for a reasonable time resolution towards measurement/control of LED display times and delays. You may use multiple timers if desired.

  • Required Functions:
    • A function void setRGB(int8_t color) which will light the RGB LED to the requested color, which is specified by a number color. Number-to-color association is up to groups to decide. The function should be able to turn the RGB off as well.

  • Game Starting Logic
    • Printing of instructions, wait for pushbutton press, and initial display of the RGB LED as white.

  • Code demonstrating above functionality:
    • Provide some code that is used to demonstrate the successfulness of the work above (excluding “Game Starting Logic”). GPIO initializations should be able to be verified by reading inputs and setting outputs. Timer initializations can be demonstrated with a second counter (e.g., the stopwatch activity) or a flashing LED at a given rate.

Important

While Activity 8 introduced the use of interrupts with the GPIO, we highly suggest that GPIO interrupts are not used in this lab as they can be very difficult to implement and debug for this type of program.

Timer interrupts will still be needed, however.

GPIO Selection

Groups may decide which port pins to use for each component but should limit pin selection to those not used by the RSLK. The pin map: RSLK_MAX_pin_map.pdf, specifies all default connections. Do not use any pins that have an entry in the “TI_RSLK Connections” column. Be aware that many RSLK cars and Launchpad Board may have faulty pins and selected pins might have to be adjusted with respect to the device used.

As listed in this document, the RGB LED is connected to:

  • P2.0: Red Channel

  • P2.1: Green Channel

  • P2.2: Blue Channel

where each segment of the LED is connected to ground (such that outputting a HIGH signal will turn the channel on). Secondary colors (Yellow, Magenta, Cyan) may be generated by turning two channels on at once and white is produced when all channels are on:

../_images/lab2_rgb-mixing.svg

RGB Additive Color Combinations

Wiring of all the external components should be consistent with the wiring required for Laboratory 1.

All inputs should have debouncing added for both presses and releases. This may be done with the __delay_cycles() function, as opposed to with the Timer; however, using the Timer to provide this functionality is acceptable. A suggested delay for debouncing is 10 ms.

Delays

The timer usage as presented in Activities 7 and 8 show how to measure time but do not show how to produce delays. Delays in code may be done through while loops, waiting for the delay condition to be met. For the case of timers, the code can be configured to wait for a specific number of timer resets/interrupts to occur. Code may be added within the delay while loop if additional functionality is required, otherwise the loop can be empty, {}, or terminated with a semicolon, ;.

timer_resets = 0;         // assuming this variable is incremented in timer ISR
while(timer_resets < 10); // wait for 10 timer resets.

Hint

Multiple global variable counters can be added to a single timer overflow/reset ISR to keep track of different timed events that may occur simultaneously.

These delays must be implemented using interrupts and not through the methods presented in Activity 7.

Checkoff

A successful checkoff for this portion of the lab will include a demonstration of the components as listed above.

These components may be demonstrated independently (e.g., commenting/uncommenting code, two separate projects, etc.). The grader will also request to see the associated GPIO and Timer initializations. Be prepared to discuss the implementation, specifically, the timer period selected and justification.

Part C: (Class 3) Finished Game

Complete the remainder of the game, get checked off, and submit your code to the Gradescope assignment.

For the code submission, your code must have:

  • group member names,

  • section,

  • side, and

  • seat number

added within the comment block at the top of the code. The laboratory will not be considered complete if this information is lacking.