Lab Exercise 5:
The purpose of this lab time is to give you practice with code organization and thinking about coding up sequential processes, like simple games or interactions. The ultimate goal of the project is to create a game to test your reaction speed.
Tasks
Board
The lab and project will use a simple configuration of the board that uses the capacitive sensor and four LEDs. Pick out two green and two red LEDs. You will also need wires and alligator clips for six of the capacitive sensors.
- Set up your board to drive four LEDs and 560 Ohm resistors (picture below). Make sure the long pin is on the side connected to the board; the resistors should connect the shorter pin to ground. I'd suggest using pins 2, 3, 4, and 5. When using serial port communication, pins 0 and 1 are used for that purpose.
-
Open a new file and save it as flash. Start with the template below.
#include <Wire.h> #include "Adafruit_MPR121.h" // create the MPR121 object Adafruit_MPR121 cap = Adafruit_MPR121(); // main program: detect whether a sensor is touched or not int main() { init(); // Setup section Serial.begin(9600); Serial.println("MPR121 Cap touch test"); // make sure we can talk with the capacitive sensor if( !cap.begin( 0x5A)) { Serial.println("MPR121 not found"); return(0); // exit } Serial.println("MPR121 found"); // body of code here return(0); }
-
Flashing Lights: Use the
pinMode()
to set the LED pins to OUTPUT and then write code to make all of the LEDs turn on and then turn off one at a time, putting a 250ms You don't need to put this in a continuous loop unless you want to do so. Test your circuit. -
Efficient Coding: Notice all of the commands you had to write to do the simple task above: is there a lot
of repetition? What we need is a function that lets us specify the state of all of the LEDs in a single command.
Inputs: In order to set the value of all of the LEDs, we need two pieces of information: which LEDs (pins) to set and what value to set them to. We can pass in which pins to use with an array. We could also pass in their new values as an array, but it is more efficient to use bits, since each LED can be only on or off. Because we want the function to work for any number of LEDs, we also need to pass in the number of LEDs.
Action: The function should examine each bit of the state input and set the corresponding LED to on or off. Fill in the function using the template below. To test if bit i in value is a 1: left shift a 1 by i, then bit-and (& operator) the expression with value.
int setLED( int pins[], int numPins, int value ) { // for the number of pins // if bit i in value is a 1 // use digitalWrite to set the pin to HIGH (1) // else // use digitalWrite to set the pin to LOW (0) return(value) }
- In your main function, make an int array called ledPins of length four and assign it the pin numbers you used for the LEDs on the board. Create a second const int variable, numPins, with the initial value of 4.
- Delete or comment out your prior body code for making the LEDs work. Instead, write a for loop that repeats 4 times. In the body of the loop, call setLED once with a value argument of 0xF, delay for 250ms, call setLED again with a value of 0, then delay again for 250ms. Download and test your code. How would you light up all of the LEDs and make the LEDs turn off one at a time?
When you are done with the lab exercises, you may start on the rest of the project.