![]() |
Raspberry pi simpel coding - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Raspberry pi simpel coding (/thread-18556.html) |
Raspberry pi simpel coding - mike3001 - May-22-2019 Hallo phyton programmers :). i have some hard time getting this to working right. i have this Stopwatch witch is working well, but i would like to have so when pin(38) on my raspbeery pi model 3B+ gets high(3.3V/input) then will the StopWatch Start. and when pin(36) get High(3.3V/input) then it will stop the Stopwatch. is there a nice person there can help me with this. from tkinter import* import tkinter.font import RPi.GPIO as GPIO import time import os import os import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(38, GPIO.IN) from tkinter import * import time def Main(): global root root = Tk() root.title("Stopwatch") width = 600 height = 200 screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() x = (screen_width / 2) - (width / 2) y = (screen_height / 2) - (height / 2) root.geometry("%dx%d+%d+%d" % (width, height, x, y)) Top = Frame(root, width=600) Top.pack(side=TOP) stopWatch = StopWatch(root) stopWatch.pack(side=TOP) Bottom = Frame(root, width=600) Bottom.pack(side=BOTTOM) Start = Button(Bottom, text='Start', command=stopWatch.Start, width=10, height=2) Start.pack(side=LEFT) Stop = Button(Bottom, text='Stop', command=stopWatch.Stop, width=10, height=2) Stop.pack(side=LEFT) Reset = Button(Bottom, text='Reset', command=stopWatch.Reset, width=10, height=2) Reset.pack(side=LEFT) Exit = Button(Bottom, text='Close', command=stopWatch.Exit, width=10, height=2) Exit.pack(side=LEFT) Title = Label(Top, text="Stopwatch For --------", font=("arial", 18), fg="white", bg="black") Title.pack(fill=X) root.config(bg="black") root.mainloop() class StopWatch(Frame): def __init__(self, parent=None, **kw): Frame.__init__(self, parent, kw) self.startTime = 0.0 self.nextTime = 0.0 self.onRunning = 0 self.timestr = StringVar() self.MakeWidget() def MakeWidget(self): timeText = Label(self, textvariable=self.timestr, font=("times new roman", 50), fg="green", bg="black") self.SetTime(self.nextTime) timeText.pack(fill=X, expand=NO, pady=2, padx=2) def Updater(self): self.nextTime = time.time() - self.startTime self.SetTime(self.nextTime) self.timer = self.after(50, self.Updater) def SetTime(self, nextElap): minutes = int(nextElap / 60) seconds = int(nextElap - minutes * 60.0) miliSeconds = int((nextElap - minutes * 60.0 - seconds) * 100) self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, miliSeconds)) def Start(self): if not self.onRunning: self.startTime = time.time() - self.nextTime self.Updater() self.onRunning = 1 def Stop(self): if self.onRunning: self.after_cancel(self.timer) self.nextTime = time.time() - self.startTime self.SetTime(self.nextTime) self.onRunning = 0 def Exit(self): root.destroy() exit() def Reset(self): self.startTime = time.time() self.nextTime = 0.0 self.SetTime(self.nextTime) if __name__ == '__main__': Main() RE: Raspberry pi simpel coding - DeaD_EyE - May-22-2019 You could add this two methods to your class StopWatch .def cb_start(self, channel): self.Start() def cb_stop(self, channel): self.Stop()This two methods are called from outside by GPIO.add_event_detect .The code which should setup the inputs and set the event detection. def set_gpio(start_pin, stop_pin, start_cb, stop_cb): GPIO.setmode(GPIO.BOARD) GPIO.setup(start_pin, GPIO.IN) GPIO.setup(stop_pin, GPIO.IN) GPIO.add_event_detect(start_pin, GPIO.RISING, callback=start_cb, bouncetime=200) GPIO.add_event_detect(stop_pin, GPIO.RISING, callback=stop_cb, bouncetime=200)Then add to Main after creation of a StopWatch instance, the set_gpio function call:In Main(): stopWatch = StopWatch(root) set_gpio(38, 36, stopWatch.cb_start, stopWatch.cb_stop) stopWatch.pack(side=TOP) More info about the GPIO functions are here: https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/ RE: Raspberry pi simpel coding - mike3001 - May-22-2019 (May-22-2019, 12:42 PM)DeaD_EyE Wrote: You could add this two methods to your class Thank you, for youre respons :) i have tryed youre code, but it will stil not work for me :/ |