Link Search Menu Expand Document

Embedded Design and Engineering

Last day for on-time checkoff: Tuesday, 10/24

Prelab due 10/16/22 at 11:00 am

Writeup due 10/23/22 at 11:00 am

About

In this lab, you will translate requirements for a game into a Finite State Machine (FSM) description of the game implementation. You will then implement the FSM to run on your Arduino, to produce a game that is played with a physical gamepad and displayed on an LCD screen. By the end of this lab, you will have used a traceability matrix to map behavioral requirements to FSM transitions guards and behaviors and have practiced creating both FSM transition descriptions and code to match an FSM.

meme

Lab 5 Rubric

Resources

Starter code

Materials

Included in your kits:

  • Arduino MKR1000 and USB cable
  • 2 small or 1 large breadboard
  • 1 16x2 LCD screen
  • 1 Potentiometer
  • 4 1MΩ resistors
  • 1 2KΩ resistor
  • Jumpers/wires

Provided:

  • Materials for gamepad

Steps

  1. Construct a capacitive-touch game controller out of tape, cardboard, and foil. While you are waiting for materials to come around to you for this step, work on Steps 2 and 3.

    arts_and_crafs

    1. Obtain the following materials: four pieces of wire, about 6” (15cm) long, ends stripped; four squares of aluminum foil, measuring about 0.5” (1cm) per side; one square of packing tape; one square of cardboard the same size or slightly larger than the tape. (Optional) also obtain a piece of tape (length slightly greater than the diagonal of your cardboard square) cut in half lengthwise, for step e, and a short piece of tape, for step f.

    2. Place the square of tape sticky-side up. Stick one end of each wire to the center of each edge of the tape, such that the exposed metal is entirely within the square.

    3. Stick a square of foil over each wire end on the tape. Take care that none of the squares of foil touch each other.

    4. Stick the piece of tape onto the cardboard square, such that the wire ends and foil are sandwiched between the tape and cardboard.

    5. (Optional) secure the controller with more tape and use a permanent marker to indicate where the foil squares are. Here, we used one long strip of tape cut in half lengthwise, and placed each piece diagonally across the square to make an “x,” folding the tape over the edges of the cardboard. We also colored in one of the foil indicators, so that we can orient the square.

    6. (Optional) roll up a piece of tape so that the sticky side faces out, and secure it to the back of your controller, so that you can stick the controller onto your table for stability. We recommend doing this step after wiring up your circuit.

  2. Compare your state chart transition table from the prelab with your partner’s, and resolve any discrepancies. Working with your partner, answer the following questions (in the past, we’ve had students fill out a traceability table, but this took a significant chunk of the lab. This year, we’re asking you to talk through two requirements in some detail, to get a sense of what it means to check a box in a traceability matrix. You can view a partial example traceability matrix here.):

    1. What would it mean if the RO1-A,B,C columns did not have any boxes checked off in the traceability matrix?

    2. Which state(s) and transition(s) satisfy RO5-A and why?

    3. Which state(s) and transition(s) satisfy RO-11 and why?

    Get your answers checked off by a TA. While you’re waiting, wire up your circuit.

  3. Wire up the following circuit. It is color-coded for clarity (the red lines go to +5V, the black lines go to ground, the orange line goes to the potentiometer, the blue lines go to the LCD, and the green lines go to the gamepad). The sub-steps of this step give you several hints and reference photographs for wiring, which we highly recommend you follow. We also suggest using both partners’ breadboards, because one breadboard does not fit both the LCD and the Arduino.

    lab5_circuit

    1. For wiring up the gamepad, it does not matter which wire of the gamepad goes into which of Arduino pins 7-10. You will configure this in code. We suggest using the red + rail of the breadboard to connect pin 11 to the resistors connected to pins 7-10, as shown. We also suggest putting the gamepad wires into the pin headers on the Arduino, rather than in the breadboard. Because the resistor legs are uncovered metal, take care to make sure that they are not touching each other, which may cause a short between two Arduino pins.

      gamepad_hint

    2. For wiring up the LCD, be sure to connect the upper ground rail of the LCD breadboard to the ground rail of the Arduino breadboard (which should be connected to the GND pin of the Arduino, just like in previous labs). We also suggest running a jumper from the Arduino 5V pin to the upper red “+ rail” of the LCD breadboard. Then, you can connect the potentiometer and relevant pins of the LCD to these rails. We also suggest putting the wires that run from the LCD to the Arduino into the pin headers on the Arduino, rather than in the breadboard, as shown.

      lcd_hint

  4. Clone or download a zip of the Lab 5 starter code. You can find the repository URL (for cloning) and the link to download a zip by pressing the <> Code button. Open the lab5 folder and open the lab5.ino file in your IDE. In the Library Manager (Tools -> Manage Libraries), install the “CapacitiveSensor” and “LiquidCrystal” libraries if they are not installed already. Open the Serial Monitor, and upload and run the code on your Arduino.

    1. You should see “CALIBRATING…” scroll across the LCD screen. If you do not see this, you may need to turn the potentiometer to adjust the contrast of the screen; or you have wired the LCD incorrectly and should double-check your circuit (especially check for loose wires!).

    2. On the serial monitor, you should see “Capacitive sensing:” and then four values, preceded by 7, 8, 9, 10. Decide on an orientation of your gamepad (i.e. decide which side is “up”). Press your finger to the gamepad sensor that you decided corresponds to “up”, and observe which of the numbers goes up on the Serial Monitor. What threshold do you observe such that values under that threshold mean the gamepad sensor is not pressed, and values above the threshold mean that the sensor is pressed? Change the corresponding values under the comment LAB STEP 4b of the code such that cap_sensors[UP] is one of 7, 8, 9, 10 corresponding to the value you observed changes, and thresholds[UP] corresponds to the threshold you decided. Repeat for the other three sensors.

    3. Comment out the calibrate() function call under the LAB STEP 4b comment and uncomment the test_calibration() function call under the LAB STEP 4c comment. Upload and run your code. Observe the LCD screen: it should display nothing when no sensors on the gamepad are pressed, and display “X pressed” when sensor X is pressed, where X corresponds to UP, RIGHT, DOWN, or LEFT. If one or more sensors is miscalibrated, repeat Step 4b or check your circuit.

    4. Comment out or remove the calibrate() and test_calibration() lines. Your code is now set up to take inputs from the gamepad.

      A note on the code: a lot of the underlying behavior (setting up the sensor and LCD, displaying custom characters on the LCD, reading sensors) is abstracted away into the lab5_utils.ino file, along with the helper functions for this lab. While you do not need to read or modify this file for this lab, you may find it helpful refer to this file as well as lab5.h to see how certain types (like state, orientation, and xyo) are defined and used. If your course project will use capacitive sensing or the LCD screen, you should take a look at the code on your own time for a starting point on how these components are set up.

  5. Implement two helper functions (under the comments reading LAB STEP 5) that correspond to your answers to prelab Q2 and Q3.

    You should use switch…case for these functions, instead of if/else if/else. Remember to put a break at the end of every case!

    Write descriptive documentation above both functions.

  6. Under the comment reading LAB STEP 6, initialize all of the variables according to prelab Q4. x, y, and o are done for you as an example of how to use the xyo type.

  7. Under the comment reading LAB STEP 7, implement the FSM that you and your partner decided on in step 2.

    The update_fsm function takes the current state and the three inputs described in the prelab. Just as described in the prelab, the order of operations is: 1) check the transition guard; 2) output something on the LCD; 3) modify the state variables (if needed); and 4) return the next state.

    You should follow the conventions described in class (which you will be graded on for this lab), namely that the FSM is implemented as a switch...case on the state input, where each case block checks the transitions and performs actions if the transition needs to be taken. Some of the transitions from the prelab have been done for you, as a reference. The code is set up to return the current state for situations when a transition is not taken.

    1. Following the examples in the code, comment each transition guard checking with the annotation of the transition, e.g. a-b for a transition going from state a to state b (the state numbers of a and b suffice, rather than the full state name).

    2. With your partner, go through this code and check that each transition defined in the table of prelab step 4 has an annotation in the code. Also make sure that each transition produces an output by calling one of the three functions that displays something on the LCD.

    3. Upload and run your code, and try playing the game. Even if the code does not work exactly correctly, get it checked off by a TA now.

    4. Spend up to the end of lab time debugging your code and state chart, but do not worry if you do not get it fully working – the point of the next lab is to test this code, so keep your circuit and make sure both partners have a copy of the code.

  8. Turn in your work:

    1. Save your code as “lab5.ino”. Upload this to the “Lab 5 Code” assignment on Gradescope (include all partner(s) on the submission). You do not have to upload the helper files we gave you. It is important to turn the code in so that you can earn the code style points from the rubric.

    2. INDIVIDUALLY, complete the Lab 5 writeup assignment on Gradescope.