3.21 Window Close In Dark
3.21.1 Overview
In this project, we program to make the window automatically close when getting dark. So the photoresistor is required to sense the ambient light. We set a threshold for the resistor. When ambient light value is lower than the threshold, the servo closes the window.
3.21.2 Test Code
3.21.2.1 Code Flow

3.21.2.2 Build Code
There are two ways to upload the code: directly open the code file we provide; or manually build blocks.
Directly open the code file we provide:
Click
and choose Load from your computer

We have already downloaded the codes on computer desktop, so open the file and choose
3-21-automaticWindow.sb3
Manually build blocks:
Build the two basic blocks:

We add
to determine the photoresistor value. If the analog value is lower than 500, the servo will rotate to 180 degree. If not, servo will rotate to 0 degree.
Complete Test Code

3.21.2.2 Test Result
After uploading code, cover the photoresistor with something, and the servo will rotate to 180 degree. Remove the cover, and the analog value exceeds 500, so the servo rotates to 0 degree.