Lesson 6 Smart Window: Temp-based Control System
Challenges: 6-1, 6-2
Students learn how to control the roof windows based on temperature.
I can read the temperature data and show them on the display.
I can clearly name a variable that represents the threshold value.
I can change the variable’s value and perform operations on its values.
Engage
2 min.
Everyone has a different idea of a comfortable temperature. Some people find 26°C perfect, while others think 25°C is already too warm.

How can we create a system that allows us to flexibly adjust this “comfortable” temperature threshold?
Background
13 min.
DHT11 Temperature & Humidity Sensor
This sensor allows our device to sense the temperature and humidity of its environment, just like a thermometer.

The temperature read block reads the current temperature from the sensor connected to pin P0.

Creating a variable

The set variable block is to assign a value to a variable. Here, we create a variable named my variable and set its starting value to 0.

The change variable block retrieves the value stored in the variable, increments it by 1, and stores the updated value back into the variable.

Build and Connect
![]() | No new build is required. We will use the same setup from Lesson 5 (window mechanism with servo motor) |
Program
25 min.
Challenge 6-1: Reading and Reacting
![]() | Ask students to connect the DHT11 sensor to the P0 of the UNIHIKER K10. Have students prepared for programming and load the program6-1-1. Guide students to predict what will happen and run the program to test their predictions. |
![]() | ![]() Students will need to set the value in the comparator, which is a little bit higher than the room’s temperature condition. Encourage students to modify the if-then-else block. They need to:Add the cache text block and set servo angle blocks inside the correct sections. Here is the sample of program6-1-2: |
Introduce the key vocabulary variables.
In the program above, the number 28 is our threshold. It's hard-coded, meaning it's fixed in the program. What if we want to change it easily? This is where variables become powerful. A variable is a named container that holds a value (like our threshold of 28). We can give it a clear name, like threshold, and change its value in buttons to affect the whole program.
Challenge 6-2: Making the Threshold Adjustable
![]() | Ask students to create a new variable named threshold or another clear name, and set its starting number in the on start function as the starting value only needs to be set once. Prompt students to show the value of the variable on the UNIHIKER K10’s screen. |
![]() | ![]() Discuss the question below:How could we change the threshold setting while the program is running?Encourage students to explore other blocks from the variable category and change the variable’s value using buttons. Part of the code is provided below. |
![]() | ![]() Challenge students to use the variable in the if condition to control the window and texts. Here is the sample of program6-2-2: |
Conclusion:
hard-coded values: only be modified in the code before uploading; unchanging values when the program is running.
Variables: Named, flexible, and reusable; ideal for adjustable values.
Extra Practice
5 min.
Create a variable to record the maximum temperature.
NOTE: every time you read the current temperature in the forever loop, if the value is higher than the variable, update it to the new one, which is the higher one.

Next Class: Lesson 6
Current Tutorial: Click Here


















