RASP-SENSE-HAT-ASTRO-PI-Bouton

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche

Détecter un bouton (dans le code)

1. Ouvrir Python 3 dans une fenêtre terminal en tant que super utilisateur avec sudo en tapant:

sudo idle3 &

2. Une fenêtre 'Python Shell' apparaîtra.

3. Sélectionnez le menu File > New Window (Fichier > Nouvelle fenêtre).

Tapez le code suivant (ou faite un copier/collr):

import RPi.GPIO as GPIO
import time
from sense_hat import SenseHat

sense = SenseHat()

UP = 26     # Haut
DOWN = 13   # bas
LEFT = 20   # gauche
RIGHT = 19  # droite
A = 16
B = 21  

running = True # Variable pour 'en_cours_d_execution'

# Fonction appelee lorsqu un bouton est active
def button_pressed(button):
    global running
    global sense

    # Afficher le bouton (la broche GPIO activee) 
    print(button)
    sense.show_message(str(button))

    # Bouton B: signaler l arret du programme
    if button == B:
        running = False

GPIO.setmode(GPIO.BCM)

# Configurer les broches en entree
for pin in [UP, DOWN, LEFT, RIGHT, A, B]:
    GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)

    # attacher un évenement sur le changement d etat 
    # d une broche GPIO. La fonction appelee est boutton_pressed
    GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_pressed, bouncetime=100)

# Le programme principale de fait rien... il attend
# Tout se passe dans les événements
while running:
    time.sleep(1)

sense.show_message("Au revoir")

5. Sélectionnez File > Save (Fichier > Sauver) et choisissez un nom de fichier pour votre programme.

6. Sélectionnez ensuite Run > Run module (Executer > Executer module).

7. This program will just display the corresponding GPIO number every time a button is pressed. If you press the B button (bottom pair, right) then the program ends. This should allow you to test that your wiring is correct before proceeding.

8. The code above makes the button_pressed function run whenever any button is pressed. However, there are many different ways you can program the button detection. For instance, you might want to make your code wait until a button is pressed before doing something. Here is an example of how to do that using the UP button:

import RPi.GPIO as GPIO
from sense_hat import SenseHat

sense = SenseHat()

UP = 26
DOWN = 13
LEFT = 20
RIGHT = 19
A = 16
B = 21  

GPIO.setmode(GPIO.BCM)

for pin in [UP, DOWN, LEFT, RIGHT, A, B]:
    GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)

sense.show_message("Press UP to Start")
GPIO.wait_for_edge(UP, GPIO.FALLING)
sense.show_message("Nous y voici!")

9. You might also want to test if a button is being held down and perhaps do something if it was held down for over 3 seconds. Here is another example:

import RPi.GPIO as GPIO
import time
from sense_hat import SenseHat

sense = SenseHat()

UP = 26
DOWN = 13
LEFT = 20
RIGHT = 19
A = 16
B = 21  

GPIO.setmode(GPIO.BCM)

for pin in [UP, DOWN, LEFT, RIGHT, A, B]:
    GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)

while GPIO.input(A) == GPIO.HIGH:  # wait while HIGH / not pressed
    time.sleep(0.01)

button_down_time = time.time()  # record the time when the button went down

held_down = False

while GPIO.input(A) == GPIO.LOW:  # wait while LOW / pressed
    time.sleep(0.01)
    if time.time() - button_down_time > 3:  # has 3 seconds gone by?
        held_down = True
        break

if held_down:
    print("Held down")
    sense.show_message("Here we go!")
else:
    print("Not held down")

Lorsque vous maintenez le bouton A enfoncé pendant 3 secondes, vous devriez voir le message "Nous y voici".


Source: Getting Started with Astro PI et Astro-Pi Guide proposé par Raspberry Pi Learning Resource (www.raspberrypi.org)

Licence Creative Commons - CC-BY-SA
The learning resource is provided for free by the Raspberry Pi Foundation under a Creative Commons licence.
Find more at raspberrypi.org/resources and github.com/raspberrypilearning.

Traduction réalisée par Meurisse. D pour shop.MCHobby.be - Licence CC-BY-SA.
Crédit de traduction: Toute référence, mention ou extrait de cette traduction doit également être explicitement accompagné du crédit de traduction suivant : «  Traduction par MCHobby (shop.MCHobby.be) » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.