here is small sample block of code pieced together from the larger program:
Code: Select all
import gpiozero as gp import tkinter as tk import time from tkinter import ttk S4 = gp.Button(24) #Binding Sensor Ldr3 = gp.LightSensor(19) #Case Feeder Buzzer = gp.PWMOutputDevice(18, frequency=6000) Relay2 = gp.OutputDevice(6, initial_value=False, active_high=True) class Alarm (tk.LabelFrame): def __init__(self, master): tk.LabelFrame.__init__(self, master, text="Cautions") self.master = master #Alerts #Binding: This will be the same concept as the LDR's but will track how long the switch is inactive. So if switch is inactive for 1.5 seconds then event is triggered. binding_warning = ttk.LabelFrame(self, text="Binding") binding_warning.pack(side = "left") binding_label = tk.Button(binding_warning, text="Good", background="green", height=2, width=5) binding_label.pack(fill = "both") #Low Cases low_case_warning = ttk.LabelFrame(self, text="Bullets") low_case_warning.pack(side = "left") self.low_case_label = tk.Button(low_case_warning, command = self.Low_Cases_Reset, text="Good", background="green", height=2, width=5) self.low_case_label.pack(fill = "both") Ldr3.when_dark = Relay2.off Ldr3.when_light = self.Low_Cases def Low_Cases(self): #Doesnt work. Should turn the Relay on. If the relay is active for longer than a second then set alarm and trip button alarm visual Relay2.on if (Relay2.value == 1): time.sleep(1) Buzzer.pulse() self.low_case_label.configure(bg="red", text = "Alert") print("Low Cases") def Low_Cases_Reset(self): # This will silence the buzzer and change the button back to green. Buzzer.off() self.low_case_label.configure(bg="green", text = "Good") class App(tk.Tk): def __init__(self): super().__init__() self.title("AMMMO Computer") self.calls = Alarm(self) self.calls.pack() if __name__ == "__main__": app=App() app.mainloop()
Here is another sample of code i was using for a different alarm. I needed a time delay so false alarms would not be tripped. It was until I was trying to incorporate it into my code did I learn that that this code doesn't work like I thought it did. Though i could probably use the threshold value to prevent false alarms but i wanted a time function as an extra safe guard.
It was not until i started playing with it more in depth that I found first time through the timing would be right. After that it would just be instant alarm and changing the > value had no affect. Only restarting the script would make the first go around accurate again. So that means its merely capturing the present time active and not actively tracking it. Basically the code is reactive and not proactive. Most other time examples only show how to capture how longs something happened after the fact. Is there a way track time in real time or add a timer function that counts down tripping the additional events. I think I may need to research threading to get this to work, but would that interfere with GPIOZero's threaded call backs? Would I be better off dropping GPIOZero and going to RPI.GPIO to get the functionality i am looking for? I have only just briefly started looking at the RPI.GPIO documentation. I saw in RPI.GPIO i could tie mulitiply functions to the same edge event. Then I could have one event close the relay. Then i could have another even that counts down to trigger the second event that is interrupted if the relay is triggered open.
Code: Select all
startTime = time.time() while True: if ldr.value > .8: endTime = time.time() #LDR.value has to be above .8 for 3 seconds to sound alarm. This prevents false alarms during primer feeding. if (endTime - startTime > 3): sleep(.2) buzzer.pulse(n=1) print("Low Primers") else: sleep(.2) buzzer.off()
This is another sample code i tried. The idea was the relay would close when light and open when dark. Then it would sleep for the time while the relay was active and then set the alarm. It doesn't work though:
I am running the current Python 3 and current GPIOZERO libraries and I use VSCode for the coding. I am an experience analog system electrician but inexperienced coder. So far I have found that the answers are out there it just takes me a week to get the question right to find it. This problem though has me completely stumped. I am not sure of where to begin to solve it because more than once things didnt quite work how I though.
Code: Select all
Ldr3.when_dark = Relay2.off Ldr3.when_light = self.Low_Cases def Low_Cases(self): #Doesnt work. Relay2.on # if (Relay2.value == 1): time.sleep(2) Buzzer.pulse() self.low_case_label.configure(bg="red", text = "Alert") print("Low Cases")
I can post a link to full program if needed also.