buttons
Deutsch   English   

6. BUTTONS

 

 

YOU LEARN HERE...

 

how to use the two Calliope buttons to develop interactive programmes.

 

 

HOW THE BUTTONS WORK

 

The Calliope has two programmable buttons that make it possible to interactively influence the programme sequence during execution. The left one is addressed with the variable button_a, the right one with button_b.


 

The function button_a.is_pressed()returns True if the left button is pressed (analogue button_b_is pressed()).

 

 

EXAMPLES

 

Example 1: Reacting to the pressing of a button

The programme waits in an infinite loop until button A or button B is pressed. Then the right or left LED starts to flash with a period of 1s for as long as the button is pressed.

The status of the buttons is queried every 200 milliseconds (delay(100)). These pauses are important, otherwise the system will be overloaded.

 

Program:

from calliope_mini import *
from callibot import *

repeat:
    if button_b.is_pressed()::
        setLED(1)
        delay(400)        
        setLED(0)
        delay(400)
     delay(100)     
► Copy to clipboard

The commands for the buttons are in the calliope_mini module, which you must also import.


Example 2: Reacting to the click of a button

A short melody should be played when button A or B is clicked. The button_a.was_pressed() function returns True if button a has been pressed briefly. The play(JUMP_UP) command from the music module plays a short melody. Some melodies are built into the music module. The music module can only be imported in real mode. The status of the buttons is queried every 100 milliseconds.

Program:

from calliope_mini import *
from callibot import *
from music import *

repeat: 
    if button_a.was_pressed():
        play(JUMP_UP)
    if button_b.was_pressed():
        play(JUMP_DOWN)
    delay(100)    
► Copy to clipboard

 

Example 3: Cancelling a programme with a button

With moving robots, it is advantageous to use an endless loop that is only repeated until a button is pressed.

The robot moves to the obstacle that it detects with its ultrasonic sensor and back again - as long as button A has not been pressed.

 

As long as button A has not been pressed, the robot repeats the commands in thewhile loop. The loop is ended with the button click and the programme is continued with the stop() command..

Program:

from calliope_mini import *
from callibot import *

setSpeed(30)
forward()
while not button_a.was_pressed():
    d = getDistance()
    if d < 10:
        backward()
        delay(1000)
        forward()
    delay(200)
stop() 
► Copy to clipboard

 

Example 4: Controlling the robot with buttons

Click on button a or b to guide the robot through the maze. The robot moves forwards, when it ‘sees’ dark with its light sensors it briefly moves backwards and stops. If you press button a, it turns 90° to the left and moves forwards again until it reaches the next dark edge. Click on button b to turn right.



 

Program:

from calliope_mini import *
from callibot import *

forward()
repeat:
    v = irLeft.read_digital()
    if v == 0:
        backward()
        delay(500)
        stop()
    if button_a.was_pressed():
        left()
        delay(550)
        forward()
    elif button_b.was_pressed():
        right()
        delay(550)     
        forward()
    sleep(10)
► Copy to clipboard

 

 

REMEMBER...

 

You can use buttons to develop interactive programmes. The is_pressed() function returns True if the button is pressed. The was_pressed() function returns True if the button has been clicked at any time since the programme was started or since the last call.

 

 

TO SOLVE BY YOURSELF

 

 

1.

Click on the button a to switch on both LEDs and play the melody ‘POWER_UP’, click on the button b to switch off both LEDs and play the melody ‘POWER_DOWN’.

 

2.

By clicking on button a, the robot should move forwards and stop for 2000 milliseconds; by clicking on button b, the robot should move backwards and stop for 2000 milliseconds.

3.

The robot should drive through a course by clicking on button a or button b to tell it whether it should turn left or right. It recognises the walls of the course itself with its distance sensor.

 

 

 
 

 

   

 

6-1
Technical Notes:

Melody built into the music module.

ADADADUM - Beethoven 5. Sinfonie C Moll
ENTERTAINER - Scott Joplin's Ragtime
PRELUDE -J.S.Bach 48 Preludien aund Fugen
ODE - "Ode on Joy" Beethoven 9. Sinfonie D Moll
NYAN - Nyan Phone tone
FUNK - Secret agent
BLUES - Boogie-Woogie Blues
BIRTHDAY - Happy Birthday to You
WEDDING - Wagner Oper "Lohengrin"
FUNERAL - funeral procession, Chopin Klaviersonate
PUNCHLINE - a funny Tonclip
PYTHON - Monty Python Flying Circust;
BADDY - Filmclip The Baddy
CHASE - Filmclick Jagdszene
BA_DING - Signalton
WAWAWAWAA - Posaunenklang
JUMP_UP - for games, up movement
JUMP_DOWN - for games, down movement
POWER_UP - power up
POWER_DOWN -power downt