Grüße,
zu Anfang mein Code:
Python
import wiringpi2 as wiringpi
from time import sleep # allows us a time delay
wiringpi.wiringPiSetup() # using wiringPi Libary
wiringpi.pinMode(0, 1) # sets PIN 0 to output
wiringpi.pinMode(1, 1) # sets PIN 1 to output
wiringpi.pinMode(2, 1) # sets PIN 2 to output
wiringpi.pinMode(3, 1) # sets PIN 3 to output
wiringpi.pinMode(4, 1) # sets PIN 4 to output
wiringpi.digitalWrite(0, 0) # sets port 0 to 0 (0V, off)
wiringpi.digitalWrite(1, 0) # sets port 1 to 0 (0V, off)
wiringpi.digitalWrite(2, 0) # sets port 2 to 0 (0V, off)
wiringpi.digitalWrite(3, 0) # sets port 3 to 0 (0V, off)
wiringpi.digitalWrite(4, 0) # sets port 4 to 0 (0V, off)
wiringpi.pinMode(5, 0) # sets PIN 5 to input
try:
while True:
if wiringpi.digitalRead(5): # If button on PIN 5 pressed
wiringpi.digitalWrite(0, 1) # switch on LED. Sets port 0 to 1 (3V3, on)
wiringpi.digitalWrite(1, 1) # switch on LED. Sets port 1 to 1 (3V3, on)
wiringpi.digitalWrite(2, 1) # switch on LED. Sets port 2 to 1 (3V3, on)
wiringpi.digitalWrite(3, 1) # switch on LED. Sets port 3 to 1 (3V3, on)
wiringpi.digitalWrite(4, 1) # switch on LED. Sets port 4 to 1 (3V3, on)
else:
wiringpi.digitalWrite(0, 0) # switch off LED. Sets port 0 to 0 (0V, off)
wiringpi.digitalWrite(1, 0) # switch off LED. Sets port 1 to 0 (0V, off)
wiringpi.digitalWrite(2, 0) # switch off LED. Sets port 2 to 0 (0V, off)
wiringpi.digitalWrite(3, 0) # switch off LED. Sets port 3 to 0 (0V, off)
wiringpi.digitalWrite(4, 0) # switch off LED. Sets port 4 to 0 (0V, off)
sleep(0.05) # delay 0.05s
finally: # when you CTRL+C exit, we clean up
wiringpi.digitalWrite(0, 0) # sets port 0 to 0 (0V, off)
wiringpi.digitalWrite(1, 0) # sets port 1 to 0 (0V, off)
wiringpi.digitalWrite(2, 0) # sets port 2 to 0 (0V, off)
wiringpi.digitalWrite(3, 0) # sets port 3 to 0 (0V, off)
wiringpi.digitalWrite(4, 0) # sets port 4 to 0 (0V, off)
wiringpi.pinMode(0, 0) # sets PIN 0 back to input Mode
wiringpi.pinMode(1, 0) # sets PIN 1 back to input Mode
wiringpi.pinMode(2, 0) # sets PIN 2 back to input Mode
wiringpi.pinMode(3, 0) # sets PIN 3 back to input Mode
wiringpi.pinMode(4, 0) # sets PIN 4 back to input Mode
# PIN 5 is already an input, so no need to change anything
Display More
Mit diesem simplen Code steuere ich gleichzeitig 5 LED über einen Taster an.
Nun würde ich gerne das jede Lampe bei gedrückthalten des Tasters einzeln mit Verzögerung leuchten oder dass wenn man 1x auf den Buzzer drückt auch nur eine Lampe leuchtet und beim wiederholten Drücken die zweite anfängt zu leuchten bis zur letzten.
Könnt ihr mir behiflich sein? Mit 'sleep' zwischen der Ansteuerung der LED'S hat es leider nicht funktioniert.
Gruß
EDIT:
Wenn ich bei:
Code
try:
while True:
if wiringpi.digitalRead(5): # If button on PIN 5 pressed
wiringpi.digitalWrite(0, 1) # switch on LED. Sets port 0 to 1 (3V3, on)
wiringpi.digitalWrite(1, 1) # switch on LED. Sets port 1 to 1 (3V3, on)
wiringpi.digitalWrite(2, 1) # switch on LED. Sets port 2 to 1 (3V3, on)
wiringpi.digitalWrite(3, 1) # switch on LED. Sets port 3 to 1 (3V3, on)
wiringpi.digitalWrite(4, 1) # switch on LED. Sets port 4 to 1 (3V3, on)
else:
wiringpi.digitalWrite(0, 0) # switch off LED. Sets port 0 to 0 (0V, off)
wiringpi.digitalWrite(1, 0) # switch off LED. Sets port 1 to 0 (0V, off)
wiringpi.digitalWrite(2, 0) # switch off LED. Sets port 2 to 0 (0V, off)
wiringpi.digitalWrite(3, 0) # switch off LED. Sets port 3 to 0 (0V, off)
wiringpi.digitalWrite(4, 0) # switch off LED. Sets port 4 to 0 (0V, off)
sleep(0.05) # delay 0.05s
Display More
In:
Code
try:
while True:
if wiringpi.digitalRead(5): # If button on PIN 5 pressed
wiringpi.digitalWrite(0, 1) # switch on LED. Sets port 0 to 1 (3V3, on)
else:
wiringpi.digitalWrite(0, 0) # switch off LED. Sets port 0 to 0 (0V, off)
sleep(0.05) # delay 0.05s
umbenne, dann leuchten trotzdem alle LED's wenn ich das Skript starte und den Buzzer drücke.
Was ist der Fehler?