You must first complete Ending a while True Loop before viewing this Lesson

Reading TouchPad Inputs

Introduction

We will now be importing the lp_sensors library, which is responsible for the Touch, Temperature, and Light sensors on our Little Python Brain. For this lesson, we will also learn how to take TouchPad inputs.

Components Needed

  • Little Python Brain
  • A tablet/phone/laptop
Steps

1

Importing libraries

The lp_sensors library is responsible for making it possible to interface with the touch, temperature, and light sensors in the Little Python Brain. 

The sleep library, as we’ve previously used, will also be used for adding the sleep function to our code. 

2

Setting up the TouchPads

Just like the buzzer and neopixel, we have to set up the touchpads first. From the Setup category, get the four xButton = lp_sensors.touchX blocks. Note the name of the variables we’ve set up (L1Button, L2Button, R1Button, R2Button)! We’ll be using them later, in step 4.

3

Creating the main loop

As with previous lessons, we’ll be making our loop from the while True block.

4

The xButtonValue = xButton.measure() block

From the Inputs category, get this block. For simplicity’s sake, we’ll call this the measure touchpadblock.

  • Variable Name – can be changed. This is basically what we’ll be calling when we want to get the value that we’re measuring
  • TouchPad – note the names we’ve set up on Step 2. This is where we’ll be putting either L1Button, L2Button, R1Button, or R2Button.

5

Changing the parameters of the measure touchpad block

Drag four of these blocks inside our while True loop. We’ll change the names of each of them to L1, L2, R1, and R2. Afterwards, we’ll just have to make sure that they are measuring the touchpad corresponding to them which we set up on Step 2.

 

6

Printing the measured values

We will then print the values that we’re measuring. We can print ALL the readings using just one print() block by separating them with a comma.

6

Adding sleep

Finally, we’ll be adding a sleep(1) block in the end of our loop. Run the code!

The Complete Code

Try touching each of the touchpads and observe the change in the values that you’re seeing. Don’t worry, the exact values themselves are not that important, what we’ll be working with is the significant change that occurs when you hold a touchpad. In the next lesson, we’ll be learning how to integrate these touchpads into conditional statements. 

Back to: Beginner > Kalimba (Handheld Piano)
© 2020 Little Python

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account