User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Mon Oct 22, 2018 6:02 am

That function looks ok. what do you mean by 'when I blow air in flow meter'? In the code the clean valve opens *after* the solenoid valve has closed. There is no facility to manually blow air.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Mon Oct 22, 2018 3:02 pm

paddyg wrote:
Mon Oct 22, 2018 6:02 am
That function looks ok. what do you mean by 'when I blow air in flow meter'? In the code the clean valve opens *after* the solenoid valve has closed. There is no facility to manually blow air.
When i press pour Button(18) solenoid LED(17) turned on and wait for flow meter Button(23) to measure amount ( like 30 ML) and send pulse signal to stop solenoid than turn on clean LED(25) which is working fine but when i add sleep(TIME_OUT) function which we added to stop solenoid just in case if flowmeter failed to send pulses to solenoid than solenoid will stop according to time_out time.

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Mon Oct 22, 2018 3:54 pm

That's very odd. It sounds like the start_pour function and count_pulse function are conflicting in some way over setting the global variables. Things like that are a reason not to use globals but that's rather hard with gpiozero! Apart from doing without the timeout the only way I can think to fix it would be to have a threaded function looping that switched off the solenoid. That way you could avoid the sleep() inside the callback function. Along the lines of

Code: Select all

import threading
import time
...
def kill_loop():
    global kill_time
    while True:
        if time.time() > kill_time:
            solenoid.off()
        time.sleep(1.0)

kill_time = 0.0
t = threading.Thread(target=kill_loop)
t.daemon = True
t.start()
...
def start_pour(amount):
    global count, count_cut_off, kill_time
    count_cut_off = amount
    count = 0
    solenoid.on()
    kill_time = time.time() + TIME_OUT
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Tue Nov 06, 2018 1:47 am

paddyg wrote:
Mon Oct 22, 2018 3:54 pm
That's very odd. It sounds like the start_pour function and count_pulse function are conflicting in some way over setting the global variables. Things like that are a reason not to use globals but that's rather hard with gpiozero! Apart from doing without the timeout the only way I can think to fix it would be to have a threaded function looping that switched off the solenoid. That way you could avoid the sleep() inside the callback function. Along the lines of

Code: Select all

import threading
import time
...
def kill_loop():
    global kill_time
    while True:
        if time.time() > kill_time:
            solenoid.off()
        time.sleep(1.0)

kill_time = 0.0
t = threading.Thread(target=kill_loop)
t.daemon = True
t.start()
...
def start_pour(amount):
    global count, count_cut_off, kill_time
    count_cut_off = amount
    count = 0
    solenoid.on()
    kill_time = time.time() + TIME_OUT
Hi Paddy,
Sorry for late update i was waiting for new flow meter and other parts. i checked everything is working fine so far just could not find why cleaning output led is flickering.
What would be easy way to convert buttons in gui mode so i can use GUI based buttons on touch display screen ?

Thank You

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Tue Nov 06, 2018 3:15 pm

It depends crucially on what kind of touchscreen you are using. But if it behaves like a normal display and mouse click then you could use tkinter and make the screen buttons call the same functions you called with the gpio buttons. Along the lines of:

Code: Select all

import tkinter as tk
...
root = tk.Tk()
root.wm_title('Measure by @onlinegill')

button_frame = tk.Frame(root)
button_frame.pack(fill=tk.X, side=tk.BOTTOM)

small_button = tk.Button(button_frame, text='SMALL', command=pour_small)
medium_button = tk.Button(button_frame, text='MEDIUM', command=pour_medium)
big_button = tk.Button(button_frame, text='BIG', command=pour_big)

button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)

small_button.grid(row=0, column=0, sticky=tk.W+tk.E)
medium_button.grid(row=0, column=1, sticky=tk.W+tk.E)
big_button.grid(row=0, column=2, sticky=tk.W+tk.E)

root.mainloop() # do this instead of the pause() used in the gpiozero code
The advantage of this is that it should work in parallel with the gpio buttons.

Did you get the kill_time in background thread working?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Tue Nov 06, 2018 10:04 pm

paddyg wrote:
Tue Nov 06, 2018 3:15 pm
It depends crucially on what kind of touchscreen you are using. But if it behaves like a normal display and mouse click then you could use tkinter and make the screen buttons call the same functions you called with the gpio buttons. Along the lines of:

Code: Select all

import tkinter as tk
...
root = tk.Tk()
root.wm_title('Measure by @onlinegill')

button_frame = tk.Frame(root)
button_frame.pack(fill=tk.X, side=tk.BOTTOM)

