In this lesson, an intelligent garage system will be simulated. In front of the garage, open the garage door through the Switch on the IoT Control page. When the car goes out to the set distance, the garage door automatically closes.
Enter the Simulation page, then connect the SIG of servo to P0 of the Ezblock Pi, the VCC to 3.3V and the GND to GND. Ultrasonic Trig pins are connected to D1; Echo to D0, VCC and GND to 3.3v and GND respectively.
Return to the Programming page, and click the IoT Control icon to enter the IoT Control page.
Click the ‘+’ button to add function blocks.
Create a Ultrasonic sensor and a Switch actuator.
Go back to the Programming page. Under the Raspberry Pi category, get a Connect WiFi block and put it into Start block.
Click Variables to create a variable called distance.
The value of the ultrasonic wave is read and stored in the variable, distance.
Select the if do block and the Comparison Operator ( = ) block in the Logic category to set a judgement condition.
If get Switch-1 value is equal to 1, the Switch state is ON.
When the Switch state is ON, turn the servo to 90 degrees to simulate opening the garage door.
Set a judging condition again. When the distance is greater than 50cm, set the servo to 0 degrees to close the garage door.
Now save your project, connect the Bluetooth and click Flash to compile and download the code.
Re-enter the IoT Control page and click the Run button. When you click the Switch, the garage door will open. When the car is 50cm away from the door, the garage door will close automatically.