LED vilguks 1 sek. intervalliga
import RPi.GPIO as GPIO import time ledPin = 11 GPIO.setmode(GPIO.BOARD) GPIO.setup(ledPin, GPIO.OUT) while True: time.sleep(1) GPIO.output(ledPin, True) time.sleep(1) GPIO.output(ledPin, False)
vastavalt kasutaja sisestatud väärtusele 0 või 1 juhitakse LED-i.
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!"
oleks graafiline kasutajaliides, millega saab LED sisse-välja lülitada
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()
uus rida tuleb ainult oleku muutuse peale
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)
lisatakse reale kuupäev ja kellaaeg, ning tulemus kirjutatakse log-i faili.
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')