small_button = tk.Button(button_frame, text='SMALL', command=pour_small)
medium_button = tk.Button(button_frame, text='MEDIUM', command=pour_medium)
big_button = tk.Button(button_frame, text='BIG', command=pour_big)

button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)

small_button.grid(row=0, column=0, sticky=tk.W+tk.E)
medium_button.grid(row=0, column=1, sticky=tk.W+tk.E)
big_button.grid(row=0, column=2, sticky=tk.W+tk.E)

root.mainloop() # do this instead of the pause() used in the gpiozero code
The advantage of this is that it should work in parallel with the gpio buttons.

Did you get the kill_time in background thread working?
Paddy,
Yes Kill_time thread working thank you !! however when i put tk codes in same file i get error

Code: Select all

python3 newsysbackup.py
Traceback (most recent call last):
  File "newsysbackup.py", line 14, in <module>
    small_button = tk.Button(button_frame, text='SMALL', command=pour_small)
NameError: name 'pour_small' is not defined


User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Tue Nov 06, 2018 11:09 pm

Hmm, tricky to tell in isolation but the fact that your error says 'line 14' makes me think that you put in the tk stuff at the start of the file. At that point the functions pour_small() etc won't have been defined. I would put all the tk code together just after the gpiozero button definitions. Let me know how you get on.

Paddy

PS for your finished code you will probably want graphical buttons http://effbot.org/tkinterbook/button.htm
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Thu Nov 08, 2018 5:05 am

Paddy,
i made it working by adding tk codes all the way down and removing pause.
so far i am using hdmi monitor and buttons are working with mouse.
i am planing to use small touch screen which could be touched by finger without showing mouse pointer.
may be 3.5 or 7 inch what you think which one would good to just start up?
gui will launch in full screen when pi will boot so far i know how to make it in full screen :p so it will work as kiosk mode.
how can i make this gui more colorfull ? such as replace buttons with png button file? also replace wondow background ? and other gray out / disable other buttons while selected pour is pouring ?
for example i am attaching png button file

Thank You
Attachments
970A5BD3-7EAB-4789-AFDC-1D6B66425D5F.gif
970A5BD3-7EAB-4789-AFDC-1D6B66425D5F.gif (3.24 KiB) Viewed 3964 times

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Thu Nov 08, 2018 11:54 pm

Code: Select all

poursmall = tk.PhotoImage(file="textures/970A5BD3-7EAB-4789-AFDC-1D6B66425D5F.gif")
small_button = tk.Button(button_frame, image=poursmall, command=pour_small)
# etc
# in the pour_small function you would do
# medium_button.configure(image=mediumpour_grey)
# etc, and reverse these changes when your pulse counting function reaches the total
but no substitute for reading the docs and searching for examples on line.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Mon Nov 12, 2018 4:01 am

Its working great !!!
is there anyway i can call pour_small buttons etc from html page with php ? so it will easy to design.
i am able to turn on led with php but don’t how to call function while .py script is running.

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sat Nov 17, 2018 12:43 am

paddyg wrote:
Thu Nov 08, 2018 11:54 pm

Code: Select all

poursmall = tk.PhotoImage(file="textures/970A5BD3-7EAB-4789-AFDC-1D6B66425D5F.gif")
small_button = tk.Button(button_frame, image=poursmall, command=pour_small)
# etc
# in the pour_small function you would do
# medium_button.configure(image=mediumpour_grey)
# etc, and reverse these changes when your pulse counting function reaches the total
but no substitute for reading the docs and searching for examples on line.
Hi Paddy,
I try to connect relays with external supply and still GPIO (clean Led 25 ) relay is flickering.
when pour stop pouring it turn on clean solenoid but relay start flickering like click click.... do you think it could be something in code which could be making this issue ? also it does not sleep according to set time it keep flickering for 5 seconds even if you set 1 second.
also is it possible to run these 3 pour from server side such as flask?
thank You so much for your help

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Wed Nov 21, 2018 6:02 pm

Update regarding clean mode relay or solenoid fluctuation.
i removed gas line which is hooked before flow meter (clean ) and instead of clean solenoid i connected my wires to LED and my led stay solid and time is accurate that mean it turn off on time. now question is how can i stop flow meter sending more pulses when there is clean mode on ?

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Wed Nov 21, 2018 11:29 pm

I've lost track of what your current code is and the full wiring diagram.(if I ever knew it) so it might be worth posting a refresher.

