This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| raspi [2014/05/15 10:11] – raivo.sell | raspi [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ==== Raspberry Pi näitekoodid ==== | ==== Raspberry Pi näitekoodid ==== | ||
| + | http:// | ||
| === Lihtsad näited Python shell === | === Lihtsad näited Python shell === | ||
| Line 6: | Line 7: | ||
| <code c> | <code c> | ||
| 5+9 | 5+9 | ||
| - | + | print ("Tere Kevad") | |
| - | print („Tere Kevad”) | + | |
| 56/ | 56/ | ||
| - | + | print ("Tere Kevad") | |
| - | print („Tere Kevad”) | + | |
| 3 > 7 | 3 > 7 | ||
| - | |||
| for x in range (1,10): | for x in range (1,10): | ||
| print (x) | print (x) | ||
| Line 24: | Line 20: | ||
| <code c> | <code c> | ||
| + | # -*- coding: utf-8 -*- | ||
| kaal = float(input(" | kaal = float(input(" | ||
| if kaal > 100: | if kaal > 100: | ||
| Line 34: | Line 30: | ||
| </ | </ | ||
| + | Muutke koodi nii, et: | ||
| + | - võrreldakse näitest erinevaid suurusi | ||
| + | - vastavalt sisendile soovitatakse kindel kaalu langetuse/ | ||
| + | |||
| + | === Python graafika === | ||
| + | <code c> | ||
| + | |||
| + | import turtle | ||
| + | for i in range(5): | ||
| + | turtle.forward(50) | ||
| + | turtle.right(144) | ||
| + | import turtle | ||
| + | turtle.pencolor("# | ||
| + | for i in range(50): | ||
| + | turtle.forward(50) | ||
| + | turtle.right(123) | ||
| + | turtle.pencolor("# | ||
| + | </ | ||
| + | |||
| + | Muutke koodi nii, et: | ||
| + | - kujundi värv oleks punane | ||
| + | - kujund oleks suurem | ||
| + | - tulemus oleks mõne teise kujuga | ||
| === Python graafiline kasutajaliides === | === Python graafiline kasutajaliides === | ||
| <code c> | <code c> | ||
| - | import | + | import |
| class Application(tk.Frame): | class Application(tk.Frame): | ||
| def __init__(self, | def __init__(self, | ||
| Line 45: | Line 63: | ||
| self.pack() | self.pack() | ||
| self.createWidgets() | self.createWidgets() | ||
| - | |||
| def createWidgets(self): | def createWidgets(self): | ||
| self.hi_there = tk.Button(self) | self.hi_there = tk.Button(self) | ||
| Line 51: | Line 68: | ||
| self.hi_there[" | self.hi_there[" | ||
| self.hi_there.pack(side=" | self.hi_there.pack(side=" | ||
| - | |||
| self.QUIT = tk.Button(self, | self.QUIT = tk.Button(self, | ||
| self.QUIT.pack(side=" | self.QUIT.pack(side=" | ||
| - | |||
| def say_hi(self): | def say_hi(self): | ||
| print(" | print(" | ||
| - | |||
| root = tk.Tk() | root = tk.Tk() | ||
| app = Application(master=root) | app = Application(master=root) | ||
| Line 64: | Line 78: | ||
| </ | </ | ||
| + | === LED vilgutamine === | ||
| + | <code c> | ||
| + | import RPi.GPIO as GPIO | ||
| + | import time | ||
| + | ledPin = 11 | ||
| + | GPIO.setmode(GPIO.BOARD) | ||
| + | GPIO.setup(ledPin, | ||
| + | while True: | ||
| + | GPIO.output(ledPin, | ||
| + | </ | ||
| + | |||
| + | Muutke koodi nii, et: | ||
| + | - LED vilguks 1 sek. intervalliga | ||
| + | - vastavalt kasutaja sisestatud väärtusele 0 või 1 juhitakse LED-i. | ||
| + | - oleks graafiline kasutajaliides, | ||
| + | === Valvenäide === | ||
| + | <code c> | ||
| import RPi.GPIO as GPIO | import RPi.GPIO as GPIO | ||
| import time | import time | ||
| - | import datetime | ||
| - | |||
| sensorPin = 7 | sensorPin = 7 | ||
| - | |||
| GPIO.setmode(GPIO.BOARD) | GPIO.setmode(GPIO.BOARD) | ||
| GPIO.setup(sensorPin, | GPIO.setup(sensorPin, | ||
| + | while True: | ||
| + | time.sleep(0.1) | ||
| + | print GPIO.input(sensorPin) | ||
| - | prevState = False | + | </ |
| - | currState = False | + | Muutke koodi nii, et: |
| - | + | - uus rida tuleb ainult oleku muutuse peale | |
| - | f = open(' | + | - lisatakse reale kuupäev ja kellaaeg, ning tulemus kirjutatakse log-i faili. |
| - | + | - tulemused näidatakse reaalajas graafiliselt | |
| - | + | ||
| - | while True: | + | |
| - | | + | |
| - | | + | |
| - | currState = GPIO.input(sensorPin) | + | |
| - | if currState != prevState: | + | |
| - | newState = " | + | |
| - | aeg=datetime.datetime.now() | + | |
| - | print "%s, %s" % (aeg, | + | |
| - | #f.write(str(aeg)+"," | + | |
| - | | + | |
| + | https:// | ||