There are so many delicious foods—hot pot, Chuanchuan (skewered hot pot), Maocai (spicy simmered vegetables), paper-wrapped fish, spicy and sour pork knuckles, taro chicken, steamed pork with rice flour... The list goes on. There are also countless dishes I’m eager to try, so picking just one among all these treats is really tough. We’re often troubled by the question “What to eat?” Every day, the questions we ask and get asked the most are “What to have for lunch?”, “What to have for dinner?”, and “What to have for a midnight snack?”...

If there is a practical food recommendation robot, whenever we are stuck on deciding what to eat, we just need to press a button gently, and it can quickly make a choice from a wide range of foods. Such a food recommendation robot is truly a boon for people who struggle with decision-making. Next, let's roll up our sleeves and design such a food recommendation robot using the UNIHIKER M10, an RGB light ring, and a button module!
Goal
Press the "Button Module". The RGB light ring will display in the form of a running light, and the position where the light bead finally stops corresponds to the food recommended to us by the food robot. Additionally, the food image corresponding to the light bead will be displayed on the UNIHIKER M10.

Get to Know
1. Learn the definition of functions with parameters
2. Master the usage method and working principle of the Button Module
Material List
Software:
Mind+ Programming Software x1(download at https://mindplus.cc/en)

Hands-on Practice
In this lesson, we will mainly use the button module and RGB light ring to make a food recommendation robot. First, we need to understand the input signal of the button module, then learn how to use the button module to control the RGB light ring, and finally complete the production of the food recommendation robot. Next, let's learn how to make a food recommendation robot through the following three small tasks!
Task 1: Read the value of the button module
Read the input signal of the button module with LED light during operation.
Task 2: Use the button to control the RGB light ring
Press the button to control the RGB light ring to execute the chasing light program.
Task 3: Make a food recommendation robot
Press the button, the RGB light ring executes the chasing light program and generates a random number, so that the light bead corresponding to the random number lights up, and the UNIHIKER M10 displays the food corresponding to the light bead.
Task1:Read the value of the button module
1.Hardware Connection
The digital button module with LED light is a luminous switch-type button. When we press the button, it can emit blue light.
Connect the digital button module with LED light to the P23 pin using a white silicone cable. After the hardware connection is successful, connect the UNIHIKER M10 to the computer using a USB cable.
Note: For relevant introduction to the "digital button module with LED light", please refer to the Knowledge Base.

2.Software Preparation
Open Mind+ and follow the diagram below to complete the software preparation process.

3.Write a Program
The digital button module with LED light is printed with the silk screen "D", indicating that it is a digital input sensor. That is to say, the signals input by this button module are digital signals, namely 0 and 1.

How can we tell when the button module inputs the digital signal 0 and when it inputs 1? To obtain the input signal of the button, use the block for “reading a digital pin” and select Pin P23.

Then use the “text display” block to show the digital values of the button being pressed and released on the UNIHIKER M10.

4.Run the Program
Click "Run". Once the program runs successfully, when you press the button, the button module will light up with a blue light, and the number 1 will be displayed on the UNIHIKER M10; when you release the button, the light on the button module will turn off, and the number 0 will be displayed on the UNIHIKER M10. That is to say, pressing the button inputs the digital signal 1, and releasing the button inputs the digital signal 0.

Task2:Use the button to control the RGB light ring
1.Hardware Connection
Based on the hardware connection from Task 1, use a white silicone cable to connect the RGB light ring to Pin P24. After the hardware connection is successful, connect the UNIHIKER M10 to the computer using a USB cable.

2.Software Preparation
Add the WS2812 RGB light library. Click "Extensions", find "WS2812 RGB Light" in the pinpong library and click it to complete the addition.

3.Write a Program
How to use the button to control the RGB light ring to execute the chasing light program? When the button is pressed, the RGB light ring will execute the chasing light program. Use the “conditional judgment” block to determine whether the button is pressed.

If the button is pressed, the RGB light ring will execute the chasing light program. We already learned about the chasing light program in Lesson 3, so we can directly use it in this task.

In the program, pressing the button executes the chasing light program once, which means the light beads run around the light ring once. What should we do if we want the light beads to run around the light ring multiple times? Use the “repeat” count block to control the number of times the chasing light program is executed. The complete program is as follows:

4.Run the Program
Click "Run". Once the program runs successfully, pressing the button will make the RGB light ring execute the chasing light program three times. After the three executions of the chasing light program are completed, the light ring will turn off. As long as a button press is detected, the three-time chasing light program will continue to execute.

5.Have a try
In the above program, when a button press is detected, the chasing light program is fixedly repeated three times. How can we make the number of executions a random number between 1 and 5? Next, everyone should modify the above program to realize that after pressing the button, the RGB light ring executes the chasing light program a random number of times between 1 and 5.
Hint: Change the range number to a random number in the numeric type.

Task3:Make a food recommendation robot
1.Write a Program
Before starting to write the program, let's first analyze how to use the light ring to implement a food recommendation robot. In daily life, everyone has played with a lucky draw turntable, right? You spin the turntable hard, then the speed of the turntable slows down from fast to slow, and finally stops. The position pointed by the pointer is the prize we win. The principle of the food recommendation robot is similar to this lucky draw turntable, using the RGB light ring as the food turntable. Press the button, the light beads start to rotate, the rotation speed slows down from fast to slow, and finally the position where the light bead stops is the recommended food for us.

(1) Set the homepage prompt interface
First, based on the "Give it a try " section of Task 2, complete the interface setup for the UNIHIKER M10's homepage. Import the background and food images from the image materials into the file system.

Use the “text display” block to show the text "Press the button, and the food robot will recommend random food for you" on the UNIHIKER M10. Then use the “image display” block to display the background image on the UNIHIKER M10.

(2) Set the rotation speed of the light beads
In the above program, the light beads execute the chasing light program 1-5 times. However, in the chasing light program, the rotation speed of the light beads is very fast. How can we make the rotation of the light beads slow down from fast? To control the rotation speed of the light beads, we can modify “the waiting time (delay)”. After the light beads finish executing the fast chasing light program, we can lengthen the waiting time to slow down the rotation speed of the light beads.

Students must have noticed that the newly added program here is also a chasing light program, with only the waiting time being different. When a section of program needs to be reused, how can we improve efficiency? We can encapsulate this section of program into a function. However, the two chasing light programs run at different speeds, so how can we make one function execute at two different running speeds?
A new function block is needed here, called “the parameterized function” block. The specific usage method is as follows: click "Functions" in the modules, then click "Custom Module", and modify the function name to: chasing light.
Note: For the relevant introduction of "parameterized functions", please refer to the Knowledge Base.

Click "Add an input", change the parameter name to "duration", and click "Finish" to complete the creation of the parameterized function.

After the parameterized function is created successfully, the chasing light function appears in the script area, and the parameter name is "duration".

Place the chasing light program under the function, and change the waiting duration (0.2) in the program to the function parameter name "duration".

When calling the chasing light function, simply fill in the specific waiting duration into the parameters of the called function.

The program has realized that the rotation speed of the light beads slows down from fast to slow. How can we make the light beads slow down to a stop, with the stopping position being random? Execute the chasing light program once, set the parameter duration of the calling function to 0.3, so that the rotation speed of the light beads becomes slower.

How to make the light bead stop at a random position? For a random position, you can use the random number block. Create a new variable named "light bead" and assign it a random integer between 0 and 11. Then set the range of the for loop block to 0 - the variable "light bead" (a random number from 0 to 11). After the chasing light program is executed, set the light bead corresponding to the random number variable to light up.

(3) Display food images on the UNIHIKER M10
After completing the random food selection, it is also necessary to display the physical image corresponding to the light bead on the UNIHIKER M10. Create a new food recommendation function, and call the food recommendation function after the random number variable light bead lights up. Use the update number parameter block under the function to hide the background image and prompt text on the homepage.

To display the food image corresponding to the light bead on the UNIHIKER M10, use the “join” block to combine the food image's file name into the format: "food + Light Bead Variable + .png". Then use the update number parameter block to set the width of the food image to 200.

Finally, after the food recommendation is successful, wait for 3 seconds, use the update number parameter block to hide the food image, and the UNIHIKER M10 interface will return to the prompt interface of the homepage. The completed program is as follows:

2.Run the Program
Click "Run". After the program runs successfully, there will be a prompt on the UNIHIKER M10 interface explaining how to use the food recommendation robot. When the button is pressed, the beads of the RGB light ring start to rotate, with the rotation speed slowing down from fast to slow before stopping, and the UNIHIKER M10 will display the food image corresponding to the position where the light bead stops.
Note:
1.When building the food turntable on the RGB light ring, the IN interface of the RGB light ring corresponds to the light bead numbered 0. Therefore, the image "food0.png" (the image of the family bucket) needs to be placed on the No. 0 light bead.
2.Food turntable images are available in the image material library, and you can print them directly for use.

Knowledge Base
1.Understanding Parameterized Functions
What is a parameterized function? As the name suggests, a parameterized function is a function with parameters. A parameter, also called a parameter variable, is a variable. A parameterized function means that when creating a function, a variable is created along with the function. For example, in the following program, "duration" is the parameter of the chasing light function, and we can also understand "duration" as a new variable created in the chasing light function.

When calling a parameterized function, since the function has defined parameters, it is necessary to input specific parameter values when calling this function. This process is called passing parameters.

The parameter "duration" following the defined function name is called a formal parameter, while the specific parameter value passed in when calling the function is called an actual parameter. The relationship between formal parameters and actual parameters is that formal parameters are used to receive actual parameters. Therefore, when calling a parameterized function, modifying the parameter to 0.1, 0.2, or 0.3 allows you to control the rotation speed of the light beads.

2.Understanding the LED Button Module
What is a button? A button is a manual controller, also known as a pushbutton switch. It can only connect or disconnect current for a short time, so it is often used in control circuits to send instructional electrical signals to other electrical components or devices, thereby achieving the purpose of controlling other components or electrical equipment.

What parts does a button module mainly consist of? A button module is composed of a button cap, a return spring, a support connecting rod, a moving contact, a fixed contact, and a housing.

How does a button work? When you press the button by hand, the moving contact of the button moves down to the fixed contact to connect the circuit. When you release your hand, the moving contact moves up under the action of the spring, restoring the circuit to a disconnected state. The circuit is only connected when the button is pressed; otherwise, it remains disconnected. The digital button module with an LED light has a built-in LED light. When the moving contact and the fixed contact are connected, the LED light turns on; when the moving contact and the fixed contact are disconnected, the LED light turns off.

3.Command Overview

Challenge
Although the program has realized random food recommendations for users, there is a minor issue: when recommending food again after a food recommendation is completed, the light bead corresponding to the previously recommended food remains on. Next, everyone should modify the program to achieve the following: when the button is pressed, first turn off all the light beads of the RGB light ring, and then execute the chasing light program.








