CS 1020 - Homework 6 - Ball counter and simple sorter

Due: Friday, 1/18

In the next 5 days of the course you will work on two assignments in a new group. First (still with your old group), disassemble your cup-collecting robot and clean up your lego kit. Then, move to your new group and get to know your new group members with whom you will face the next set of challenges!

The first assignment (due Friday) has two parts. Part 1 is to build a device that can detect black and white ping-pong balls as they roll by on a slanted track. The program should beep when a ball rolls by a reflectance light sensor, using two different notes to distinguish between black and white. It should also keep track of how many balls of each color it has seen so far. Here is an example (which we will also demo in lab):

As you can see, initially there is not much Lego building involved. We'll make it a little more interesting in a little while, and over the weekend it will get quite a bit more challenging, of course. For now, however, the challenge lies in writing the program to detect and count the balls. First, build a slanted track like the one shown above and attach a light sensor. Get hold of a white and a black ping-pong ball (which we will supply), and observe the sensor's readings as you move the balls past it. Determine the ranges of values corresponding to "no ball", "white ball", and "black ball".

The key for determining a ball's color as it rolls by the sensor is to keep track of the minimum value the sensor returns. To do this, declare two global variables, "min" and "current". (Later, you'll also need two other global variables for counting the number of balls of each color.) Try implementing the following strategy:

  1. wait until the sensor value leaves the range "no ball".
  2. initialize "min" to the sensor value at that point
  3. enter a loop in which the following happens:
    1. set "current" to the current sensor value
    2. if "current" is smaller than "min", update "min" to this new minimum
    3. print both "current" and "min" to the display
When you get this far, you should be able to run your program and observe the minimum value as you roll a ball by your sensor once. Hopefully the minima are different enough to distinguish black from white. The remaining problems are:
  1. to quit out of the loop when the ball has passed by; and
  2. to play the right note and increment the right counter.
The second task is fairly straightforward, and we won't give you any further hints :). For the first task, think about how you could detect that the ball has passed by. Note that the value may not go all the way back into the "no ball" range (try rolling by two white balls in close succession); rather you may want to compare "current" with "min" to detect when the value starts rising again. And how do you quit out of a loop in Logo? Well, one way is to use an "output" command (which stops execution of a procedure and returns a value back to the caller). So you'll need to put your loop in a procedure, and then return a value using "output" when you want to exit the loop. A good candidate for the value to return would be the "min". You can also use the "stop" command to exit the procedure without returning a value.

Note: Do not use any of the concurrency commands for this assignment. They are not necessary here since no two things happen in parallel.

Once your ball counter works, move on to part 2 of the assignment: add some Lego machinery that actually sorts the balls into two bins! We'll be more specific for the next homework, but this time any mechanism that manages to sort the balls into two different containers will do. Make sure, however, that it sorts properly even if the balls roll by in quick succession.

When you are done, print out your code (names and group number in a comment!) and hand it in Friday at the beginning of class. We will also go around to see your counter and sorter in action during Friday's lab.