Modifications

Sauter à la navigation Sauter à la recherche
4 311 octets ajoutés ,  22 octobre 2015 à 19:09
Ligne 56 : Ligne 56 :     
[[fichier:RASP-SENSE-HAT-ASTRO-PI-Bouton-03.png]]
 
[[fichier:RASP-SENSE-HAT-ASTRO-PI-Bouton-03.png]]
 +
 +
In the diagram above we wire the GPIO pin to 3.3 volts through a large 10k ohm resistor so that it always reads {{fname|HIGH}}. Then we can short the pin to ground via the button, so that the pin will go {{fname|LOW}} when you press it.
 +
 +
So {{fname|HIGH}} means button up and {{fname|LOW}} means button down.
 +
 +
Fortunately, the Raspberry Pi has all the above circuitry built in; we can select a pull up circuit in our code for each GPIO pin, which sets up some internal circuitry that is too small for us to see. So you can get away with just using two jumper wires here, although you're welcome to wire it up the way shown above if you wish.
 +
 +
All we now need to do is create the above circuit six times for each of the GPIO pins.
 +
 +
== Branchement sur breadboard ==
 +
The diagram below shows how to wire up the six buttons on a breadboard so that they match the flight hardware. As always, wire colour does not matter. The numbers next to each button indicate the GPIO number that they are connected to. Every button requires one side to be connected to ground so that the {{fname|HIGH}} GPIO pin can be shorted to {{fname|LOW}} when the button is pressed.
 +
 +
[[fichier:RASP-SENSE-HAT-ASTRO-PI-Bouton-10.png]]
 +
 +
[[fichier:RASP-SENSE-HAT-ASTRO-PI-Bouton-11.png]]
 +
 +
== Détecter un bouton (dans le code) ==
 +
'''1.''' Open Python 3 from a terminal window en tant que super utilisateur avec {{fname|sudo}}  by typing:
 +
 +
<nowiki>sudo idle3 &</nowiki>
 +
 +
'''2.''' A Python Shell window will now appear.
 +
 +
'''3.''' Select {{fname|File > New Window}}.
 +
 +
Type in or copy/paste the following code:
 +
 +
<nowiki>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 
 +
 +
running = True
 +
 +
def button_pressed(button):
 +
    global running
 +
    global sense
 +
    print(button)
 +
    sense.show_message(str(button))
 +
    if button == B:
 +
        running = False
 +
 +
GPIO.setmode(GPIO.BCM)
 +
 +
for pin in [UP, DOWN, LEFT, RIGHT, A, B]:
 +
    GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
 +
    GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_pressed, bouncetime=100)
 +
 +
while running:
 +
    time.sleep(1)
 +
 +
sense.show_message("Bye")</nowiki>
 +
 +
'''5.''' Select {{fname|File > Save}} and choose a file name for your program.
 +
 +
'''6.''' Then select {{fname|Run > Run 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 {{fname|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:
 +
 +
<nowiki>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!")</nowiki>
 +
 +
'''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:
 +
 +
<nowiki>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")</nowiki>
 +
 +
Lorsque vous maintenez le bouton '''A''' enfoncé pendant 3 secondes, vous devriez voir le message "Nous y voici".
    
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}
 
{{RASP-SENSE-HAT-ASTRO-PI-TRAILER}}
29 917

modifications

Menu de navigation