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!)

Brightness Sensors

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.

More Coding!

  1. 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 "LEGO" in your middfiles account if you haven't done so already.
    2. 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.
    3. Use "Save As" to save a copy of this file in your middfiles LEGO 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). 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.

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

  3. 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.

    Which strategy works the best?

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.

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!