Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
raspi_lahendused [2014/05/15 13:25] raivo.sellraspi_lahendused [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 4: Line 4:
 === LED vilgutamine === === LED vilgutamine ===
  
-=== LED vilguks 1 sek. intervalliga ===+LED vilguks 1 sek. intervalliga
 <code c> <code c>
 import RPi.GPIO as GPIO import RPi.GPIO as GPIO
 import time import time
- 
 ledPin = 11 ledPin = 11
- 
 GPIO.setmode(GPIO.BOARD) GPIO.setmode(GPIO.BOARD)
 GPIO.setup(ledPin, GPIO.OUT) GPIO.setup(ledPin, GPIO.OUT)
- 
 while True: while True:
     time.sleep(1)         time.sleep(1)    
Line 21: Line 18:
 </code> </code>
  
-=== vastavalt kasutaja sisestatud väärtusele 0 või 1 juhitakse LED-i.=== +vastavalt kasutaja sisestatud väärtusele 0 või 1 juhitakse LED-i.  
 +<code c> 
 +import RPi.GPIO as GPIO 
 +ledPin 11 
 +GPIO.setmode(GPIO.BOARD) 
 +GPIO.setup(ledPin, GPIO.OUT) 
 +while True: 
 +    input_var input("Enter LED state: ") 
 +    if input_var == 1:         
 +    GPIO.output(ledPin, True) 
 +    elif input_var == 0:     
 +    GPIO.output(ledPin, False) 
 +    else: 
 + print "Unknown command!" 
 +</code>
  
-=== oleks graafiline kasutajaliides, millega saab LED sisse-välja lülitada=== +oleks graafiline kasutajaliides, millega saab LED sisse-välja lülitada 
 +<code c> 
 +import RPi.GPIO as GPIO 
 +ledPin 11 
 +GPIO.setmode(GPIO.BOARD) 
 +GPIO.setup(ledPin, GPIO.OUT) 
 +import Tkinter as tk 
 +class Application(tk.Frame): 
 +    def __init__(self, master=None): 
 +        tk.Frame.__init__(self, master) 
 +        self.pack() 
 +        self.createWidgets() 
 +    def createWidgets(self): 
 +        self.led_on tk.Button(self) 
 +        self.led_on["text"] = "ON" 
 +        self.led_on["command"] = self.turn_led_on 
 +        self.led_on.pack(side="top"
 + self.led_off = tk.Button(self) 
 +        self.led_off["text"] = "OFF" 
 +        self.led_off["command"] = self.turn_led_off 
 +        self.led_off.pack(side="top"
 +        self.QUIT = tk.Button(self, text="SULGE", fg="red",command=root.destroy) 
 +        self.QUIT.pack(side="bottom"
 +    def turn_led_on(self): 
 +        GPIO.output(ledPin, True) 
 +    def turn_led_off(self): 
 +        GPIO.output(ledPin, False) 
 +root = tk.Tk() 
 +app = Application(master=root) 
 +root.geometry("250x150+300+300"
 +app.mainloop() 
 +</code> 
 + 
 +=== Valvenäide === 
 +uus rida tuleb ainult oleku muutuse peale 
 +<code c> 
 +import RPi.GPIO as GPIO 
 +import time 
 +sensorPin = 7 
 +GPIO.setmode(GPIO.BOARD) 
 +GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
 +prevState = False 
 +currState = False 
 +while True: 
 +    time.sleep(0.1) 
 +    prevState = currState 
 +    currState = GPIO.input(sensorPin) 
 +    if currState != prevState: 
 +        newState = "HIGH" if currState else "LOW" 
 +        print "GPIO pin %s is %s" % (sensorPin, newState) 
 + 
 +</code> 
 + 
 +lisatakse reale kuupäev ja kellaaeg, ning tulemus kirjutatakse log-i faili. 
 + 
 +<code c> 
 +import RPi.GPIO as GPIO 
 +import time 
 +import datetime 
 +sensorPin = 7 
 +GPIO.setmode(GPIO.BOARD) 
 +GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
 +prevState = False 
 +currState = False 
 +f = open('andur.log', 'w',0) 
 +while True: 
 +    time.sleep(0.1) 
 +    prevState = currState 
 +    currState = GPIO.input(sensorPin) 
 +    if currState != prevState: 
 +        newState = "1" if currState else "0" 
 +        aeg=datetime.datetime.now() 
 +        print "%s, %s" % (aeg,newState) 
 +        #f.write(str(aeg)+","+newState+'\n'
 +        f.write(newState+'\n'
 + </code>      
raspi_lahendused.1400160333.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0