c_room/task01.c or py_room/task01.py) and include the library.What you need to know about the room simulation:
• With the manual control panel at the bottom of the simulation window (shown/hidden with Manual Control in the menu) you can change sensors and outputs by hand: the Change Room Temperature and Change Daylight sliders; the Light On / Light Off, AC On / AC Off, Curtain Up / Curtain Stop / Curtain Down buttons.
• The temperature never stands still: with the AC off it rises 1 °C every 0.1 s, with the AC on it drops at the same speed. When the AC is switched, the room waits about 1 second before changing direction. On its own the temperature stays between 13 and 44 °C.
• The outdoor light slowly goes back and forth between 20 and 1000 lux on its own (1 lux every 0.1 s). If you do not want to wait, set any value with the slider.
• The curtain starts fully down (closed). Opening it fully takes about 4.5 seconds. When the curtain reaches the top or the bottom, the motor stops by itself and read_curtain() returns 0 (CURTAIN_STOP). The curtain position cannot be read, only what the motor is doing.
• When your program ends, the lamp, the AC and the curtain keep the last command. If things change too fast, slow the simulation down with Animation Speed in the menu; Restart Sim resets everything.
Write a program that connects to the simulation, prints Hello room! and closes the connection.
Start the simulation first, then run the program. In the console you should also see the library messages [SIM] Connected and [SIM] Disconnected.
Then close the simulation and run the program again: it cannot connect, so it must end with an error message.
sim_connect() returns 0 on success. If it is not 0, end the program right away.Write a program that turns the room lamp on. The program ends right after that.
Does the lamp stay on after the program has ended? Observe it. Then turn the lamp off with the Light Off button in the manual control panel.
lamp(LAMP_ON).Turn the lamp on, wait 2 seconds and turn it off. The lamp should be visible for 2 seconds and stay off when the program ends.
Sleep() takes milliseconds (1 second = 1000), in Python time.sleep() takes seconds.Write a program that follows this order:
1. Turn the lamp on.
2. After 3 seconds turn the AC on (the AC wind appears in the room).
3. After 5 more seconds turn both the lamp and the AC off.
Watch the temperature display while the AC is on: it starts dropping after about 1 second.
Start with the curtain closed (at the bottom). Run the curtain up, wait 6 seconds, then run it down and end the program.
The curtain must stop by itself at the top; after the program ends it must keep moving down and stop at the bottom.
curtain(CURTAIN_UP) and curtain(CURTAIN_DOWN) run the motor. Opening fully takes about 4.5 seconds.Start with the curtain closed. Run the curtain up for 2 seconds and stop it. The curtain should stay at about half of the window.
Then try 1 second and 3 seconds and compare (close the curtain with the Curtain Down button before each try).
CURTAIN_STOP to stop the motor.Start with the curtain closed. Write a morning scenario that prints what it does before each step:
1. Print Good morning!, turn the lamp on, wait 2 seconds.
2. Print Opening the curtain, run the curtain up, wait 5 seconds.
3. Print Fresh air, turn the lamp off, turn the AC on, wait 3 seconds.
4. Turn the AC off and print Ready for the day.
Read the room temperature into a variable and print it as Temperature: 24 C.
Run the program a few times and compare the value with the temperature display in the simulation. Then press AC On and run it again.
int t = read_temp(); and printf("%d", t); in Python t = read_temp() and an f-string.Write a report program that reads all values of the room and prints them one per line:
Temperature: .., Lux: .., AC: .., Lamp: .., Curtain motor: ..
In the manual control panel turn the lamp on, set the Change Daylight slider to 500, run the curtain up and start the program while the curtain is moving. The numbers in the report must show your changes.
read_temp(), read_lux(), read_ac(), read_lamp(), read_curtain().Read the outdoor light. If it is less than 100 lux, turn the lamp on and print Dark (.. lux): lamp on; otherwise turn the lamp off and print Bright (.. lux): lamp off.
Test: set the Change Daylight slider to 50 and run the program, then set it to 800 and run it again.
if (condition) { ... } else { ... } structure. The condition is lux < 100.Read the temperature. If it is higher than 26 °C, turn the AC on and print Hot (.. C): AC on; otherwise turn the AC off and print Not hot (.. C): AC off.
Test: with the AC off the room heats up within a few seconds; run the program and the AC must turn on. Then wait 5 seconds (the room cools) and run it again: the AC must turn off.
Read the curtain motor state and show it as text instead of a number: 0 → Curtain stopped, 1 → Curtain moving up, 2 → Curtain moving down, any other value → Unknown value.
Test: run it while the curtain is still; then press Curtain Up (or Curtain Down) and run it right away while the curtain is moving.
else if chain (elif in Python). Compare with constants such as CURTAIN_STOP instead of numbers; equality is checked with ==.If the lamp is on AND it is brighter than 600 lux outside, turn the lamp off and print Lamp was on in daylight: turned off. Otherwise send no command and print Nothing to do.
Test: press Light On, set the light slider to 800 and run the program (the lamp must go off). Then set the slider to 300, turn the lamp on and run it again (the lamp must stay on).
&& in C and and in Python. read_lamp() gives the lamp state.Improve Task 11: before sending a command, check the AC state with read_ac().
• If the temperature is higher than 26 °C: if the AC is already on print AC already on, otherwise turn it on and print AC turned on.
• Otherwise: if the AC is already off print AC already off, otherwise turn it off and print AC turned off.
Test: set Animation Speed in the menu to the lowest value. While the room is hot, run the program twice in a row: first you should see AC turned on, then AC already on.
if inside an if block (nested if).Blink the lamp 5 times: each time 1 second on, 1 second off. On each blink print Blink 1, Blink 2 ... Blink 5.
Use a loop instead of copying the code 5 times.
for (int i = 1; i <= 5; i++) { ... } — Python: for i in range(1, 6):Start with the curtain closed. Count down from 10 to 1, printing one number every second. When the count ends, print Go!, turn the lamp on and run the curtain up.
i-- in C, range(10, 0, -1) in Python.Let the room heat up with the AC off, then run the program. The program turns the AC on, takes 20 readings every 200 ms and prints each as 1: 44 C. At the end it turns the AC off and prints the lowest and highest reading: Lowest: .. C, highest: .. C.
First run the curtain down and wait until the motor stops (the curtain is fully closed). Then run the curtain up and increase a counter every 100 ms until the motor stops by itself. At the end print Opened in about .. ms (you should see about 4500 ms).
Do not wait a fixed time; find out that the motor has stopped with read_curtain().
while (read_curtain() != CURTAIN_STOP) { ... } repeats as long as the condition is true.Put Task 10 into a loop that never ends: every 100 ms read the outdoor light; if it is less than 100 lux turn the lamp on, otherwise turn it off.
Run the program and move the light slider between 50 and 500; the lamp must follow by itself. Watch the console as well: what do you see? Stop the program with Ctrl+C.
while (1) in C, while True: in Python. Always put a small wait in the loop.Fix Task 19: send a command to the lamp only when its state has to change. Each time the lamp changes, print one line such as Lux 85: lamp on or Lux 120: lamp off.
Move the slider: the console should now show a line only at the moments of change.
read_lamp() (the idea from Task 14).Write a thermostat that runs all the time: every 100 ms read the temperature; above 26 °C the AC must be on, otherwise off. Like in Task 20, send a command only on change and print 27 C: AC on / 26 C: AC off.
While it runs, watch how often the AC switches (lower the Animation Speed to watch comfortably).
Use the lamp buttons of the manual control panel as an input. The program counts how many times the lamp is switched on: each time the lamp goes from off to on, it prints Press 1, Press 2 ... After the 5th time it turns the AC on, prints 5 presses: AC on and ends.
Test: start the program with the lamp off, then press Light On and Light Off one after the other.
prev. If it was 0 before and is 1 now, the lamp has just been switched on. End the loop with while (presses < 5).Write a function blink(times, ms) that blinks the lamp times times, each time ms milliseconds on and ms milliseconds off.
In the main program call blink(3, 200) (fast), pause 1 second, then call blink(3, 1000) (slow).
time.sleep(ms / 1000).Write a function move_curtain(dir): it runs the curtain in the given direction and waits until the motor stops. If the motor stops within 8 seconds it returns 1. If not, it stops the motor and returns 0 (timeout).
Main program: close the curtain and print Closed, wait 2 seconds, open the curtain; if the result is 1 print Opened, if 0 print Timeout!.
To test the timeout, press Stop Sim in the menu while the curtain is opening: after 8 seconds you should see Timeout!.
while loop from Task 18. return leaves the function and gives back a value.Write the thermostat of Task 21 with two thresholds: turn the AC on when the temperature goes above 28 °C, off when it goes below 20 °C; do nothing in between. Count how many times the AC is switched on and print each change like 29 C: AC on (switch 3).
Compare with Task 21: how often does the AC switch now?
else between 20 and 28: in that zone the AC stays as it is.The thermostat from Task 25 keeps running every 100 ms. Add a warning light to the same loop: while the curtain is moving, the lamp changes state every half second (0.5 s on, 0.5 s off); when the curtain stops, the lamp goes off.
Test: press Curtain Up or Curtain Down. While the lamp blinks, the thermostat must keep controlling the AC.
Do not use Sleep(500); the thermostat would also wait half a second.
ticks that increases on every loop. Since the loop runs every 100 ms, ticks % 5 == 0 is true once every half second.Control the curtain automatically by daylight. The curtain has four states: CLOSED, OPENING, OPEN, CLOSING.
• At the start, close the curtain fully (the function from Task 24) and begin in state CLOSED.
• In CLOSED, if lux goes above 600, run the curtain up → OPENING.
• In OPENING, when the motor stops → OPEN.
• In OPEN, if lux goes below 300, run the curtain down → CLOSING.
• In CLOSING, when the motor stops → CLOSED.
Print the name of the new state at every change. Test: move the light slider between 800 and 100.
enum in C, constants in Python). In the loop, first check which state you are in, then apply that state's rule.If the simulation is not open yet, the program should not give up at once: let it try to connect 10 times, 1 second apart (Attempt 1/10 failed...). If all 10 attempts fail, it prints Could not connect, giving up. and ends with code 1.
After connecting it runs a short self test: it turns the lamp and the AC on, waits 200 ms, checks with read_lamp() and read_ac() that the commands were really written and prints Lamp OK / Lamp FAILED and AC OK / AC FAILED. Then it turns both off and closes the connection.
Test: run the program first, then start the simulation a few seconds later.
connect_retry(tries) that returns 0 (success) or 1 (failure).Until now we interrupted endless programs with Ctrl+C; the lamp, the AC and the curtain stayed as they were and sim_disconnect() never ran. Change the night lamp of Task 20 so that on Ctrl+C it leaves the loop cleanly, turns the lamp and the AC off, stops the curtain, calls sim_disconnect() and prints Bye!.
try: ... except KeyboardInterrupt:. C: register a function for SIGINT with signal.h; it only sets running = 0, and the loop becomes while (running).Write a program that runs the room fully automatically. Each subsystem is in its own function and all of them run in the same 100 ms loop:
• Lamp: on if the outdoor light is below 100 lux, otherwise off.
• AC: on above 28 °C, off below 20 °C (Task 25).
• Curtain: the state machine of Task 27 (open above 600 lux, close below 300 lux).
• Commands are sent only on change and every change is printed.
• Clean exit with Ctrl+C (Task 29).
Test: set the light slider to 50, 400, 800 and back to 50; watch what the lamp and the curtain do at each step. Or just wait: the outdoor light changes by itself anyway.
state = control_curtain(state, lux).Write an energy meter that controls nothing, it only watches. Switch the lamp and the AC with the manual control panel (or with another program running at the same time); the meter measures:
• how many seconds in total the lamp and the AC were on,
• how many times the AC was switched on.
Every 5 seconds it prints a line such as Lamp 12.3 s, AC 4.0 s, AC started 2 times. On Ctrl+C it calculates the energy used: lamp 60 W, AC 1500 W. Energy (Wh) = power (W) × time (s) / 3600. Example: Energy: lamp 0.21 Wh, AC 1.67 Wh.
lamp_ticks. Seconds = ticks / 10. For the switch count use the rising edge idea from Task 22. For a decimal result divide by 10.0 in C and use %.1f.Automatic systems should listen to the user. The lamp (on below 100 lux) and the AC (28/20 °C hysteresis) run automatically. But if the user changes the lamp or the AC with the manual control panel, the program must not touch that device for 30 seconds and prints Lamp: manual for 30 s (or AC: ...). When the 30 seconds are over, it prints Lamp: back to automatic and returns to automatic mode. Clean exit with Ctrl+C.
Test: set the light slider to 50 (the lamp turns on), then press Light Off: the lamp must stay off for 30 seconds, then turn on again.
lamp_sent). If read_lamp() is different, someone else, the user, has changed it. 30 seconds = 300 ticks.Write a state machine that runs one day of the room. States: NIGHT, DAY, EVENING.
• Start: if lux is 300 or more, open the curtain fully and start in DAY; otherwise close it fully and start in NIGHT.
• NIGHT → when lux goes above 300, the morning routine: print Good morning!, blink the lamp 3 times quickly (300 ms), open the curtain step by step (1 s up, wait 2 s, 1 s up, wait 2 s, then all the way), go to DAY.
• DAY → when lux goes below 100: print Good evening!, run the curtain down, turn the lamp on, go to EVENING.
• EVENING → after 10 seconds turn the lamp off, print Good night!, go to NIGHT. The loop must not block while waiting these 10 seconds (tick counter).
• Clean exit with Ctrl+C.
Test: speed up the day with the light slider (50 → 500 → 50), or wait about 3 minutes and watch the daylight change by itself.
blink from Task 23 and move_curtain from Task 24. The morning routine is short, so it may block; for the 10 seconds in the evening, set a counter to 100 when entering EVENING and decrease it on every turn.