The Car Park Counter That Went Negative
Camford Car Park has 50 spaces, one entrance barrier and one exit barrier. A sensor sends the text "in" or "out" once per vehicle. A student writes this control program:
spaces = 50
count = 0
loop
read sensor
if sensor = "in" then
count = count + 1
else if sensor = "out" then
count = count - 1
end if
if count > spaces then
display "FULL"
barrier = closed
else
display "SPACES: " + (spaces - count)
barrier = open
end if
end loop
On the first morning the display read "SPACES: 53" when the car park was almost empty, and the entrance barrier was still open after the last space had been taken, so the attendant had to wave cars back out. The exit sensor is mounted where a driver reversing away from the entrance barrier can trigger it. The car park also reserves two car spaces for six motorcycles, which the vehicle counter cannot distinguish from cars.
1. Dry run the program for the sensor sequence in, out, out, out, out. Show count and the displayed message after each event, and use the result to explain the "SPACES: 53" reading.
Read the worked answer
Starting values: spaces = 50, count = 0, display "SPACES: 50".
| Event | count after | Display |
|---|---|---|
| in | 1 | SPACES: 49 |
| out | 0 | SPACES: 50 |
| out | -1 | SPACES: 51 |
| out | -2 | SPACES: 52 |
| out | -3 | SPACES: 53 |
The program has no guard on the "out" branch, so a spurious "out" makes the count negative. The display shows spaces - count, so a negative count is displayed as more free spaces than exist. Four unmatched "out" events are enough to produce 53.
Those events are realistic here: the exit sensor can be triggered by a vehicle reversing away from the entrance barrier, and by motorcycles leaving the reserved bays. The program trusts every sensor event completely, so one wiring or layout fault corrupts the count for the rest of the day.
2. Rewrite the loop so that (a) the barrier closes before the 51st car can enter and (b) the count can never fall below zero. Explain why the order of the checks matters.
Read the worked answer
loop
read sensor
if sensor = "in" then
if count >= spaces then
display "FULL"
barrier = closed
else
count = count + 1
display "SPACES: " + (spaces - count)
barrier = closed
end if
else if sensor = "out" then
if count > 0 then
count = count - 1
end if
display "SPACES: " + (spaces - count)
barrier = open
end if
end loop
Order matters because the original increments first and tests afterwards. On the 51st "in", count becomes 51, the test is then true, and the barrier closes - but the car has already crossed the open barrier. The occupancy check must happen before the increment and before the barrier is opened for that vehicle, so a full car park never admits the extra car.
The guard count > 0 stops spurious "out" events driving the count negative. This masks the symptom but does not remove the cause: a spurious out followed by a real in will still leave the count one too low, so the sensor and layout fixes are also needed.
3. The counter still miscounts because of sensor bounce and reversing vehicles. Suggest one software measure and one hardware or layout measure, and state a cost of each.
Read the worked answer
Software: debounce and direction logic. Record the time of the last accepted event and ignore a second "in" from the same sensor within, say, two seconds; alternatively require the entry beam to be broken and then released in the correct order before counting a vehicle. Cost: no hardware spending, but legitimate fast-following vehicles may be rejected, so the time window must be tuned, and the program becomes more complex to test.
Hardware/layout: inductive loop or paired direction sensors. Fit an inductive loop in the entrance lane that only detects a vehicle moving forward, or two sensors spaced apart whose trigger order determines direction. Move the exit sensor so it cannot see vehicles reversing away from the entrance. Cost: purchase and installation, lane closure during fitting, and maintenance.
Neither fixes the motorcycle problem: a vehicle counter cannot know that six motorcycles occupy two car spaces. That needs per-bay occupancy sensors or a separate motorcycle area with its own counter, which is a larger change and should be justified against the revenue lost from misreported spaces.