Page 1 sur 1

guirlande de led automatique

Posté : lun. 19 avr. 2021 10:57
par PapyMougeot
Bonjour j'utilise un programme trouvé sur le toile pour faire une rampe de led et je voudrais qu'il démarre sans que j'appuie sur le poussoir (sans le retirer) nécessaire au fonctionnement de la boucle.
Je ne sais pas quel paramètre modifier ?

Voici le programme:

import RPi.GPIO as GPIO
import time

LED1 = 14
LED2 = 15
LED3 = 18
LED4 = 10
LED5 = 24
BUTTON = 23

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # mode de numérotation des pins GPIO18
#or GPIO.setmode(GPIO.BOARD) #12
GPIO.setup(LED1,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)
GPIO.setup(LED3,GPIO.OUT)
GPIO.setup(LED4,GPIO.OUT)
GPIO.setup(LED5,GPIO.OUT)
GPIO.setup(BUTTON,GPIO.IN)


mode_auto = 0
last_button_statement = 0
try:
while True:

if GPIO.input(BUTTON) == 1 and last_button_statement != 1:
if mode_auto == 1:
mode_auto = 0
print("NoAuto")
else:
mode_auto = 1
print("Auto")
print("Bouton")
last_button_statement = 1
elif GPIO.input(BUTTON) == 0 :
last_button_statement = 0

if mode_auto == 1 :
GPIO.output(LED1,GPIO.HIGH)
time.sleep(0.1)
GPIO.output(LED2,GPIO.HIGH)
time.sleep(0.1)
GPIO.output(LED3,GPIO.HIGH)
time.sleep(0.1)
GPIO.output(LED4,GPIO.HIGH)
time.sleep(0.1)
GPIO.output(LED5,GPIO.HIGH)
time.sleep(0.1)
GPIO.output(LED1,GPIO.LOW)
time.sleep(0.2)
GPIO.output(LED2,GPIO.LOW)
time.sleep(0.2)
GPIO.output(LED3,GPIO.LOW)
time.sleep(0.2)
GPIO.output(LED4,GPIO.LOW)
time.sleep(0.2)
GPIO.output(LED5,GPIO.LOW)
time.sleep(0.2)

except KeyboardInterrupt:
GPIO.output(LED1, GPIO.LOW)
GPIO.output(LED2, GPIO.LOW)
GPIO.output(LED3, GPIO.LOW)
GPIO.output(LED4, GPIO.LOW)
GPIO.output(LED5, GPIO.LOW)
GPIO.cleanup()
print("\nEnd of program\n")

Re: guirlande de led automatique

Posté : mar. 3 août 2021 09:47
par piper
Bonjour,
En python, l'indentation (les espaces ou tabulations) au début de chaque lignes sont essentielles pour savoir ce qui est fait dans chaque instructions conditionnelles (if, elif, else, while, for etc...)

Hors ton copier coller n'a pas garder les indentation
Donc il y a des else dont on ne sait pas trop à quel if ils correspondent et des actions dont on ne voit pas bien quand elles démarrent.
Ex : ceci est clair :

Code : Tout sélectionner

if toto=1:
   tutu=0
else:
   tutu=2
   toto=0
Ceci n'est pas clair :

Code : Tout sélectionner

if toto=1:
tutu=0
else:
tutu=2
toto=0
Pourrais-tu mettre les indentations ?
Je pense que le bouton "code" (</>) devrait t'aider à garder les indentations
Ex :