exercises and reference

1
USING SHIELDS AND LIBRARIES
Microcontrollers and Interfacing
week 11 exercises
1
Using shields and libraries
Large or sophisticated input/output devices are often available as dedicated hardware shields that plug directly into the Arduino
pins. These devices usually require complex software, and so they are often accompanied by a library that makes using them
very easy.
1.1
Example shield: the LoL Shield
LoL stands for Lots of LEDs. A LoL Shield has 126 LEDs that can be turned
on or off individually. The shield uses 12 of the 14 digital pins to control the
LEDs. The scheme for addressing the individual LEDs is quite complex. To hide
the complexity, the designer of the LoL Shield provides a library that hides the
complexity and presents a very simple interface to Arduino sketches.
1.2
Installing the hardware and library
Obtain a copy of the LoL Shield library ‘LoLShield v82.zip’. If you are using a lab computer, obtain a copy of the file
from the instructor. If you are using your own computer, download the file from this web page:
https://code.google.com/p/lolshield/downloads/list
Install the library. There are two ways to do this. The hard way is to unpack the .zip file in your Arduino/Libraries
folder and then restart your Arduino IDE. The easy way is to use the IDE, by clicking on
Sketch > Include Library > Add .ZIP Library
and then selecting the file you just downloaded.
Plug the LoL Shield into the Arduino. There is only one orientation that will work. Carefully line up the pins before pressing,
and do not force the shield into place if it seems to resist being plugged in.
Test the LoL Shield by opening an example sketch. Click on
File > Examples > LoLShield > LoLShield BasicTest
to open a window with the example sketch. Compile the sketch and upload it to the Arduino.
1.3
#include <Charliplexing.h> // LoLShield library
Using the LoL Shield library
The library exports several functions, including:
Init(uint8 t mode = 0) initializes the library. The
mode argument can be used to enable support for doublebuffering and greyscale.
Set(uint8 t x, uint8 t y, uint8 t c = 1)
turns on the LED at coordinates (x, y). To turn it on
full brightness, the value 7 should be passed as the third
argument c.
Clear(uint8 t c = 0) sets the brightness of all the
LEDs to c. (The default value 0 turns them all off.)
Create a sketch that turns on each LED in turn for 20 ms.
, 1 Ask the instructor to check your work.
void setup(void)
{
LedSign::Init(0);
}
// initialize the library
void loop(void)
{
for (int y= 0; y < 9; ++y) {
for (int x= 0; x < 14; ++x) {
// turn on LED at (x,y) full brightness
LedSign::Set(x, y, 7);
delay(50); // 20 ms
// turn off LED at (x,y)
LedSign::Set(x, y, 0);
}
}
}
1 of 2
A
LOLSHIELD API
Modify your sketch to ‘bounce’ a dot around the array of LEDs.
The LED at the position of the dot will be on, all other LEDs will be off. Begin with the dot at position (0, 0), and the
corresponding LED turned on. Every 50 ms add the vector (1, 1) to the dot’s position, so that it occupies positions (1, 1), then
(2, 2), then (3, 3), and so on. Whenever the dot moves, turn off the LED that is at the old position, move the dot, then turn on
the LED at the new position.
initial position (0,0)
initial direction (1,1)
‘bounce’
When the dot ‘hits’ the bottom of the LoL Shield, at position (8, 8), reverse the vertical direction of motion; in other
direction
direction
(-1,1)
(-1,-1)
words, begin adding (1, -1) to the position at each step. When
the dot ‘hits’ the right edge, at position (13, 3), reverse the
‘bounce’
horizontal direction; in other words, begin adding (-1, -1) to
the position at each step.
When the dot reaches the top (or left) edge, reverse the
direction
(1,-1)
vertical (or horizontal) direction again. If you do this cor‘bounce’
rectly, the dot should bounce around the array as if it were a
ball bouncing around the inside of a box with no friction.
, 2 Ask the instructor to check your work.
‘bounce’
‘bounce’
Modify your sketch to allow manual control of the dot. Remove the code that performs the bouncing. Instead, read characters
from the Serial input. If one of the characters W, A, S or D is read, move the dot up, left, down or right (respectively). You
will also have to modify the ‘bounce’ logic. For example, if the dot is moved off the edge of the array, you could make it
reappear on the opposite side.
You can use the serial monitor to send the movement command characters. Type the desired movement sequence into the
top text entry area (e.g., ‘DDDSSS’ for ‘right 3, down 3’) and then press ‘send’.
If you have a copy of the screen program, you could use it to connect to your USB serial device and control the dot’s
movement in ‘real time’.
, 3 Ask the instructor to check your work.
Reference
A
LoLShield API
The LoLShield uses digital pins 2 through 13. (Pins 0 and 1 are still available, but are also used by the USB serial communication. The six analogue pins can also be configured as digital inputs/outputs if necessary.)
#include "Charliplexing.h"
Init(uint8 t mode = 0)
initializes the library. The mode argument can be used to enable support for double-buffering (DOUBLE BUFFER) and/or
greyscale (GRAYSCALE).
SetBrightness(uint8 t brightness)
sets the overall brightness of the screen, between 0 and 127.
Set(uint8 t x, uint8 t y, uint8 t c = 1)
turns on the LED at coordinates (x, y). To turn it on full brightness, the value 7 should be passed as the third argument c.
Clear(uint8 t c = 0)
sets the brightness of all the LEDs to c. (The default value 0 turns them all off.)
Flip(bool blocking = false)
swaps the front and back buffers, if double-buffering is enabled.
Horizontal(uint8 t y, uint8 t c = 0)
fills an entire row with the specified value (0 for off, 7 for full on).
Vertical(uint8 t x, uint8 t c = 0)
fills an entire column with the specified value (0 for off, 7 for full on).
2 of 2