from machine import Pin, ADC
import utime
# Ultrasonic sensor
trigger = Pin(2, Pin.OUT)
echo = Pin(3, Pin.IN)
distance = 0 # Initialize distance as a global variable
# Simulate the turbidity sensor with a potentiometer
potentiometer_pin = 26 # Use Pin 26 for the ADC input
turbidity_adc = ADC(Pin(potentiometer_pin))
# Simulate the pH sensor with another potentiometer
ph_potentiometer_pin = 27 # Use Pin 27 for the ADC input of the simulated pH sensor
ph_adc = ADC(Pin(ph_potentiometer_pin))
# Simulate the water flow sensor with a push button
water_flow_pin = 14 # Use Pin 14 for the push button input
water_flow_sensor = Pin(water_flow_pin, Pin.IN, Pin.PULL_UP)
water_flow_count = 0
water_flow_pressed = False # Flag to track button press
# Function to perform filtering process
def perform_filtering_process():
# Simulated filtering process
print("Performing filtering process...")
def ultra():
global distance # Declare distance as a global variable
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
while True:
ultra()
utime.sleep(1)
if 70 < distance < 90:
print("Water tank is going to be full")
if distance < 100 and not (70 < distance < 90):
print("Water tank is full")
break
# Simulate turbidity level with the potentiometer
turbidity_value = turbidity_adc.read_u16()
print("Turbidity Level:", turbidity_value)
if turbidity_value > 50000:
print("Water is dirty")
while turbidity_value > 50000: # Keep filtering until water is pure
perform_filtering_process() # Function to perform filtering process
turbidity_value = turbidity_adc.read_u16() # Update turbidity value
print("Turbidity Level:", turbidity_value)
utime.sleep(1)
print("Water is now pure")
else:
print("Water is pure")
# Simulate pH level with the potentiometer
simulated_ph_value = ph_adc.read_u16()
print("Simulated pH Level:", simulated_ph_value)
if simulated_ph_value > 30000: # Adjust the threshold value as needed
print("Water pH is outside the acceptable range")
perform_filtering_process() # Function to perform the filtering process
else:
print("Water pH is within the acceptable range")
# Simulate water flow sensor with a push button
if not water_flow_sensor.value() and not water_flow_pressed:
water_flow_count += 1
water_flow_pressed = True # Set flag to True when button is pressed
print("Water flow count:", water_flow_count)
elif water_flow_sensor.value() and water_flow_pressed:
water_flow_pressed = False # Reset flag when button is released
utime.sleep(1)