• Tutorials

    Tutorials in Scratch, C und Python erleichtern dir den Einstieg und helfen dir bei der Umsetzung kreativer Ideen.

Joystick + Led

Programmiere Joystick, Taster und Led in Python

Wie verwende ich Tas­ter, Joy­stick und LEDs am Py­thon­board.

In diesem Tutorial lernst du die Verwendung des Tasters, Joysticks und die Ansteuerung der RGB-Leuchtdiode.

 

Blinken der LED (rot-grün-blau)

1 import sys
2 sys.path.append('/home/pi/raspbotics/Pythonboard_Module/RFID')
3 sys.path.append('/home/pi/raspbotics/Pythonboard_Module/LED_Matrix')
4 sys.path.append('/home/pi/raspbotics/Pythonboard_Module')
5 from RPi import GPIO
6 GPIO.setmode(GPIO.BCM)
7 import time
8  
9 LEDrot = 17
10 LEDgruen = 22
11 LEDblau = 24
12  
13 GPIO.setup(LEDrot,GPIO.OUT)
14 GPIO.setup(LEDgruen,GPIO.OUT)
15 GPIO.setup(LEDblau,GPIO.OUT)
14  
15 GPIO.output(LEDrot,0)
16 GPIO.output(LEDgruen,0)
17 GPIO.output(LEDblau,0)
18  
19 try:
20     while 1:
21         GPIO.output(LEDblau,1)
22         time.sleep(1)
23         GPIO.output(LEDblau,0)
24         GPIO.output(LEDrot,1)
25         time.sleep(1)
26         GPIO.output(LEDrot,0)
27         GPIO.output(LEDgruen,1)
28         time.sleep(1)
29         GPIO.output(LEDgruen,0)
30 except KeyboardInterrupt:
31 GPIO.cleanup()

 

Taster + Joystick (Ausgabe im Terminal)

1 import sys
2 sys.path.append('/home/pi/raspbotics/Pythonboard_Module/RFID')
3 sys.path.append('/home/pi/raspbotics/Pythonboard_Module/LED_Matrix')
4 sys.path.append('/home/pi/raspbotics/Pythonboard_Module')
5 from RPi import GPIO
6 GPIO.setmode(GPIO.BCM)
7 import time
8  
9 Joy_left = 4
10 Joy_right = 23
11 Joy_up = 19
12 Joy_down = 13
13 Joy_push = 6
14 Taster = 5
15  
16 GPIO.setup(Joy_left,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
17 GPIO.setup(Joy_right,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
18 GPIO.setup(Joy_up,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
19 GPIO.setup(Joy_down,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
20 GPIO.setup(Joy_push,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
21 GPIO.setup(Taster,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
22  
23 try:
24      while 1:
25          if GPIO.input(Joy_left)==1:
26              print "Joystick left"
27          if GPIO.input(Joy_right)==1:
28              print "Joystick right"
29          if GPIO.input(Joy_up)==1:
30              print "Joystick up"
31          if GPIO.input(Joy_down)==1:
32              print "Joystick down"
33          if GPIO.input(Joy_push)==1:
34              print "Joystick push"
35          if GPIO.input(Taster)==1:
36              print "Taster"
37 time.sleep(0.3)
38  
39 except KeyboardInterrupt:
40 GPIO.cleanup()

 

Workshops

PTS Workshop Mechatronik
Coding Mikrocontroller + Elektronik

PTS Schopenhauerstrasse, 24. + 25. Juni 2019

Kontakt

Raspbotics

  • Claus Zöchling
  • +436504291944
  • info[at]raspbotics.at
  • Facebook

Download

latest Image

Stand: 02.06.2019

ältere Images