I would suggest using a python Web server such as Flask rather than introducing another language such as php (especially as it's horrible) but a non trivial amount of learning to add Web interface
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Thu Nov 22, 2018 2:28 am

paddyg wrote:
Wed Nov 21, 2018 11:29 pm
I've lost track of what your current code is and the full wiring diagram.(if I ever knew it) so it might be worth posting a refresher.

I would suggest using a python Web server such as Flask rather than introducing another language such as php (especially as it's horrible) but a non trivial amount of learning to add Web interface
Paddy,
Basically everything working the way it should be only problem is that when flowmeter send pulses to solenoid = LED(17) it stops and right away clean solenoid clean = LED(25) turn on but due to clean solenoid send air to clean flow meter and tap flow sensor keep sending pulses and clean solenoid is flickering. if i remove air from clean mode than clean solenoid is working fine.
so that mean flowmeter keep sending pulses due to clean solenoid keep spinning it.

i contacted someone who said

Looking briefly at your code, my guess is that your count_pulse method is firing after you intended for it to stop, and count is greater than count_cut_off, so it repeatedly runs:

Code: Select all

     solenoid.off()
        clean.on() # run flow meter cleaning solenoid with food grade gas
        sleep(GAS_PAUSE) #keep clean mode for 2 second
        clean.off() # turn off clean mode enjoy drink
here is my final code and i am attaching fritzing diagram which i tried to create with basic parts.

Code: Select all

from gpiozero import Button, LED
from time import sleep
import threading
import time
pulse = Button(23)
button_small = Button(18) # first button to  pour small
button_medium = Button(21) # 2nd  button to pour medium
button_big = Button(22) # 3rd  button to pour big
solenoid = LED(17)
clean = LED(25)

GAS_PAUSE = 2.0
TIME_OUT = 10.0

MEASURE_SMALL = 80
MEASURE_MEDIUM = 130
MEASURE_BIG = 200

def pour_small():
    start_pour(MEASURE_SMALL)

def pour_medium():
    start_pour(MEASURE_MEDIUM)

def pour_big():
    start_pour(MEASURE_BIG)

def kill_loop():
    global kill_time
    while True:
        if time.time() > kill_time:
            solenoid.off()
        time.sleep(1.0)
kill_time = 0.0
t = threading.Thread(target=kill_loop)
t.daemon = True
t.start()
def start_pour(amount):
    global count, count_cut_off, kill_time
    count_cut_off = amount
    count = 0
    solenoid.on()
    kill_time = time.time() + TIME_OUT
def count_pulse():
    global count, count_cut_off
    count  += 1
    if count >= count_cut_off:
        solenoid.off()
        clean.on() # run flow meter cleaning solenoid with food grade gas
        sleep(GAS_PAUSE) #keep clean mode for 2 second
        clean.off() # turn off clean mode enjoy drink


pulse.when_released = count_pulse
button_small.when_pressed = pour_small
button_medium.when_pressed = pour_medium
button_big.when_pressed = pour_big

import tkinter as tk
root = tk.Tk()
root.wm_title('By onlinegill')
button_frame = tk.Frame(root)

button_frame.pack(fill=tk.X, side=tk.BOTTOM)

small_button = tk.Button(button_frame, text='Pour Small', command=pour_small)
medium_button = tk.Button(button_frame, text='Pour Medium', command=pour_medium)
big_button = tk.Button(button_frame, text='Pour Full Glass', command=pour_big)

button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)

small_button.grid(row=0, column=0, sticky=tk.W+tk.E)
medium_button.grid(row=0, column=1, sticky=tk.W+tk.E)
big_button.grid(row=0, column=2, sticky=tk.W+tk.E)

root.mainloop() # do this instead of the pause() used in the gpiozero code


https://drive.google.com/file/d/1F7vlaI ... sp=sharing
Last edited by onlinegill on Wed Dec 05, 2018 10:06 pm, edited 1 time in total.

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Thu Nov 22, 2018 8:27 am

Now I look at it again I see that there is a sleep inside the count_pulse function and no way to stop it rerunning. It probably needs an extra flag to stop the clean restarting and an additional clean kill time.

I'm away at the moment but will be back in a day or so and will send suggestion.

Paddy
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Fri Nov 23, 2018 11:05 pm

Hi, back now. Something like this.. (Not checked but you will be able to see the type of changes I made - very similar to the previous one)

Code: Select all

from gpiozero import Button, LED
import threading
import time

pulse = Button(23)
button_small = Button(18) # first button to  pour small
button_medium = Button(21) # 2nd  button to pour medium
button_big = Button(22) # 3rd  button to pour big
solenoid = LED(17)
clean = LED(25)

GAS_PAUSE = 2.0
TIME_OUT = 10.0

MEASURE_SMALL = 80
MEASURE_MEDIUM = 130
MEASURE_BIG = 200

def pour_small():
    start_pour(MEASURE_SMALL)

def pour_medium():
    start_pour(MEASURE_MEDIUM)

def pour_big():
    start_pour(MEASURE_BIG)

def kill_loop():
    global kill_time
    while True:
        tm = time.time()
        if tm > kill_time:
            solenoid.off()
        if tm > gas_kill_time:
            clean.off()
        time.sleep(1.0)

kill_time = 0.0
gas_kill_time = 0.0
cleaned = True
t = threading.Thread(target=kill_loop)
t.daemon = True
t.start()

def start_pour(amount):
    global count, count_cut_off, kill_time
    count_cut_off = amount
    count = 0
    solenoid.on()
    kill_time = time.time() + TIME_OUT
    cleaned = False # so cleaning starts after count_cut_off

def count_pulse():
    global count, count_cut_off
    count  += 1
    if count >= count_cut_off:
        solenoid.off()
        if cleaned = False:
            clean.on() # run flow meter cleaning solenoid with food grade gas
            gas_kill_time = time.time() + GAS_PAUSE
            cleaned = True # set it True now to stop it keeping or restarting!

pulse.when_released = count_pulse
button_small.when_pressed = pour_small
button_medium.when_pressed = pour_medium
button_big.when_pressed = pour_big

import tkinter as tk
root = tk.Tk()
root.wm_title('By onlinegill')
button_frame = tk.Frame(root)

button_frame.pack(fill=tk.X, side=tk.BOTTOM)

small_button = tk.Button(button_frame, text='Pour Small', command=pour_small)
medium_button = tk.Button(button_frame, text='Pour Medium', command=pour_medium)
big_button = tk.Button(button_frame, text='Pour Full Glass', command=pour_big)

button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)

