1 of 14

Touch Sensor

2 of 14

  1. Introduction to touch sensors
  2. Use Digital Read block
  3. Use a buzzer to play sound
  4. Check if the touch sensor is pressed

CONTEN TS

3 of 14

  • In this lesson, we attach dual touch sensors to Bittle:
    • When the left sensor is touched, Bittle turns his head to the right.
    • When the right sensor is touched, Bittle turns his head to the left.

Bittle doesn’t like people touching his stuff

Huh!

4 of 14

Connect the Sensor

5 of 14

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.

6 of 14

After wiring the sensor, we can display the sensor value on the serial port monitor

Let Bittle read the value of a touch sensor

7 of 14

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

8 of 14

Think & Analysis

  • Now that we have a sensor return value, what can we do with this return value?
  • Can the return values from both sensors be read at the same time?
  • What can Bittle do with the two values?

9 of 14

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

10 of 14

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

11 of 14

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!

12 of 14

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.

13 of 14

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

14 of 14

It’s great that Bittle can sense the environment and react

Next time we'll continue to help Bittle perceive the world

woof~

woof~