Touch Sensor
CONTEN TS
Bittle doesn’t like people touching his stuff
Huh!
Connect the Sensor
How Does a Touch Sensor Work
When the finger acts as a resistor, a slight current flows through, and the sensor receives a signal
A touch sensor is an electronic device capable of sensing human contact through capacitance changes caused by the human body, which generates an electrical signal. The signal can be detected by the controller chip.
After wiring the sensor, we can display the sensor value on the serial port monitor
Let Bittle read the value of a touch sensor
Digital Read
If the sensor senses a human touch
The serial monitor prints 1
Else
The serial monitor Prints 0
\CODE\6.01_TouchAndPrint.mp
Think & Analysis
Task 1
Please write code to achieve the following:
If Bittle detects someone touching the left sensor:
He turns his head to the right
If someone touches the right sensor:
He turns his head to the left
\CODE\6.02_TouchAndTurnHead.mp
Task 2
Please modify the previous code to set an alarm for Bittle
If the sensor detects a human touch
(either left or right touch)
Play the alarm immediately
Else
Stop playing
\CODE\6.03_TouchAndOpenBuzzer.mp
Task 3
Write code to recognize simultaneous touches.
Think about the problems with using the original code directly.
If the left and right touch sensors are touched simultaneously
Light the LED and walk backward;
Else,
If touched on one side:
Play an alarm and tilt Bittle’s head to the other side;
If not touched:
Turn off the alarm, turn off the LED, and sit down.
Bittle will take care of it!
Task 3
\CODE\6.04_DoubleTouchAndDrawBack.mp
Our code is getting longer. The future examples may not show the basic starting and connecting serial port building blocks. You can add them on your own.
Task 4
Write code to identify the case of a long press.
If the touch on the left sensor is long
Light up the LED
Else if the touch is short
The buzzer alarms
Our code is getting longer. The future examples may not show the basic starting and connecting serial port building blocks. You can add them on your own.
\CODE\6.05_LongPress.mp
It’s great that Bittle can sense the environment and react
Next time we'll continue to help Bittle perceive the world
woof~
woof~