1 of 21

Infrared Reflection Sensor

2 of 21

  1. Introduction to reflective sensors
  2. Task 1 - Print the sensor’s return value to the serial monitor
  3. Task 2 - Play sound and light when detecting obstacles
  4. Task 3 - Use the reflective sensor to recognize obstacles and count

CONTEN TS

3 of 21

  • We have learned about the light sensor to help Bittle sense the ambient light around it.
  • In this lesson, we will help Bittle detect obstacles.
  • Count obstacles and perform various tasks

woof~woof~

Let's get started!

4 of 21

Reflective Photoelectric Sensor

The basic working principle of reflective photoelectric sensor:

  1. The emitter emits the light.
  2. The light is reflected by an obstacle within a certain distance.
  3. The receiver converts the reflected light into electronic signals.

5 of 21

Think about how these sensors can be used in real life.

6 of 21

Infrared Reflection Sensor

  • The reflective photoelectric sensor is a photoelectric sensor that couples the transmitter and the receiver into one unit and uses the reflection principle to achieve photoelectric control. It can be used to detect changes in light and color of the ground, as well as detecting approaching objects.

7 of 21

Connect the Hardware

Connect the sensor to A2 and A3 ports, which are analog pins

8 of 21

Task 1

  • Read the return value of the sensor
  • Shall we digital read or analog read the pin?

\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.

9 of 21

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

10 of 21

Task 3

  • Based on Task 2, trigger the buzzer every time the LED is on

\CODE\8.03_OpenBuzzer.mp

You can choose the tone you like

11 of 21

Think & Analysis

  • Have we learned how to use reflective photoelectric sensors?
  • How can we use sensors to help Bittle count the number of obstacles?

12 of 21

1

2

  1. Set a variable
  2. The variable is incremented by 1 each time an obstacle is detected

13 of 21

Task 4

  • When the return value of the sensor is within 500, print the value to the serial monitor.
  • Increase the count of obstacles and print it to the serial monitor.

14 of 21

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

  • Declare a count variable to store the number of detected obstacles.
  • The count is incremented by 1 every time an obstacle is detected.
  • Finally, the count is printed on the serial port monitor

15 of 21

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

16 of 21

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

17 of 21

Task 5

  • Please write the code to achieve the above effect

18 of 21

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

19 of 21

Task 6

  • Write code that allows the Bittle to measure both sides at the same time and turn its head toward the closer end.

\CODE\6.07_TurnHead.sb3

20 of 21

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!

21 of 21

当检测到障碍物时, ledVal的值就逐渐增加,从而实现LED灯的亮度逐渐变亮;反之, ledVal的值就逐渐减小

注意ledVal的范围,请同学们思考如何解决这个问题