You must first complete Reading Touchpad Inputs before viewing this Lesson

TouchPad Conditional Statements

Introduction

Since we learned how to take measurements of the touchpad values, it’s now time to use those to work with outputs!

Components Needed

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

1

Setting up the buzzer

For this project, we will be making a simple 4-note piano. The first step in doing that is to set up our buzzer.

2

Clearing the loop

We will then clear out all the stuff inside the while True block. We will instead be using conditional statements (if and elif) to control the output of the buzzer.

3

The .is_touched() function

There is a built in function inside our touchpads. We can use this by simply typing is_touched() after the button name we initiated on Step 2 of the previous lesson. This, in turn, can be used inside the condition spaces of the if and elif condition blocks

4

Setting our conditions

We will then drag 1 if condition block and 3 elif condition blocks inside our loop. We will change the condition to that of our button + is_touched(). 

Copy the blocks shown.

 

5

Making the Piano Notes

Inside each conditional statement, we will drag a bz.playNote() block. We’ll let the first one play ‘c4’ (middle C on a traditional piano!), the second should be ‘e4’, the third should be ‘g4’, then finally the last should be ‘c5’. Keep all note durations as 100.

Run the code!

The Complete Code

Try playing a few notes! Simply press Ctrl+C when you want to end the code.

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

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account