small_button.grid(row=0, column=0, sticky=tk.W+tk.E)
medium_button.grid(row=0, column=1, sticky=tk.W+tk.E)
big_button.grid(row=0, column=2, sticky=tk.W+tk.E)

root.mainloop() # do this instead of the pause() used in the gpiozero code
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 4:25 am

paddyg wrote:
Fri Nov 23, 2018 11:05 pm
Hi, back now. Something like this.. (Not checked but you will be able to see the type of changes I made - very similar to the previous one)

Code: Select all

from gpiozero import Button, LED
import threading
import time

pulse = Button(23)
button_small = Button(18) # first button to  pour small
button_medium = Button(21) # 2nd  button to pour medium
button_big = Button(22) # 3rd  button to pour big
solenoid = LED(17)
clean = LED(25)

GAS_PAUSE = 2.0
TIME_OUT = 10.0

MEASURE_SMALL = 80
MEASURE_MEDIUM = 130
MEASURE_BIG = 200

def pour_small():
    start_pour(MEASURE_SMALL)

def pour_medium():
    start_pour(MEASURE_MEDIUM)

def pour_big():
    start_pour(MEASURE_BIG)

def kill_loop():
    global kill_time
    while True:
        tm = time.time()
        if tm > kill_time:
            solenoid.off()
        if tm > gas_kill_time:
            clean.off()
        time.sleep(1.0)

kill_time = 0.0
gas_kill_time = 0.0
cleaned = True
t = threading.Thread(target=kill_loop)
t.daemon = True
t.start()

def start_pour(amount):
    global count, count_cut_off, kill_time
    count_cut_off = amount
    count = 0
    solenoid.on()
    kill_time = time.time() + TIME_OUT
    cleaned = False # so cleaning starts after count_cut_off

def count_pulse():
    global count, count_cut_off
    count  += 1
    if count >= count_cut_off:
        solenoid.off()
        if cleaned = False:
            clean.on() # run flow meter cleaning solenoid with food grade gas
            gas_kill_time = time.time() + GAS_PAUSE
            cleaned = True # set it True now to stop it keeping or restarting!

pulse.when_released = count_pulse
button_small.when_pressed = pour_small
button_medium.when_pressed = pour_medium
button_big.when_pressed = pour_big

import tkinter as tk
root = tk.Tk()
root.wm_title('By onlinegill')
button_frame = tk.Frame(root)

button_frame.pack(fill=tk.X, side=tk.BOTTOM)

small_button = tk.Button(button_frame, text='Pour Small', command=pour_small)
medium_button = tk.Button(button_frame, text='Pour Medium', command=pour_medium)
big_button = tk.Button(button_frame, text='Pour Full Glass', command=pour_big)

button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)

