Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
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.inofile 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 elseallows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped.