Created
March 26, 2019 16:14
-
-
Save unixfox/4c620c55cfbafe3d2659119b1e6c917a to your computer and use it in GitHub Desktop.
Exercices architecture de l'ordinateur Henallux Q2 manipulation 4 Raspberry pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
from time import sleep | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(24, GPIO.OUT) | |
try: | |
while 1: | |
GPIO.output(24, 1) | |
sleep(0.1) | |
GPIO.output(24, 0) | |
sleep(0.1) | |
except KeyboardInterrupt: | |
GPIO.cleanup() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# script by Alex Eames https://raspi.tv | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BCM) | |
# GPIO 23 & 24 set up as inputs. One pulled up, the other down. | |
# 23 will go to GND when button pressed and 24 will go to 3V3 (3.3V) | |
# this enables us to demonstrate both rising and falling edge detection | |
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) | |
# now we'll define the threaded callback function | |
# this will run in another thread when our event is detected | |
def my_callback(channel): | |
print "Rising edge detected on port 24 - even though, in the main thread," | |
print "we are still waiting for a falling edge - how cool?\n" | |
print "Make sure you have a button connected so that when pressed" | |
print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n" | |
print "You will also need a second button connected so that when pressed" | |
print "it will connect GPIO port 24 (pin 18) to 3V3 (pin 1)" | |
raw_input("Press Enter when ready\n>") | |
# The GPIO.add_event_detect() line below set things up so that | |
# when a rising edge is detected on port 24, regardless of whatever | |
# else is happening in the program, the function "my_callback" will be run | |
# It will happen even while the program is waiting for | |
# a falling edge on the other button. | |
GPIO.add_event_detect(24, GPIO.RISING, callback=my_callback) | |
try: | |
print "Waiting for falling edge on port 23" | |
GPIO.wait_for_edge(23, GPIO.FALLING) | |
print "Falling edge detected. Here endeth the second lesson." | |
except KeyboardInterrupt: | |
GPIO.cleanup() # clean up GPIO on CTRL+C exit | |
GPIO.cleanup() # clean up GPIO on normal exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# script by Alex Eames http://RasPi.tv | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
# GPIO 24 set up as an input, pulled down, connected to 3V3 on button press | |
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) | |
def my_callback(channel): | |
print "falling edge detected on 17" | |
def my_callback2(channel): | |
print "falling edge detected on 23" | |
raw_input("Press Enter when ready\n>") | |
GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300) | |
GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback2, bouncetime=300) | |
try: | |
print "Waiting for rising edge on port 24" | |
GPIO.wait_for_edge(24, GPIO.RISING) | |
print "Rising edge detected on port 24. Here endeth the third lesson." | |
except KeyboardInterrupt: | |
GPIO.cleanup() # clean up GPIO on CTRL+C exit | |
GPIO.cleanup() # clean up GPIO on normal exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# script by Alex Eames http://RasPi.tv | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(24, GPIO.OUT) | |
# GPIO 24 set up as an input, pulled down, connected to 3V3 on button press | |
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) | |
def my_callback(channel): | |
GPIO.output(24, 1) | |
def my_callback2(channel): | |
GPIO.output(24, 0) | |
raw_input("Press Enter when ready\n>") | |
GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback, bouncetime=300) | |
GPIO.add_event_detect(18, GPIO.FALLING, callback=my_callback2, bouncetime=300) | |
try: | |
print "Waiting for rising edge on port 24" | |
GPIO.wait_for_edge(17, GPIO.RISING) | |
print "Rising edge detected on port 24. Here endeth the third lesson." | |
except KeyboardInterrupt: | |
GPIO.cleanup() # clean up GPIO on CTRL+C exit | |
GPIO.cleanup() # clean up GPIO on normal exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
from time import sleep | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(24, GPIO.OUT) | |
led = GPIO.PWM(24, 100) | |
led.start(100) | |
pause_time = 0.02 | |
try: | |
while True: | |
for i in range(0,101): | |
led.ChangeDutyCycle(i) | |
sleep(pause_time) | |
for i in range(100,-1,-1): | |
led.ChangeDutyCycle(i) | |
sleep(pause_time) | |
except KeyboardInterrupt: | |
led.stop() | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment