Python Forum
Water flow sensor and Servo Motor wont run at the same time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Water flow sensor and Servo Motor wont run at the same time
#1
Hei, we have a project going on at moment where we have to use a flowmeter sensor and a servo motor. The main objective of the servo motor is to open or close a valve, and once it opens the flowmeter will measure the flow. Both of them works perfectly fine separately, but once we merge those two together, the flowmeter works fine, but servo motor wont move, but only vibrate, we found out that we can use threading, but the same issue still appears,

We decided to use an S3003(180 degrees) Servo Motor and an YF-S201 Water Flow Sensor

We have split our coding into three different tabs:

Init:
from threading import Thread
import Servo_Motor # Import Servo_Motor.py function
import Flowmeter # Import Flowmeter.py function
import time

if __name__=='__main__':
	water = Flowmeter.flow()
	
	
	on = Servo_Motor.on(1)
	off = Servo_Motor.off(0)
	while True:
	Thread(target = water).start()
	#GPIO.cleanup()
Flowmeter:
import RPi.GPIO as GPIO # Used for sending repetitive signals at even intervals by importing a class to control the GPIO on a Raspberry Pi
import time # Used for slowing down our Python scipt by delaying
import sys # Used for sys.exit() so we can exit from Python

GPIO.setmode(GPIO.BCM) # Activate the Broadcom-chip specific pin numbers
flowPIN = 17 # Assign GPIO17 Pin 11 to Flow Meter
GPIO.setup(flowPIN, GPIO.IN) #Sets Flow meter PIN = 17 as INPUT

def flow():
	rate_cnt = 0 # Revolution counts (r/min)
	tot_cnt = 0 # Total counts
	time_zero = 0.0 # System start up time
	time_start = 0.0 #Keep measurement beging time
	time_end = 0.0 # Keep measurement end time
	gpio_last = 0 # Was last state 0 or 1 or other?
	pulses = 0 # 0-5 pulses from YF-S201
	constant = 1.79 # Water meter calibration factor
	
	print('Water flow - Approximate')
	time_zero = time.time()
	while True:
		rate_cnt = 0 # Reset rate counter
		pulses = 0 # 0-5 pulses from YF-S201
		time_start = time.time() # Keep start time
		while pulses <= 5: # 6 pulses per revolution
			gpio_cur = GPIO.input(flowPIN) # Poll input
			if gpio_cur != 0 and gpio_cur != gpio_last: # If the rotor has not stopped
				pulses += 1 # Count pulses upwards
			'''
			try:
						None
					except KeyboardInterrupt: # Look for exit command
						print("\nCTR + C - Exiting")
						GPIO.cleanup() # Clean up GPIO
						print("Done") # Print 'Done'
						sys.exit() # Exit
			'''
			gpio_last = gpio_cur # Keep last input state
		rate_cnt += 1 # Revolution / time
		tot_cnt += 1 # Total revolutions since start
		time_end = time.time() # End of measurement time
		
		print("\nLiters / min ", round((rate_cnt * constant)/(time_end - time_start),2),"approximated")
		#print("Total liters ", round(tot_cnt*constant,1))
		#print("Time (min & clock) ", round((time.time() - time_zero)/60,2), "\t", time.asctime(time.localtime(time.time())),"\n")
Servo Motor:
import RPi.GPIO as GPIO # Used for sending repetitive signals at even intervals by importing a class to control the GPIO on a Raspberry Pi
import time # Used for slowing down our Python scipt by delaying


GPIO.setmode(GPIO.BCM) # Activate the Broadcom-chip specific pin numbers
servoPIN = 27 # Assign GPIO17 Pin 11 to servomotor
GPIO.setup(servoPIN, GPIO.OUT) # Sets ServoPIN = 17 as OUTPUT

p = GPIO.PWM(servoPIN, 50) # Sets servoPIN up with a frequency of 50Hz
p.start(2.5) # Set the output to a 25% Duty Cycle



def on(x): # Function to open the valve
	if(x == 1):
		p.ChangeDutyCycle(7.5) # Set the output to a 75% Duty Cycle
		time.sleep(0.5) # Delay by 500 milliseconds
	else:
		print("Cant open")

def off(x): # Function to close the valve
	if(x == 0):
		p.ChangeDutyCycle(2.5) # Set the output to a 25% Duty Cycle
		time.sleep(0.5) # Delay by 500 milliseconds
	else:
		print("Cant close")
'''
def keyboard_interrupt():
	p.stop() # Stop PWM
	GPIO.cleanup() # Gargbage collecting (Release any resources our script uses)
'''
Our question is, why can`t we run those two at the same time?
Reply
#2
Bump!

Alright, we found out that we dont need threading, and and that our Servo_Motor.py works, our problem has been pinpointed to the Flowmeter.py.

We found out that it doesnt print at all, that it skips Flowmeter.py and immediately jumps to Servo_Motor.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why wont this work? samh625 6 3,321 Jul-30-2020, 08:30 PM
Last Post: perfringo
  MyProgrammingLab wont accept anything I put in chicks4 2 11,562 Feb-10-2019, 11:44 PM
Last Post: chicks4
  nested while loop flow help Ponamis 4 2,970 Nov-02-2018, 11:22 PM
Last Post: Ponamis
  Inflow watertank before outward flow starts orjstrand 6 4,981 May-02-2018, 11:31 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020