Open KidsBlock and connect the board to computer. Choose File –> Load from your computer.
Choose path D:\Code\2.Code_kidsIOT and open 4.3Remote control.sb3.
Click to connect to port and then .
Code Blocks
Blocks |
Code block |
---|---|
Build blocks:
Click to import libraries DC Motor for esp32 and ir remote.
Initialization
Define four variables with initial values of 0: key1_press_num; key2_press_num; key3_press_num; key.
The first three variables are used to store the pressing times of key 1, key 2 and key 3; the last one records the data received by IR receiver.
Zero out INA and INB, and initialize the IR receiver pin IO13.
Main Code
Loop: Press key 1, key 2 and key 3, the fan will rotates at different speed. Press again, the fan stops. These three speeds can have a switchover without stopping the fan.
Build blocks:
①
② Add , , , and as follows:
When an infrared signal is received, assign the received value to the variable key and then refresh the received value.
③ Put , , , and as follows: It determines whether the key 1 is pressed.
If key = 16738455 (key 1 is pressed), key1_press_num will adds one, and key2_press_num and key3_press_num will zero out.
④ Put , , , and as follows:
These code blocks determine whether the remainder of key1_press_num divided by 2 equals 1.
remainder = 1: fan rotates.
remainder ≠ 1: fan stops.
⑤ Put the blocks in ④ into ③:
For this whole, the fan will rotate at a high speed if key 1 is pressed; it stops when key 1 is pressed again.
Analysis:
Press key 1 once, key1_press_num(initial value is 0) adds one, and key2_press_num and key3_press_num zero out.
Determine whether the remainder of key1_press_num divided by 2 equals 1: herein, 1 ÷ 2 = 0…1, so the condition is met, and the fan rotates at high speed.
Press key 1 again, key1_press_num adds one. Now it equals 2, and the other two values is 0.
Determine whether the remainder of key1_press_num divided by 2 equals 1: herein, 2 ÷ 2 =1…0, so the condition is not satisfied, and the fan stops.
⑥ Duplicate the “if” block in ⑤ twice.
As follows:
The working principles are the same. These two copies are used to control key 2 and key 3. For key 2, the fan will rotates at a medium speed; For key 3, it is at low speed.
⑦ Drag blocks of ⑥ into ②, as follows:
When infrared signals are received, determine whether they are the decimal codes of key 1, key 2 and key 3.
Then, determine the number of pressing time is odd or even. Odd: the fan rotates at corresponding speed; Even: fan stops.
Q :Why zero out the other two variables?
These three keys work according to the same principle. When the pressing times is an odd number, the fan rotates; otherwise, the fan stops.
If we do not zero out them, we cannot make sure every time these three speeds can have a switchover without stopping the fan. This is because we have no idea whether the value of the other two is always even, so the fan may change speed or stop when we press another key. We zero out them when we press one key, therefore the fan can immediately change its speed without stopping when we press another key.
A : This step ensures that, after we press one key, the values of the other two are always even, so that these three speeds can have a switchover without stopping the fan.