c_room/task01.c or py_room/task01.py) and include the library.What you need to know about the garage: when the simulation starts, the door is fully closed (door_pos_read() = 100; 0 means fully open). The door motor does NOT stop by itself at the ends; your program must stop it. A full opening or closing takes about 7 seconds (with the default Animation Speed). The keypad on the wall has the keys 0–9, # and X; a key reads 1 only while you hold it down with the mouse and 0 after you release it — presses are not stored anywhere. The warning light is the coloured strip just above the keypad. The Light On / Light Off and Door Up / Door Stop / Door Down buttons of the manual control panel write to the same addresses as your program; you can use them to close the door again after a task.
Start the garage simulation. Write a program that connects to the simulation, prints Connected to the garage! if the connection succeeded and then closes the connection.
Then close the simulation, run the program once more and observe what happens.
sim_connect() returns 0 on success. Close the connection with sim_disconnect().Write a program that turns on the garage's interior lamp. Observe that the lamp stays on after the program has ended.
Press the Light Off button on the manual control panel to turn it off.
garage_lamp() function with the LAMP_ON constant.Turn the lamp on, wait 3 seconds, then turn it off. The lamp must be off when the program ends.
Sleep(3000) waits 3000 milliseconds; in Python time.sleep(3) waits 3 seconds (add import time first).Light the warning light red (255, 0, 0) for 2 seconds, then yellow (255, 255, 0) for 2 seconds, then green (0, 255, 0) for 2 seconds, and finally turn it off. The warning light is the coloured strip just above the keypad.
warning_light(r, g, b, WARN_ON) sets the colour and turns the light on. Yellow is a mix of red and green.Start with the door closed. Run the door motor upwards, wait 2 seconds and stop the motor. The door should be open about one third.
To close it again, press Door Down on the manual control panel, then Door Stop when the door is closed.
door_motor(DOOR_UP) starts the motor; it keeps running until you call door_motor(DOOR_STOP).With the door closed, make one full door cycle:
1. Light the warning light orange (255, 165, 0) and run the door up for 7 seconds (the door opens fully).
2. Stop the motor, turn the warning light off and wait 3 seconds.
3. Light the warning light red (255, 0, 0) and run the door down for 7 seconds.
4. Stop the motor and turn the warning light off.
Read the closed percentage of the door and print it like Door closed: 100%.
Then press Door Up on the manual control panel, press Door Stop when the door is half way and run the program again. This time you should see a different number.
door_pos_read() returns a whole number from 0 to 100: 100 is fully closed, 0 is fully open. Store the value in a variable.Read the door position. If the door is fully closed (100), print Door is closed and light the warning light green; otherwise print Door is NOT closed and light it red.
Run the program once with the door closed and once after opening the door a little by hand.
if (...) { ... } else { ... } structure. Equality is checked with ==.Extend Task 8 to three states:
• 100: print Closed, warning light green (0, 255, 0).
• 0: print Fully open, warning light red (255, 0, 0).
• Anything in between: print the value like Partly open: 40%, warning light yellow (255, 255, 0).
else if in C and elif in Python.Read and print the current state of the lamp, the warning light and the door motor, for example:
Lamp: ON
Warning light: OFF
Motor: UP
For the motor print 0 → STOPPED, 1 → UP, 2 → DOWN. Run the program; then press Light On and Door Up on the manual control panel and run it again. Do not forget to press Door Stop at the end.
garage_lamp_read(), warning_light_read() and door_motor_read() read the last state of the outputs. Compare the motor value with the DOOR_STOP, DOOR_UP, DOOR_DOWN constants.The program prints Press and hold key 5 (3 s)... and waits 3 seconds. During this time hold key 5 of the keypad in the simulation down with the mouse.
When the time is up, read the key: if it is pressed, turn the lamp on and print Key 5 is pressed; otherwise turn the lamp off and print Key 5 is not pressed. Try it once with and once without pressing.
key_read(5) returns 1 if the key is pressed at that moment, otherwise 0. The keypad does not store presses: you must hold the key until the program reads it.The program prints Press and hold key 1 (3 s)... and waits 3 seconds. Open the door only if both conditions are true: the door is fully closed (100) AND key 1 is pressed.
In that case print Opening, run the door up for 3 seconds and stop it. Otherwise print Cannot open, light the warning light red for 2 seconds and turn it off. Try it with the door closed and with the door a little open.
&&, in Python and combines two conditions.Calculate the colour of the warning light from the door position:
red = (100 − position) × 255 / 100
green = position × 255 / 100
blue = 0
So a closed door is green, a fully open door red and a half open door something in between. Print the position and the values like Door closed: 50% R=127 G=127. Move the door to different positions by hand and run the program a few times.
//.Put Task 13 into an endless loop: every 300 ms read and print the position and update the colour of the warning light.
While the program runs, open and close the door with the manual control panel (press Door Stop at the end) and watch the colour change step by step. Stop the program with Ctrl+C.
while (1) { ... } in C and while True: in Python repeat forever. Put a short wait at the end of the loop.Do Task 6 with the sensor instead of timing:
1. Light the warning light orange (255, 165, 0), run the motor up and wait, checking every 50 ms, until door_pos_read() is 0. Then stop the motor, turn the light off and print Door is fully open.
2. Wait 1 second.
3. In the same way close the door until the position is 100 and print Door is fully closed.
Try it with the door half open too: it must still stop at the right place.
while (condition) repeats as long as the condition is true. While opening, the condition can be door_pos_read() > 0.Write a guard program that runs all the time: if the motor is running up and the door is fully open (0), or the motor is running down and the door is fully closed (100), it stops the motor and prints Limit reached, motor stopped. The loop runs every 50 ms.
Leave the program running and move the door only with the Door Up / Door Down buttons of the manual control panel: the door must now stop by itself at each end.
door_motor_read() also reads commands given by hand. Join the two cases with || in C or or in Python.Use the keypad as a remote control: 1 → door up, 2 → door down, 0 → stop. When the door reaches an end the motor stops by itself (Task 16). While the motor runs the warning light is orange (255, 165, 0); when it stops the light goes off. The loop runs every 50 ms.
Try it: press 1, press 0 when the door is half way, then press 2.
ifs, then do the end check from Task 16. Decide the light by looking at door_motor_read() last.Scan the 12 keys of the keypad (0–11) with a for loop and print the key that is pressed: Key 7 for digits, Key # for 10, Key X for 11. The outer loop runs every 200 ms.
Notice that the text repeats while you hold a key; you will fix that in the next task.
for (int k = 0; k <= 11; k++) / for k in range(12): counts k from 0 to 11. The constants ADR_KEY_HASH (10) and ADR_KEY_X (11) are ready to use.Count how many times the # key is pressed. Each press must count only once, however long you hold the key; every time the number grows print it like # pressed 3 times.
The X key resets the counter and prints Counter reset (if the counter is already 0 it does nothing). The loop runs every 20 ms.
Wait until a digit key (1–9) is pressed and continue when it is released. Then print something like Blinking 4 times and blink the garage lamp that many times (0.5 s on, 0.5 s off). Then wait for the next digit. The keys 0, # and X are ignored.
for for the blinking.Read 4 digits from the keypad (this time 0 is valid too; # and X are ignored). Each time a digit is pressed and released, light the warning light blue (0, 0, 255) for 150 ms (like a key beep) and print a *.
When 4 digits are in, join them into one number (1, 2, 3, 4 → 1234) and print Code: 1234. If the code is 1234 light the warning light green for 3 seconds, otherwise red for 3 seconds; then the program ends.
code = code * 10 + digit. Because 0 is valid, use -1 to mean 'no key'.Split Task 21 into functions and build a code lock that runs all the time:
• wait_key(): waits until any key (0–11) is pressed and released, shows a 150 ms blue key light and returns the key number.
• open_door() and close_door(): open / close the door fully using the sensor; the warning light is orange while moving (Task 15).
The main loop reads 4 digits (# and X are ignored) and prints a * for each. If the code is 1234 it prints Access granted, turns the lamp on, opens the door, waits 5 seconds, closes the door and turns the lamp off. Otherwise it prints Wrong code and lights the warning light red for 2 seconds.
main. wait_key() returns a value, so it is an int function (return in Python); the other two are void.The code no longer has a fixed length. The user types digits and confirms with #:
• Each digit is stored in an array and a * is printed; at most 8 digits are stored, more are ignored.
• X clears what was typed so far, prints Cleared and lights the warning light yellow (255, 255, 0) for 300 ms.
• The correct code is 2580 and is stored in an array ({2, 5, 8, 0}). When # is pressed compare the typed digits with the code: if the length and all digits match, open and close the door as in Task 22 (lamp included); otherwise print Wrong code and light red for 2 seconds.
The entry is reset after every attempt. Use the functions from Task 22.
len variable next to the array that says how many places are used, and compare in a function with a for loop. In Python use a list; two lists can be compared with ==.Add security to Task 23: count the wrong attempts and print them like Wrong code (2/3). After 3 wrong attempts in a row the system locks for 15 seconds:
• Print LOCKED for 15 s.
• During the lockout the warning light blinks red (250 ms on, 250 ms off) and keys are ignored.
• When the lockout ends print Unlocked and reset the counter.
The counter is also reset when the correct code is entered. After the 1st and 2nd wrong attempt the light is red for 2 seconds as before.
lockout() function. 15 seconds is 30 blinks of 500 ms. Keys are ignored automatically because wait_key() is not called during the lockout.Real garage doors stop and open again if there is an obstacle while closing. The simulation has no obstacle sensor, so use the X key as an 'obstacle detected' button.
Write a function close_door_safe(): while closing, every 50 ms it checks the X key together with the position. If X is pressed it stops the motor at once, prints Obstacle! Reopening, lights the warning light red for 1 second, opens the door fully and returns 0. If the door closes without an obstacle it returns 1.
Main program: when # is pressed it opens the door, waits 5 seconds and closes it with close_door_safe(); if the door was reopened because of an obstacle it waits 5 seconds and tries again. When the door is closed it prints Door closed.
X with an if; when found leave the function at once with return 0. In the main program while (close_door_safe() == 0) repeats the attempt.Add a timeout to the code entry of Task 23: if the user typed at least one digit and then presses no key for 5 seconds, the entry is cleared, Timeout, entry cleared is printed and the warning light is yellow for 1 second.
For this write a function wait_key_timeout(ms): if a key is pressed and released within the given time (with the blue key light) it returns the key number, otherwise -1. Do not move the door in this task: for the correct code print Access granted and light green for 3 seconds, for a wrong one print Wrong code and light red for 2 seconds.
GetTickCount() gives the milliseconds since the computer started (DWORD), in Python time.time() gives seconds. Store the start time and look at the difference.Improve the keypad remote control of Task 17: while the door moves the warning light blinks orange (255, 165, 0) (300 ms on, 300 ms off), but the keys still react immediately.
So do NOT blink with Sleep(300) / time.sleep(0.3): the loop runs every 20 ms; in every round read the clock and, if 300 ms have passed since the last change, flip the light. When the motor stops the light stays off. Press 0 while the door is half way and check that it stops at once.
light_on) and the time of the last change (last_change).Control the door with a state machine. There are four states: CLOSED, OPENING, OPEN, CLOSING.
• CLOSED: motor stopped, lamp and warning light off. When # is pressed → OPENING.
• OPENING: lamp on, warning light orange, motor up. When the position is 0 → OPEN.
• OPEN: motor stopped, warning light green. After 10 seconds or when # is pressed → CLOSING.
• CLOSING: warning light orange, motor down. When the position is 100 → CLOSED; if X is pressed (obstacle) print Obstacle! and → OPENING.
On every state change print the new state like -> OPEN. If the door is not closed when the program starts, begin with CLOSING. The loop runs every 20 ms with no long Sleep anywhere; keys are read with edge detection (Task 19).
enum in C). An enter(state) function sets the motor and lights once when entering a new state; the main loop only checks the transition conditions. A new_key() function returns a key number only at the moment it is pressed, otherwise -1.Combine the state machine of Task 28 with the code entry. States: CLOSED, OPENING, OPEN, CLOSING, LOCKED.
• CLOSED: the code is typed on the keypad (Tasks 23–26). Digit → * and 100 ms blue light (at most 8 digits); X → clear the entry, 300 ms yellow; # → check. The code is 2580. If correct, Access granted → OPENING. If wrong, Wrong code (n/3) and 1 s red; on the 3rd wrong one → LOCKED. If no key is pressed for 5 s while the entry is not empty: Timeout, entry cleared and 1 s yellow.
• LOCKED: for 15 s the warning light blinks red (250 ms), keys are ignored; then Unlocked → CLOSED.
• OPENING: lamp on, motor up, warning light blinks orange (300 ms, without stopping the program — Task 27). Position 0 → OPEN.
• OPEN: motor stopped, steady green light. After 10 s or with # → CLOSING.
• CLOSING: motor down, orange blinking. X → Obstacle! Reopening → OPENING. Position 100 → CLOSED.
In CLOSED and LOCKED the motor stops, lamp and warning light are off and the entry is cleared. Print every state change like -> OPEN; if the door is not closed at the start, begin with CLOSING.
state_time variable in enter() and use it for both LOCKED (15 s) and OPEN (10 s). Move the code handling into a function handle_code(key). Do the blinking in a function blink(r, g, b, period) as in Task 27.Move the door to a position chosen on the keypad. The user types a closed percentage from 0 to 100 and confirms with # (e.g. 5, 0, # → 50%). The typed digits are shown on the screen; at most 3 digits are taken. X clears the typed number (Cleared).
• If no digit was typed or the value is above 100, print Invalid and light the warning light red for 1 second.
• If the door is already at that position, print Already there.
• Otherwise the program chooses the direction itself: if the target is smaller than the current value the door goes up, if it is bigger the door goes down; it stops at the target. While moving the warning light is orange and every time the position changes it prints something like 45% -> 50%. At the target it prints Arrived and lights green for 1 second.
• If X is pressed while moving, the door stops at once and Stopped is printed.
go_to(target) function. Careful: a smaller value means a MORE open door. The stop condition is pos <= target going up and pos >= target going down. To print the position only when it changes, store the last printed value.The code can be changed without changing the program. The user code is 1234 at the start, the admin code is a fixed 9999. Every entry is confirmed with #, X clears it (Cleared); a * is printed for each digit and at most 8 digits are taken.
• User code: Access granted, open the door, wait 5 s, close it (functions from Task 22).
• Admin code: print Admin mode; the warning light stays yellow (255, 255, 0). Print Enter new code: , the user types the new code and presses #; then print Repeat new code: and the same code is typed again. If both entries are the same and have at least 4 digits the code changes: print Code changed and blink the warning light green twice (300 ms on, 300 ms off). Otherwise print Code NOT changed and light red for 2 s.
• Any other entry: Wrong code and 2 s red.
Note: do not use the blue key light in this task, so the steady yellow light is not disturbed.
read_entry() function that reads one entry up to # and use it in three places. In C you need a function same(a, a_len, b, b_len) that compares two arrays and a for loop to copy the new code.Three people use the garage and each has a 4-digit code: Ali 1111, Ayse 2222, Can 3333. Store the names in an array and the codes in a two-dimensional array (a list of lists). Entries are read as in Task 31 (*, X clears, # confirms).
• If a user's code is entered, print Welcome, Ayse, add 1 to that person's opening count, turn the lamp on, open the door, after 5 s close it with close_door_safe() from Task 25 (if it reopened because of an obstacle, try again after 5 s) and turn the lamp off.
• If an unknown code is entered, print Unknown code, add 1 to the wrong-attempt counter and light red for 2 s.
• If only 0 is entered and # pressed, print a report:
Ali: 2 openings
Ayse: 0 openings
Can: 1 openings
Wrong attempts: 1
Total openings: 3
find_user() function compare the entry with every user's code and return the user's index (0–2), or -1 if not found. Use a counter array with one element per user for the openings.