Smart Trash Can with Micro:bit – A Step-by-Step Guide

Smart Trash Can with Micro:bit - A Step-by-Step Guide

Introduction: Continuing from yesterday’s tutorial, we begin today’s lesson.

Programming Section

Step Six

This is the crucial step for the smart trash can. Having crafted a unique body for the trash can, we now implant unique intelligence by burning the control program.

Functionality of the Program

Based on our design, using the distance feedback from the ultrasonic sensor as a test condition, when the sensor is obstructed by a hand or an obstacle, the feedback distance is less than the preset value, triggering the operation to lift the trash can and rotate the servo at a certain angle. When the obstruction is removed, the trash can opens for a while before automatically closing. We can also enhance the Micro:bit’s functionality with some dazzling sound and light effects.

Program Logic

Upon powering on, the trash can is in standby mode. At this time, we let the Micro:bit’s LED screen display a cross and the Robot:bit’s RGB light show a specified color. Here, the cat-loving person prefers blue, so it displays blue in standby mode; you can set your favorite color. The ultrasonic sensor reads the distance feedback, and when the value read is less than the preset ultrasonic distance value, the servo is rotated. When the obstacle is removed, the servo rotates with a delay, allowing the trash can to close with a delay, which is the simple program control logic for the smart trash can.

Implementing the Program

1. Open the Makecode interface and insert the robot:bit plugin. For instructions on how to insert a plugin, please refer to the previous tutorial.

2. Drag the initial program framework. Drag the block indicated by the arrow into the programming area on the right.

Smart Trash Can with Micro:bit - A Step-by-Step Guide

3. Set the initial state upon startup. To reduce false triggers, we preset a flag at program startup. Simply put, this means setting a goal before doing anything; if achieved, it signifies reaching that goal. This is a common logic in programming, which is to set a Flag. This Flag will change as the program executes. For a deeper understanding of program flags, one can study various programming languages, as this is a commonly used logical method in programming.

Smart Trash Can with Micro:bit - A Step-by-Step Guide

Click on variables to set a new variable for distance, referring to the example shown in the figure to preset the initial state upon startup.

The following image shows the required blocks.

Smart Trash Can with Micro:bit - A Step-by-Step Guide

Adjust Brightness Block

Smart Trash Can with Micro:bit - A Step-by-Step Guide

Ultrasonic Settings Block

Smart Trash Can with Micro:bit - A Step-by-Step Guide

Startup State Settings Completed

You can see that upon startup, the LED screen will display a cross. Here we first set a flag and the marker to 0, defining the output value of the ultrasonic pin as the variable distance, and presetting the initial distance value as 30. Due to the initial state, the RGB light might be quite bright, so it is recommended to set the brightness value as shown in the figure. Here we set the RGB light color to blue.

After setting the initial state, we proceed to write the main control program. First, let’s look at the main program

Smart Trash Can with Micro:bit - A Step-by-Step Guide

Program Explanation

1. Use the ultrasonic pin data as the reference value for the distance variable.

2. Using an if-then logic, if the detected distance is less than 100MM or greater than or equal to 5MM, when the instruction or flag is 0 and the detection is in the startup state, set the flag Flag to 1.

3. Since the Flag is 1, we can enter the next sequence. Here, we use the if…else… statement, and if the flag is 1, when the distance is detected to be less than 20MM, the control program starts, the LED displays a check mark, accompanied by a buzzer sound, and the RGB indicator light shows green; at this point, the trash can is about to open, followed by the servo rotating 85 ° for two seconds before closing, accompanied by a prompt sound. This program can be freely combined, depending on your creativity. If the detected values are not within the program’s execution range, the trash can remains closed. As described later in the program, otherwise, the servo angle is 0 ° and the program returns to the original state with the distance preset to 30MM, and the flag is cleared, thus avoiding the program from entering a dead loop.

4. Following the block stacking order shown in the figure, we can complete the gesture control of the trash can. Hurry up and burn the program for testing!

The tutorial ends perfectly, and now it’s time for everyone to freely experience it.

Feel free to click the lower left corner “Read the Original“, the cat has prepared the corresponding program files for you,

Click the original text to obtain it~ Don’t forget to interact with the cat~

Smart Trash Can with Micro:bit - A Step-by-Step Guide

Smart Trash Can with Micro:bit - A Step-by-Step Guide

Note: The KittenBot subscription account is operated by Shenzhen Xiaomiao Technology Co., Ltd. Welcome to subscribe.

All content in this public account is original. Please feel free to share the original link, and media must contact the operator for authorization. Unauthorized plagiarism, modification, or reprinting is not excluded from legal action!

Leave a Comment

×