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

stop solenoid valve with flow meter pulse

Mon Oct 15, 2018 8:10 pm

Hi,
i am using fowling codes ( found on internet ) to read flow meter pulses. what i would like to do that when flow meter read 24 pulses it will stop solenoid valve on pin 17. and keep running in loop ( reset count / pulses to 0 ).
Project making wine dispenser.

push button to turn on solenoid valve (to run pour with food grade gas )
liquor will come from bottle and go through flowmeter to glass.
flowmeter send pulses to rasberry pi.
when pulses are like 24 etc it will stop solenoid valve.
flowmeter pulse will reset to 0 for next pour 24 pulse ( 30ML) according to my flowmeter.
keep in loop.

Code: Select all

import RPi.GPIO as GPIO
import time, sys

FLOW_SENSOR = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down = GPIO.PUD_UP)

global count
count = 0

def countPulse(channel):
   global count
   count = count+1
   print(count);
GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse)

while True:
    try:
        time.sleep(1)
    except KeyboardInterrupt:
        print('\ncaught keyboard interrupt!, bye')
        GPIO.cleanup()
        sys.exit()

  • solenoid attached to pin 17.
    push button attached to pin 18
    flowmeter attached to pin 23.
Thank you

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

Re: stop solenoid valve with flow meter pulse

Mon Oct 15, 2018 8:18 pm

Hi, I would use the https://gpiozero.readthedocs.io/en/stable/recipes.html module it breaks each 'action' out into a function like you've done with your pulse meter. Have a look at that and post again if you get stuck or something doesn't make sense.

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

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

Re: stop solenoid valve with flow meter pulse

Mon Oct 15, 2018 8:27 pm

paddyg wrote:
Mon Oct 15, 2018 8:18 pm
Hi, I would use the https://gpiozero.readthedocs.io/en/stable/recipes.html module it breaks each 'action' out into a function like you've done with your pulse meter. Have a look at that and post again if you get stuck or something doesn't make sense.

Paddy
Hi i was reading that examples and i made something with timer based but could not find anything related reading flowmeter pulses.
like
if pulses 30
stop solenoid
here is my timmer based code which are working fine but dont know how to combine with flowmeter pulse system




Code: Select all

import  RPi.GPIO as GPIO
import time
from gpiozero import LED, Button
from time import sleep

output = LED(17)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_UP)


while True:
    inputValue = GPIO.input(18)
    if (inputValue == False):
        output.on()
        sleep(5)
        output.off()
        

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

Re: stop solenoid valve with flow meter pulse

Mon Oct 15, 2018 10:26 pm

well you might find a better example among all the recipes in the docs but I would do something along the lines of this (untested), using LED as generic output and Button for input, may be more appropriate ones (i.e. digitalInputDevice, digitalOutputDevice)

Code: Select all

from gpiozero import Button, LED
from signal import pause
MEASURE_COUNT = 30

pulse = Button(23)
button = Button(18)
solenoid = LED(17)

def count_pulse():
    global count
    count  += 1
    if count >= MEASURE_COUNT:
        solenoid.off()

def start_pour():
    global count
    solenoid.on()
    count = 0
 
 pulse.when_released = count_pulse
 button.when_pressed = start_pour
 
 count = 0
 
 pause()
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Mon Oct 15, 2018 11:24 pm

paddyg wrote:
Mon Oct 15, 2018 10:26 pm
well you might find a better example among all the recipes in the docs but I would do something along the lines of this (untested), using LED as generic output and Button for input, may be more appropriate ones (i.e. digitalInputDevice, digitalOutputDevice)

Code: Select all

from gpiozero import Button, LED
from signal import pause
MEASURE_COUNT = 30

pulse = Button(23)
button = Button(18)
solenoid = LED(17)

def count_pulse():
    global count
    count  += 1
    if count >= MEASURE_COUNT:
        solenoid.off()

def start_pour():
    global count
    solenoid.on()
    count = 0
 
 pulse.when_released = count_pulse
 button.when_pressed = start_pour
 
 count = 0
 
 pause()

