Pseudocoding In My Hammock — Elevators On The Mind

What do pseudocoding, hammocks, elevators and my brain all have in common?

Well, not much. However, this describes my afternoon pretty well. Today’s challenge from the Basic Prep course was to write a program in pseudo-code for operating an elevator.

I can’t complain too much, because I started the process by sketching some ideas on paper while sitting in my hammock. The afternoon sun was coming across the porch with a glimmer. It was delightful.

I struggled at first, but in the end came up with a solution which looks pretty good to me. I am glad I stuck it through the initial challenges.

Here is the challenge: An elevator has 25 floors and needs to be programmed. It has buttons for the floors in the elevator, and also call buttons at each floor. It sometimes gets stuck on a half-floor, but has a sensor which can recognize this. It moves fast, so it needs to slow down one full floor before it stops. Don’t smash anyone. Don’t kill anyone. Don’t let anyone off on a half-floor. Go!

I started with a rough draft, and then filled in detail. If I were really being a perfectionist, I can tell that there are still more details that could be added. I can also sense a few spots that could potentially lead to bugs. But, I’m happy with how this turned out for now, and just being able to recognize potential buggy spots could be useful.

Anyone should be able to read this, because it’s written in human language, so without further ado…

PROGRAM Elevator
WHILE Operating DO
IF Button pushed
RUN Program ButtonResponse
END
IF upqueue contains values
RUN Program Upqueue
END
IF downqueue contains values
RUN Program Downqueue
END
If queue is empty
UNLESS floorlocation=1
THEN Add "1" to downqueue
ELSE Wait
ENDWHILE
END
PROGRAM ButtonResponse
IF button pushed value > floorlocation
THEN Add callvalue to upqueue
LIGHT button pushed
END
IF button pushed value < floorlocation
THEN add callvalue to downqueue
LIGHT button pushed
END
IF button pushed value = floorlocation
IF Doors Closed
IF Elevator Stopped
RUN PROGRAM OpenDoors
END
END
END
IF "floorreached" = "A"
UNLIGHT floor button "A"
UNLIGHT up button "A"
UNLIGHT down button "A"
END
END
PROGRAM Upqueue
WHILE upqueue contains values DO
tripvalue = "up trip"
IF Doors Open
THEN wait 15 seconds
THEN RUN Program CloseDoors
END
IF Doors Closed
UNLESS floorlocation = 25
WHILE Floorlocation != next in upqueue DO
WHILE next in upqueue > floorlocation + 1
DO Move UP FULL
ENDWHILE
WHILE next in upqueue = floorlocation + 1
DO Move UP SLOW
ENDWHILE
ENDWHILE
IF Floorlocation = next in upqueue
THEN Stop Elevator
RUN Program OpenDoors
REMOVE current callvalue from upqueue
OUTPUT "floorreached"="A"
END
END
ENDWHILE
END
PROGRAM Downqueue
WHILE downqueue contains values DO
trip value = "down trip"
IF Doors Open
THEN wait 15 seconds
THEN RUN Program CloseDoors
IF Doors Closed
UNLESS floorlocation = 1
WHILE Floorlocation != next in downqueue DO
WHILE next in downqueue < floorlocation - 1
DO Move DOWN FULL
ENDWHILE
WHILE next in downqueue < floorlocation - 1
DO Move DOWN SLOW
ENDWHILE
ENDWHILE
IF Floorlocation = next in downqueue
THEN Stop Elevator
RUN Program OpenDoors
REMOVE current callvalue from downqueue
OUTPUT "floorreached"="A"
END
END
ENDWHILE
END
PROGRAM CloseDoors
WHILE Doors Open
WHILE Doorsensor = "unblocked"
DO Close Doors
ENDWHILE
IF Doorsensor = "blocked"
Open Doors
Wait 15 seconds
WHILE Doorsensor = "blocked"
Beep Loudly
ENDWHILE
END
ENDWHILE
END
PROGRAM OpenDoors
WHILE Doors Closed
WHILE floorlocation = halffloor DO
If Elevator trip value = "up trip"
Adjust Elevator up
ELSE Adjust Elevator down
ENDWHILE
Open Doors
ENDWHILE
END