small_button.grid(row=0, column=0, sticky=tk.W+tk.E)
medium_button.grid(row=0, column=1, sticky=tk.W+tk.E)
big_button.grid(row=0, column=2, sticky=tk.W+tk.E)

root.mainloop() # do this instead of the pause() used in the gpiozero code
Hi Paddy,
Thank you so much i was getting following error when i run the codes you post.

Code: Select all

Python 3.5.3 (/usr/bin/python3)
>>> %Run test1.py
  File "/home/pi/Desktop/test1.py", line 58
    if cleaned = False:
               ^
SyntaxError: invalid syntax

so i replaced if cleaned = False: to if cleaned > False: i dont know if did right.
than i was able to run script but when i press button 18 and flowmeter start spinning it gives me following error and after turning off dolenoid it does not turn on clean.

Code: Select all

>>> %Run test1.py
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if cleaned > False:
UnboundLocalError: local variable 'cleaned' referenced before assignment
KeyboardInterrupt: Forced reset
>>> 

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 8:36 am

Doh, my rookie error!! It should have obviously been
if not cleaned:
But it is good practice for you to debug that type of thing
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 7:17 pm

paddyg wrote:
Sat Nov 24, 2018 8:36 am
Doh, my rookie error!! It should have obviously been
if not cleaned:
But it is good practice for you to debug that type of thing
Paddy,
I replaced as you suggest

Code: Select all

if count >= count_cut_off:
        solenoid.off()
        if not cleaned:
but after flowmeter stop solenoid, clean does not turn on and i am getting following error along with couple lines
i am pulling my hair.

Code: Select all

UnboundLocalError: local variable 'cleaned' referenced before assignment

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 327, in _fire_events
    self._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 360, in _fire_deactivated
    super(HoldMixin, self)._fire_deactivated()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 306, in _fire_deactivated
    self.when_deactivated()
  File "/home/pi/Desktop/test1.py", line 58, in count_pulse
    if not cleaned:
UnboundLocalError: local variable 'cleaned' referenced before assignment

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 7:34 pm

Update to my previous post i added cleaned = False inside def count_pulse(): and it clean start working but its fluctuating
please let me know if i did right

Code: Select all

def count_pulse():
    global count, count_cut_off
    count  += 1
    if count >= count_cut_off:
        solenoid.off()
        cleaned = False 
        if not cleaned:
            clean.on() # run flow meter cleaning solenoid with food grade gas
            gas_kill_time = time.time() + GAS_PAUSE
            cleaned = True # set 

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 7:42 pm

Ah yes, obviously.. you need to add 'cleaned' to the global lines in start_pour() and count_pulse(). Similarly you will need to add 'gas_kill_time' to the globals used in kill_loop() and count_pulse()

Sorry about that.

PS and also obviously, now you can see what was causing the error, the last thing you want to do is introduce a local variable called 'cleaned' to count_pulse() especially if you set it to False. The code you posted is equivalent to just taking out the if not cleaned test!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 7:47 pm

paddyg wrote:
Sat Nov 24, 2018 7:42 pm
Ah yes, obviously.. you need to add 'cleaned' to the global lines in start_pour() and count_pulse(). Similarly you will need to add 'gas_kill_time' to the globals used in kill_loop() and count_pulse()

Sorry about that.

PS and also obviously, now you can see what was causing the error, the last thing you want to do is introduce a local variable called 'cleaned' to count_pulse() especially if you set it to False. The code you posted is equivalent to just taking out the if not cleaned test!
Paddy ,
Could you please give me an example about it ?sorry not good in python.

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 7:52 pm

This kind of thing (also applied to other global variables and functions mentioned in my last post)

Code: Select all

def count_pulse():
    global count, count_cut_off, cleaned, gas_kill_time
    count  += 1
    if count >= count_cut_off:
      ...
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 8:08 pm

Got it thank you i will update test result monday. as i do not have relays at home.
You are life saver
Could you please let me know how can i call buttons like pour_small with command line or flask or node red ?
would like to make web interface. i tried calling python3 test.py pour_small or python3 test.py button_small it open tk window but does not turn on solenoid :idea:

User avatar
paddyg
Posts: 2591
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: stop solenoid valve with flow meter pulse

Sat Nov 24, 2018 9:02 pm

Matt Richardson did a good Fask intro here http://mattrichardson.com/Raspberry-Pi-Flask/ and I've seen similar things with code red that you will be able to find with a bit of googling. I have to warn you that if you are new to programming then the asynchronous client/server concepts will be pretty tricky. I would have thought your application would require a human close to the action (holding the glass) and didn't lend itself to a web interface, why do you want to use it?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”