Infrared Reflection Sensor
CONTEN TS
woof~woof~
Let's get started!
Reflective Photoelectric Sensor
The basic working principle of reflective photoelectric sensor:
Think about how these sensors can be used in real life.
Infrared Reflection Sensor
Connect the Hardware
Connect the sensor to A2 and A3 ports, which are analog pins
Task 1
\CODE\8.01_PrintDistance.mp
The sensor will return a value caused by the obstacle in front, and it is monotonically related to the distance.
Task 2
Requirement:
If the return value of the sensor is within 100
Light up the LED
Else
Turn off the LED lights
\CODE\8.02_OpenLEDinRange100.mp
Task 3
\CODE\8.03_OpenBuzzer.mp
You can choose the tone you like
Think & Analysis
1
2
Task 4
Task 4
There’s a serious problem:
The count is very large even if only one obstacle is detected.
Why?
\CODE\8.04_CountBlock.mp
Task 4
When the difference between the two readings is greater than 100, we consider there’s a new object.
\CODE\8.05_CountBlockInAGoodWay.mp
Think & Analysis
How to realize that when an obstacle is detected, the brightness of the LED increases as the detection time increases;
If the LED is at its brightest:
Keep it at its brightest
And play an alarm
If no obstacle is detected
The brightness of the LED gradually dimmed
If the LED is darkest,
It is kept at its darkest state
Task 5
Determine if an obstacle leftVal<500 is detected
Increment the count variable by 1 if detected.
Check whether the lightLevel variable is greater than 255.
If it is greater than 255, it is assigned the value 255.
Then a tone is played
Light the LED with lightLevel
lightLevel is 10 times the value of count.
Else (no obstacle detected)
Subtract 1 from the variable ledVal.
Determining whether a variable is less than 0
If it is less than 0, it is assigned a value of 0.
\CODE\6.06_CountBlockAndOpenLEDandBuzzer.sb3
Task 6
\CODE\6.07_TurnHead.sb3
woof~woof~
Great, we've helped Bittle make sense of our world again - be able to detect obstacles
Next time we will unlock a cool device on Bittle. Let's look forward to it!
当检测到障碍物时, ledVal的值就逐渐增加,从而实现LED灯的亮度逐渐变亮;反之, ledVal的值就逐渐减小
注意ledVal的范围,请同学们思考如何解决这个问题