Deutsch English |
YOU LEARN HERE... |
how to use the two Calliope buttons to develop interactive programmes. |
REACT TO PRESSING A BUTTON |
|
Program: from calliope_mini import * while True: if button_a.is_pressed(): display.show(Image.NO) else: display.clear() sleep(10) The seemingly superfluous sleep(10) is important so that you don't waste unnecessary computer resources if the programme doesn't have to do anything other than check whether the button is pressed. In technical terms, the state of the button is also said to be ‘polled’ in the infinite loop. |
REACTING TO THE CLICK OF A BUTTON |
In your example, clicking on button A displays the image SQUARE and clicking on button B displays the image NO. In contrast to the first example, you do not have to keep the buttons pressed.
Program: from calliope_mini import * def blink(x, y): display.set_pixel(x, y, 9) sleep(500) display.set_pixel(x, y, 0) sleep(500) while True: if button_a.is_pressed(): display.show(Image.SQUARE) sleep(1000) display.clear() blink(2, 2) sleep(10) Just as you know it from mouse clicks, you can also interrupt a running programme with a button click and execute another action. In an endless while loop, the centre LED flashes with a period of 200 ms. Click on button A to interrupt the flashing and display a square for 1000 ms. The programme then resumes flashing. You also use the button_a.was_pressed() function here. The click is regarded as an event that is registered by the system even if your programme is doing something else. Program: from calliope_mini import * def blink(x, y): display.set_pixel(x, y, 9) sleep(500) display.set_pixel(x, y, 0) sleep(500) while True: if button_a.was_pressed(): display.show(Image.SQUARE) sleep(1000) display.clear() blink(2, 2) sleep(10) |
REMEMBER... |
You can develop interactive programmes that react to a button being held down or to a button click. With the is_pressed() function, the button must be pressed for True to be returned; with the was_pressed() function, True is returned if the button has been clicked at any time since the programme was started or since the last button click. |
TO SOLVE BY YOURSELF |
|