5.1 If else¶
Usually we use conditional judgment to complete the most basic reality interaction projects. Here, we build a door detection system with reed switch and LED to show this logic.
Fix the magnet on one side of the door and the reed switch (with circuit) on the other side of the door. When the door is closed, the magnet is close to the reed switch, which will turn it on.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
3 in 1 Starter Kit |
380+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
Schematic

Wiring

Code
Note
Open the
5.1.if_else.ino
file under the path of3in1-kit\basic_project\5.1.if_else
.Or copy this code into Arduino IDE.
Or upload the code through the Arduino Web Editor.
After the code is uploaded successfully, if you do not close the door, the LED will light up, prompting you to close the door.
By the way, if we need the opposite effect (lighting up the LED when the door is closed), we just need to modify the condition in the if
.
-
The
if else
allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped.