CS 1020 - Lab 3 - More programming: parameters, return values, printing

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!)
  1. 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 flashlight. 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 one.

  2. 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:

    1. Create a folder "CS1020" in your tigercat account if you haven't done so already.
    2. Open one of the previous programs in Metapad, for example your file "lab2.txt", or one of the programs in from the "Day1" folder.
    3. Use "Save As" to save a copy of this file in your tigercat "CS1020" folder. You can use the name "lab3.txt".
    4. 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). 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.

  3. 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
        average 44	
    Download your code and make sure your procedures calculate the minimum, maximum, and average of the two sensor values correctly.

  4. 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!

    Which strategy works the best?

Once you are done, add your names and your group number in a comment at the top of your program(s), and hand in a printout of your program as part of your homework 3 submission on Friday in class. Then, move on to the homework assignment: design a cup collector!