CS 1020 - Lab 3 - More programming: parameters, return values,
In this lab, you will gain experience with parameters and return
values, and also experiment with the brightness sensors. We will
supply each group with a pair of sensors and a flashlight. Start by
getting to know your new group members, and familiarize yourself with
the new Sciborg that you will use for this lab. (One of you should
know it already!)
Attach the two brightness sensors to analog inputs 5 and 6 of your
Handy Board. Use port 5 for the left sensor, and port 6 for the right
one. Turn the Handy Board's user knob past option 7 and observe the
values returned by the sensors as you illuminate them with the
In exercise 4 below you will program the Sciborg to
follow the light source. For this to work, the sensors need to be
mounted such that they measure the light received from the front left
and front right. You may want to build a small Lego enclosure for
each sensor that blocks the light coming from the sides and from
behind the sensor. Once your sensors are installed properly, they
should record roughly equal values if the light is coming from
straight ahead; but if the light is coming from the side, the
corresponding sensor should report a much smaller value than the other
Once you're done, add your names and your group number in a comment
at the top of your program(s) and make sure that the code associated
with each problem is denoted by comments in the code.
- Now it's time for some programming. From now on, you will have
to write your programs from scratch. The easiest way to do this is to
use an existing program as a template. Here are some quick
instructions to get you started:
- Create a folder "LEGO" in your middfiles account if you
haven't done so already.
- Open one of the previous programs in a text editor, for
example your file "lab2.txt", or one of the programs in
from the code folder.
- Use "Save As" to save a copy of this file in your
middfiles LEGO folder. You can use the name "lab3.txt".
- Replace the old program text with your new code. Don't
forget to update the menu options as well.
First, write two procedures "left-brightness" and "right-brightness"
that return the values of the two brightness sensors. These will
look very similar to the procedures "left-sensor" and "right-sensor"
from the Sciborg code. (Such procedures are useful because they make
the code easier to read, but also easier to maintain: if you ever
need to plug the sensors into different ports, you'll only have to
change one line!)
Next, write a procedure "print-brightness" that
prints the current values of the two sensors to the top line of the
LCD display like this:
L: 56 R: 72
(assuming the sensors are currently returning the values 56 and 72).
Note also that your actual display will look slighly different than this since the
Handy Boards always print lowercase and the spacing may be a bit different.
Download your procedure to the Handy Board and test it. Your
procedure should continuously update the values, similar to the Handy
Board's "display analog values" mode. If the values change too fast,
try inserting a "wait 1" statement to slow it down.
- Write three procedures "min", "max", and "avg" that each take two
parameters, ":a" and ":b". The procedures should compute the minimum,
maximum, and average of :a and :b, respectively. Write two test
procedures "print-min-max" and "print-avg" that work like the
"print-brightness" procedure, but in addition show the result of the
min/max and avg procedures in the bottom line of the LCD
display, like this:
L: 122 R: 94
min 94 max 122
L: 65 R: 23
Try and see if you can exercise good code reuse when writing these function. Specifically,
you should avoid having the exact same code copied to print of the L and R values! (Come talk
to me if you don't know what I'm saying here :)
Download your code and make sure your procedures calculate the
minimum, maximum, and average of the two sensor values correctly.
- Write three procedures "follow-light1", "follow-light2", and
"follow-light3" that use the three strategies below to make
the Sciborg follow the flashlight around the room. Write
and test one procedure at a time! As you test them, you may
also need to revisit how you mounted them.
- If the left sensor sees more light (smaller value)
than the right sensor, go left;
- otherwise, go right.
Based on your experiments from
problems 1 and 2, choose a threshold to distinguish
between light and dark. Then:
(This strategy can be implemented with only two
ifelse-statements, one for each motor - can you see how?)
- If both sensors see light, go straight
- If only the left sensor sees light, go left
- If only the right sensor sees light, go right
- If neither sensor sees light, stop.
First, compute the difference between the two sensor
values (left - right) and store it in a global variable "diff. Then:
Of course, you'll have to find proper values for "small"
- If the difference is small (positive or negative),
go straight. (Note: to compute the negative value of
a number, subtract it from 0).
- Otherwise, if the difference is positive, but not
too big, turn slightly to the right (left motor at
full speed, right motor at half speed).
- Similarly, if the difference is negative, but (its
absolute value) is not too big, turn slightly to the
- If the difference is positive and big, turn right
- If the difference is negative and (its absolute
value) is big, turn left
Which strategy works the best?
Hand in a printout of your program
as part of your homework 3 submission on Monday in class. Then, move
on to the homework assignment: the eight-ball contest!