Hi, thank you for your time to write codes and help. when i try to run codes it gives me following error. i have no knowledge of python but little understanding how it works.

Code: Select all

 python autoflow.py
  File "autoflow.py", line 19
    pulse.when_released = count_pulse
                                    ^
IndentationError: unindent does not match any outer indentation level

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

Re: stop solenoid valve with flow meter pulse

Mon Oct 15, 2018 11:37 pm

Hi,
I removed spaces and its working fine. thank you so much!!
can i add more buttons ? like if i want to add more two pours ?
first button with 30 pulses.
2nd with 60 pulses.
3rd with full glass.

also turn solenoid.off() after couple seconds ( like 50 ) just in case wine does not pour ( empty bottle or straw get clogged ) or flowmeter failed as it wont stop until it read pules from flowmeter.

i added one more solenoid for cleaning flowmeter with second air solenoid when pour is completed.but it has delay instead of turning off in 2 sec it take like 5 seconds

Code: Select all

if count >= MEASURE_COUNT:
         solenoid.off()
     wait for 15 secconds if no pulse counted after button = Button(18) is pressed turn off solenoid = LED(17)
modified code

Code: Select all

from gpiozero import Button, LED
from signal import pause
MEASURE_COUNT = 30
from time import sleep
pulse = Button(23)
button = Button(18)
solenoid = LED(17)
clean= LED(25)
def count_pulse():
    global count
    count  += 1
    if count >= MEASURE_COUNT:
         solenoid.off()
         clean.on() # run flow meter cleaning solenoid with food grade gas
         sleep(2) #keep clean mode for 2 second
         clean.off() # turn of clean mode enjoy drink
         
def start_pour():
    global count
    solenoid.on()
    count = 0
pulse.when_released = count_pulse
button.when_pressed = start_pour
count = 0
pause()


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

Re: stop solenoid valve with flow meter pulse

Tue Oct 16, 2018 1:23 pm

Hi, I glad (and maybe slightly surprised) it worked with only minor indent mismatch! For your own sanity you should get used to putting all your imports at the top (but after the `#!/usr/bin/env python3`) then globals, constants etc. If you stick to a system it will make debugging easier; sensible to stick to the PEP8 guide.

No reason not to include other buttons. You would have to have another function and set the number of pulses to count. gpiozero doesn't provide a mechanism for passing arguments to functions so you would need to do it something like

Code: Select all

MEASURE_SMALL = 30
MEASURE_MEDIUM = 60
MEASURE_BIG = 100
...
def pour_small():
    start_pour(MEASURE_SMALL)

def pour_medium():
    start_pour(MEASURE_MEDIUM)
...
def start_pour(amount):
    global count, count_cut_off
    count_cut_off = amount
    count = 0
    solenoid.on()
...

def count_pulse():
    global count, count_cut_off
    count  += 1
    if count >= count_cut_off:
        ...

