CSCE 236 Embedded Systems, Spring 2013
Lab 3

Thursday, February 7, 2013
All Checkoffs Due Before Class: Thursday, February 21, 2013
Names of Group Members:

## 1  Instructions

This is a group assignment to work on during class. You only need to hand in one copy of this, but make sure that the names of all of your group members are on this sheet to receive credit. Complete all of the sections below and make sure to get the instructor or TA to sign off where required. You should keep your own notes on what you complete since parts of future homework will build on this lab.

## 2  Dimming LEDs

In this section you will take two different approaches to solving the same problem. The goal is to dim one of the LEDs whenever the button is pressed. You can dim an LED by only turning it on for a very short amount of time and then turning it off for a period of time. If you do this faster than about 30 Hz, you will not notice the flickering of the LED turning on and off and it will appear dimmer than if it is fully on (since the human eye acts as an integrator at this frequency).

### 2.1  Manual

First, you will achieve this by using manual delays. Download the sample code for this lab from the course website and examine it. Make sure you connect your LEDs and buttons to the proper pins by examining the code. This code dims the RED LED when the button is pressed. It does so by using the function delay(ms). Try changing the values of the delays and determine what values work well? Approximately what is the frequency and duty cycle that it is being dimmed at? If you increase the amount of time the LED is off, at what frequency can you start to see the LED pulsing?
The problem with this approach is that the processor is always busy doing the pulsing of the LED to dim it. If a function is called that prevents this from occurring, the LED will not dim properly. To see this, insert another delay of 100 milliseconds outside of the logic that controls the LED. This simulates the execution of other code that takes a while to complete. What happens when you press the button now?
Checkoff: Show the LED dimming when the button is pressed and have answers to the above questions.

### 2.2  PWM

To overcome the problem that you cannot easily do other actions while dimming the LED, you will now implement LED dimming using PWM. To do this, connect your LED to pin PB2 (pin 10 on Arduino, which should have the blue LED connected to it already), which is also OC1B (output compare B for timer1). Now, look at section 16 (Timer/Counter1) of the datasheet (the description of fast PWM mode in 16.9.3 and the register description in 16.11 are especially useful). Configure it such that:
• the timer is in fast PWM mode, with ICR1 as the top.
• OC1B is cleared on compare match and set at bottom (see table 16-2).
• Make sure to set all the bits in TCCR1A, TCCR1B, and TCCR1C since they may be preconfigured by other Arduino code. One way to do this is by initially setting them all to zeros (their default values) and then using ORs as you set bits
You will also need to:
• set an appropriate frequency (1KHz works well) by configuring the clock prescalar and the TOP value (stored in ICR1)
• set the duty cycle by writing to the output compare register OCR1B
• set PB2 as an output pin to enable the PWM output (this is already done in the LEDInit function).
Checkoff: Write the code using the PWM system to dim the LED when the button is pressed. Note how adding code (e.g. delay(100)) in the main loop no longer impacts the dimming.

## 3  Servo Control

Servos are also controlled by PWM signals. Most servos enable angular control of the output shaft (e.g. between 0 and 120 degrees). The servos we are using in class are known as continuous rotation servos. Instead of controlling position, we are able to control the forward and backward rotational speed using PWM signals. Most servos use PWM signals with a period of approximately 20 milliseconds. By varying the pulse length between 1ms to 2ms the servo will go from full speed forward to full speed backwards (on a traditional servo this would mean rotating from 0 to 120 degrees).
You have seen how to manually configure the PWM channels to control the frequency and duty cycle. However, instead of manually configuring all the PWM channels, we will instead make use of an Arduino servo control library. Read the information on how to use the servo library here:
http://arduino.cc/en/Reference/Servo
To hook up the servos to your breadboard, you will need to first use the 3-pin header to adapt the female servo connector to a male connector. Then, connect the black wire to ground, red to power (5V), and white to the PWM signal. Double check these connections before powering your board. Also note that the servos can draw more power than a USB port can supply. Therefore, you should not restrict the output of the servo while using USB power, instead make use of the AC adapter to power the servos whenever AC power is available.
Checkoff: Demonstrate controlling a servo by making the direction reverse when the button is pressed.

## 4  Building Robot and Driving Primitives

This section of the lab requires individual checkoff for each person. I do not expect you to complete this in lab so please come and get checked off by the date indicated above.
You should now use the supplied parts to build your own robot as demonstrated in Lab and the online instructions: http://cse.unl.edu/~carrick/wordpress/?p=130
Checkoff: Create driving primitives that enable you to drive in a straight line and also rotate a particular angle (e.g. 90 degrees). Program your robot so that when you press the button it will drive a square. The turning and distance traveled will depend a lot on the surface you are driving on, so I would recommend you make this easily tunable to adjust to different surfaces. Also, these are functions you will be using a lot for the rest of the semester. So it is worth investing a little time to make the code reusable.

File translated from TEX by TTH, version 3.89.
On 4 Feb 2013, 11:26.