button_small = Button(18)
button_medium = Button(...
...
button_small.when_pressed = pour_small
button_medium.when_pressed = pour_medium
...

The timeout cut-off logically follows the opening of the solenoid so you could put it in start_pour()

Code: Select all

from gpiozero import Button, LED
from signal import pause
from time import sleep

MEASURE_COUNT = 30
GAS_PAUSE = 2.0
TIME_OUT = 15.0

pulse = Button(23)
button = Button(18)
solenoid = LED(17)
clean = LED(25)

def count_pulse():
    global count
    count  += 1
    if count >= MEASURE_COUNT:
        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 of clean mode enjoy drink
 
def start_pour():
    global count
    solenoid.on()
    count = 0
    sleep(TIME_OUT)
    solenoid.off()

pulse.when_released = count_pulse
button.when_pressed = start_pour
count = 0
pause()
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Tue Oct 16, 2018 2:18 pm

Paddy,
i am little confuse. which button will pour medium and full pour ?

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

Re: stop solenoid valve with flow meter pulse

Tue Oct 16, 2018 2:36 pm

Well, you have to match up the Button() in the software with the physical connection to your RPi. That's up to you to do, but it might be neater to choose three adjacent spare GPIO pins. 14 and 15 would be next to 18 but double up as serial connections so how about three from 5, 6, 13, 19, 26 or from 16, 20, 21?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Tue Oct 16, 2018 2:47 pm

Paddy,
i hope you dont mind as i am asking so many questions but its my desire to complete it as soon as possible.
i will rewire everything such as buttons,leds (which will be acting as solenoid valves) and let you know if i dont understand something. i am sure i will be bad in coding side.

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

Re: stop solenoid valve with flow meter pulse

Tue Oct 16, 2018 8:14 pm

I don't mind at all. Just to further confuse you... It would be possible to have just one Button() and press it for different length of time to get different amounts. Along the lines of

Code: Select all

import time
...
SMALL_TM = 0.5
BIG_TM = 3.0
...
def start_timer():
    global start_tm
    start_tm = time.time()

def start_pour():
    global count, count_cut_off, start_tm
    press_len = time.time() - start_tm
    if press_len < SMALL_TM:
        count_cut_off = MEASURE_SMALL
    elif press_len < BIG_TM:
        count_cut_off = MEASURE_MEDIUM
    else:
        count_cut_off = MEASURE_BIG
    count = 0
    solenoid.on()
...
button.when_pressed = start_timer
button.when_released = start_pour

count = 0
count_cut_off = 30
start_tm = 0.0
 
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Tue Oct 16, 2018 11:10 pm

Paddy,
i would like to stick with 3 buttons because it will be user friendly also in future i would like to use touch displays to set temperature, add more bottles.
i tried to modify previous codes you posted but i am getting following error. i know there could be more errors. could you please check and test ?


Code: Select all

sudo python 3btn.py
  File "3btn.py", line 36
    solenoid.off()
           ^
IndentationError: expected an indented block

Code: Select all


from gpiozero import Button, LED
from signal import pause
from time import sleep
pulse = Button(23)
button = Button(18) # first button to  pour small
button = Button(14) # 2nd  button to pour medium
button = Button(15) # 3rd  button to pour big
solenoid = LED(17)
clean = LED(25)


MEASURE_COUNT = 30
GAS_PAUSE = 2.0
TIME_OUT = 15.0


MEASURE_SMALL = 30
MEASURE_MEDIUM = 60
MEASURE_BIG = 100
def pour_small():
    start_pour(MEASURE_SMALL)
def pour_medium():
    start_pour(MEASURE_MEDIUM)
def pour_big():
    start_pour(MEASURE_BIG)
def start_pour(amount):
    global count, count_cut_off
    count_cut_off = amount
    count = 0
    solenoid.on()

def count_pulse():
    global count, count_cut_off
    count  += 1
    if count >= count_cut_off:
    solenoid.off()

button_small = Button(18)
button_medium = Button(14)
button_big = Button(15)
button_small.when_pressed = pour_small
button_medium.when_pressed = pour_medium
button_big.when_pressed = pour_big

def count_pulse():
    global count
    count  += 1
    if count >= MEASURE_COUNT:
        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 of clean mode enjoy drink
 
def start_pour():
    global count
    solenoid.on()
    count = 0
    sleep(TIME_OUT)
    solenoid.off()

pulse.when_released = count_pulse
button.when_pressed = start_pour
count = 0
pause()
i am attaching a picture what type it will be look like.
Left white wine right red wine.
Attachments
machine.PNG
machine.PNG (5.97 KiB) Viewed 735 times

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

Re: stop solenoid valve with flow meter pulse

Fri Oct 19, 2018 2:07 am

paddyg wrote:
Tue Oct 16, 2018 8:14 pm
I don't mind at all. Just to further confuse you... It would be possible to have just one Button() and press it for different length of time to get different amounts. Along the lines of

Code: Select all

import time
...
SMALL_TM = 0.5
BIG_TM = 3.0
...
def start_timer():
    global start_tm
    start_tm = time.time()

def start_pour():
    global count, count_cut_off, start_tm
    press_len = time.time() - start_tm
    if press_len < SMALL_TM:
        count_cut_off = MEASURE_SMALL
    elif press_len < BIG_TM:
        count_cut_off = MEASURE_MEDIUM
    else:
        count_cut_off = MEASURE_BIG
    count = 0
    solenoid.on()
...
button.when_pressed = start_timer
button.when_released = start_pour

count = 0
count_cut_off = 30
start_tm = 0.0
 
Hi Paddy,
Could you please to put both codes together in my post #7 ?

Thank You

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

Re: stop solenoid valve with flow meter pulse

Fri Oct 19, 2018 9:24 am

Hi, your earlier post with the error message pointed to what the issue might be: that line turning the solenoid off should have been indented because it came just after the if line ending with a colon. However there were other inconsistencies that would have stopped the code working - you had the same variables defined in different places etc. This is an example of exactly why it's a good idea to always stick to the same layout/convention. If you start with imports then all your CONST variables, then class and function definitions then creating instances and initial values then... etc etc, you will find that your code magically starts to work!

This is what I think your 3 button version should look like (untested so you will still need to do some debugging)

Code: Select all

from gpiozero import Button, LED
from signal import pause
from time import sleep

MEASURE_COUNT = 30
GAS_PAUSE = 2.0
TIME_OUT = 15.0

MEASURE_SMALL = 30
MEASURE_MEDIUM = 60
MEASURE_BIG = 100

def pour_small():
    start_pour(MEASURE_SMALL)

def pour_medium():
    start_pour(MEASURE_MEDIUM)

def pour_big():
    start_pour(MEASURE_BIG)

def start_pour(amount):
    global count, count_cut_off
    count_cut_off = amount
    count = 0
    solenoid.on()

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 = Button(23)
button_small = Button(18) # first button to  pour small
button_medium = Button(14) # 2nd  button to pour medium
button_big = Button(15) # 3rd  button to pour big
solenoid = LED(17)
clean = LED(25)

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

count = 0
count_cut_off = MEASURE_MEDIUM

pause()
and for the single timed button version

Code: Select all

import time
from gpiozero import Button, LED
from signal import pause

MEASURE_COUNT = 30
GAS_PAUSE = 2.0
TIME_OUT = 15.0
SMALL_TM = 0.5
BIG_TM = 3.0

MEASURE_SMALL = 30
MEASURE_MEDIUM = 60
MEASURE_BIG = 100

def start_timer():
    global start_tm
    start_tm = time.time()

def start_pour():
    global count, count_cut_off, start_tm
    press_len = time.time() - start_tm
    if press_len < SMALL_TM:
        count_cut_off = MEASURE_SMALL
    elif press_len < BIG_TM:
        count_cut_off = MEASURE_MEDIUM
    else:
        count_cut_off = MEASURE_BIG
    count = 0
    solenoid.on()

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
        time.sleep(GAS_PAUSE) #keep clean mode for 2 second
        clean.off() # turn off clean mode enjoy drink

pulse = Button(23)
button = Button(18) # one variable length press button
solenoid = LED(17)
clean = LED(25)

pulse.when_released = count_pulse
button.when_pressed = start_timer
button.when_released = start_pour

count = 0
count_cut_off = MEASURE_MEDIUM
start_tm = 0.0

pause()
edit: duplicate function deleted. Pot, kettle...😳
Last edited by paddyg on Sat Oct 20, 2018 6:43 am, edited 1 time in total.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Fri Oct 19, 2018 9:25 pm

paddyg wrote:
Fri Oct 19, 2018 9:24 am
Hi, your earlier post with the error message pointed to what the issue might be: that line turning the solenoid off should have been indented because it came just after the if line ending with a colon. However there were other inconsistencies that would have stopped the code working - you had the same variables defined in different places etc. This is an example of exactly why it's a good idea to always stick to the same layout/convention. If you start with imports then all your CONST variables, then class and function definitions then creating instances and initial values then... etc etc, you will find that your code magically starts to work!

This is what I think your 3 button version should look like (untested so you will still need to do some debugging)

Code: Select all

from gpiozero import Button, LED
from signal import pause
from time import sleep

MEASURE_COUNT = 30
GAS_PAUSE = 2.0
TIME_OUT = 15.0

MEASURE_SMALL = 30
MEASURE_MEDIUM = 60
MEASURE_BIG = 100

def pour_small():
    start_pour(MEASURE_SMALL)

def pour_medium():
    start_pour(MEASURE_MEDIUM)

def pour_big():
    start_pour(MEASURE_BIG)

def start_pour(amount):
    global count, count_cut_off
    count_cut_off = amount
    count = 0
    solenoid.on()

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

def start_pour():
    global count
    solenoid.on()
    count = 0
    sleep(TIME_OUT)
    solenoid.off()

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

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

count = 0
count_cut_off = MEASURE_MEDIUM

pause()
and for the single timed button version

Code: Select all

import time
from gpiozero import Button, LED
from signal import pause

MEASURE_COUNT = 30
GAS_PAUSE = 2.0
TIME_OUT = 15.0
SMALL_TM = 0.5
BIG_TM = 3.0

MEASURE_SMALL = 30
MEASURE_MEDIUM = 60
MEASURE_BIG = 100

def start_timer():
    global start_tm
    start_tm = time.time()

def start_pour():
    global count, count_cut_off, start_tm
    press_len = time.time() - start_tm
    if press_len < SMALL_TM:
        count_cut_off = MEASURE_SMALL
    elif press_len < BIG_TM:
        count_cut_off = MEASURE_MEDIUM
    else:
        count_cut_off = MEASURE_BIG
    count = 0
    solenoid.on()

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
        time.sleep(GAS_PAUSE) #keep clean mode for 2 second
        clean.off() # turn off clean mode enjoy drink

pulse = Button(23)
button = Button(18) # one variable length press button
solenoid = LED(17)
clean = LED(25)

pulse.when_released = count_pulse
button.when_pressed = start_timer
button.when_released = start_pour

count = 0
count_cut_off = MEASURE_MEDIUM
start_tm = 0.0

pause()
edit: duplicate function deleted. Pot, kettle...😳
Paddy,
When i run first codes ( 3 buttons) when i press button led ( solenoid ) does not turn on and when i blow air in flow meter it turn on clean led which is not solid on its blinking ( like getting less power ).
when i put (self): it turn on solenoid when i press button 18 but it does not turn off solenoid and turn on clean when i blow air in flow meter. however it turn off after couple seconds may be because of time out ?

Code: Select all

def start_pour(self):
    global count
    solenoid.on()
    count = 0
    sleep(TIME_OUT)
    solenoid.off()

here is terminal output log when i press first button

Code: Select all

 python riz3btn.py
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python2.7/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python2.7/dist-packages/gpiozero/mixins.py", line 323, in _fire_events
    self._fire_activated()
  File "/usr/lib/python2.7/dist-packages/gpiozero/mixins.py", line 355, in _fire_activated
    super(HoldMixin, self)._fire_activated()
  File "/usr/lib/python2.7/dist-packages/gpiozero/mixins.py", line 301, in _fire_activated
    self.when_activated()
  File "riz3btn.py", line 14, in pour_small
    start_pour(MEASURE_SMALL)
TypeError: start_pour() takes no arguments (1 given)
^CTraceback (most recent call last):
  File "riz3btn.py", line 59, in <module>
    pause()
KeyboardInterrupt

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

Re: stop solenoid valve with flow meter pulse

Sat Oct 20, 2018 6:41 am

hi sorry about that the second version of the fuction is incorrect and should be deleted. I tried to do that but hard on phone. I will re.edit prev post
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Sat Oct 20, 2018 5:14 pm

paddyg wrote:
Sat Oct 20, 2018 6:41 am
hi sorry about that the second version of the fuction is incorrect and should be deleted. I tried to do that but hard on phone. I will re.edit prev post
Hi Paddy,
No problem. i will wait for update.
Thank you

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

Re: stop solenoid valve with flow meter pulse

Sat Oct 20, 2018 7:27 pm

Hi @onlinegill, I'm back at a proper computer now so can see what I'm doing etc. I did correct my previous code when I replied to you and it appears I did it correctly (but there could well be other errors so you might have to debug as before!!). The error message you got pointed to the fact that there was an argument being passed to start_pour which is correct but that it was defined to have no arguments, which was incorrect. This led me to look at the code for the function and lo and behold the function was still in the code twice (from when I copied it from your earlier post). You look to have gone down a similar route but for some reason decided to put a 'self' into the function arguments. That's something that only really applies to functions that are being used as class methods where self is used as a reference to the instance.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Sat Oct 20, 2018 7:33 pm

Paddy,
I used code the one you provided which are in my this post below. how can i make these codes work which are in my current post ?
when i click on button it does not turn on solenoid and right way i can see following output in terminal window.

Code: Select all

[email protected]:~/wine $ sudo python padydy3.py
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/gpiozero/pins/rpigpio.py", line 213, in _call_when_changed
    super(RPiGPIOPin, self)._call_when_changed()
  File "/usr/lib/python2.7/dist-packages/gpiozero/pins/pi.py", line 268, in _call_when_changed
    method()
  File "/usr/lib/python2.7/dist-packages/gpiozero/mixins.py", line 323, in _fire_events
    self._fire_activated()
  File "/usr/lib/python2.7/dist-packages/gpiozero/mixins.py", line 355, in _fire_activated
    super(HoldMixin, self)._fire_activated()
  File "/usr/lib/python2.7/dist-packages/gpiozero/mixins.py", line 301, in _fire_activated
    self.when_activated()
  File "padydy3.py", line 14, in pour_small
    start_pour(MEASURE_SMALL)
TypeError: start_pour() takes no arguments (1 given)

code i am using

Code: Select all

from gpiozero import Button, LED
from signal import pause
from time import sleep

MEASURE_COUNT = 30
GAS_PAUSE = 2.0
TIME_OUT = 15.0

MEASURE_SMALL = 30
MEASURE_MEDIUM = 60
MEASURE_BIG = 100

def pour_small():
    start_pour(MEASURE_SMALL)

def pour_medium():
    start_pour(MEASURE_MEDIUM)

def pour_big():
    start_pour(MEASURE_BIG)

def start_pour(amount):
    global count, count_cut_off
    count_cut_off = amount
    count = 0
    solenoid.on()

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

def start_pour():
    global count
    solenoid.on()
    count = 0
    sleep(TIME_OUT)
    solenoid.off()

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

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

count = 0
count_cut_off = MEASURE_MEDIUM

pause()

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

Re: stop solenoid valve with flow meter pulse

Sat Oct 20, 2018 7:55 pm

Well, as I said above: read the error message (only the last three or four lines generally make any sense, but don't stop reading it until you understand what caused the error) Then look at your code. You will immediately see that start_pour is redefined with an incorrect version after the correct version. DELETE THE WRONG version and see if things improve.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Sat Oct 20, 2018 8:01 pm

paddyg wrote:
Sat Oct 20, 2018 7:55 pm
Well, as I said above: read the error message (only the last three or four lines generally make any sense, but don't stop reading it until you understand what caused the error) Then look at your code. You will immediately see that start_pour is redefined with an incorrect version after the correct version. DELETE THE WRONG version and see if things improve.
I tried my best to understand but feeling i am dumb. could not understand what is wrong and what needs to be fixed. sorry

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

Re: stop solenoid valve with flow meter pulse

Sat Oct 20, 2018 8:10 pm

In the code you just posted: highlight lines 37-43 (inclusive) press delete, then save.
Alternative look at my post above 19 Oct 09:24 click on 'select all' at the top of the first code sample copy it then highlight all of your current code in your editor and paste over it. (It's exactly the same except I deleted those seven lines (at 7:30 this morning))
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Sun Oct 21, 2018 2:43 am

Paddy,
i deleted 37-43 which were following codes.

Code: Select all

def start_pour():
    global count
    solenoid.on()
    count = 0
    sleep(TIME_OUT)
    solenoid.off()
i played with codes and adjusted pins as as there was no pin #14 etc or may be could not find in dark :? .
I have following two issues now

1 All the buttons are working fine but i need to put

Code: Select all

sleep(TIME_OUT)
Where should i put it ?which would turn off solenoid if flow meter failed.

2 clean = LED(25) which use sleep(GAS_PAUSE) is not turning off in 2 seconds also its blinking like it has less power. i made a video you will see after i blow air in flow meter solenoid led turned off right away than clean LED turn on which is supposed to turn off in 2 second but it kept on for couple seconds.

https://photos.app.goo.gl/Zjy4PnwDJhf9RGuJA



Finally here my updated codes

Code: Select all

from gpiozero import Button, LED
from signal import pause
from time import sleep

GAS_PAUSE = 2.0
TIME_OUT = 15.0

MEASURE_SMALL = 30
MEASURE_MEDIUM = 60
MEASURE_BIG = 100

def pour_small():
    start_pour(MEASURE_SMALL)

def pour_medium():
    start_pour(MEASURE_MEDIUM)

def pour_big():
    start_pour(MEASURE_BIG)

def start_pour(amount):
    global count, count_cut_off
    count_cut_off = amount
    count = 0
    solenoid.on()

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 = 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)

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

count = 0
count_cut_off = MEASURE_MEDIUM

pause()
i also removed following since we are measuring in deterrent size

Code: Select all

MEASURE_COUNT = 30

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

Re: stop solenoid valve with flow meter pulse

Sun Oct 21, 2018 5:35 pm

Ah, well done spotting and removing the unneeded MEASURE_COUNT. You probably also spotted that the timeout and turn the solenoid off should go at the end of the start_pour just as it was in the function you deleted.

I'm not really sure what your video shows as I don't know what the button, or the two leds are connected to. I would expect the sequence to go: press button, valve opens, flow meter runs for 30/60/100 pulses, valve closes, clean valve opens, two seconds flow, clean valve closes. Depending on what the circuit is you connect the GPIO to you might be pulling too much current. You should switch things like solenoids using transistors (or using a board designed for this kind of thing), you can drive leds but you need to limit the current with resistors (which you do seem to have but can't see how they're connected)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: stop solenoid valve with flow meter pulse

Sun Oct 21, 2018 11:26 pm

paddyg wrote:
Sun Oct 21, 2018 5:35 pm
Ah, well done spotting and removing the unneeded MEASURE_COUNT. You probably also spotted that the timeout and turn the solenoid off should go at the end of the start_pour just as it was in the function you deleted.

I'm not really sure what your video shows as I don't know what the button, or the two leds are connected to. I would expect the sequence to go: press button, valve opens, flow meter runs for 30/60/100 pulses, valve closes, clean valve opens, two seconds flow, clean valve closes. Depending on what the circuit is you connect the GPIO to you might be pulling too much current. You should switch things like solenoids using transistors (or using a board designed for this kind of thing), you can drive leds but you need to limit the current with resistors (which you do seem to have but can't see how they're connected)
Paddy,
when i put time out code at the end of the start_pour than solenoid does not stop when i blow air in flow meter but it does stop in timeout time period when i dont blow air. so that mean it work vice versa.
Am i missing something ?

Code: Select all

def start_pour(amount):
    global count, count_cut_off
    count_cut_off = amount
    count = 0
    solenoid.on()
    sleep(TIME_OUT)
    solenoid.off()

Return to “Python”