from machine import Pin, I2C, Timer,SoftI2C
import ssd1306
import time
# Button pin definitions
BUTTON_AUTO_MANUAL_PIN = 23
BUTTON_SETTINGS_PIN = 19
BUTTON_SELECT_PIN = 18
# Ultrasonic sensor pin definitions
ULTRASONIC_TRIGGER_PIN = 5
ULTRASONIC_ECHO_PIN = 4
# Pump control pin
PUMP_PIN = 2
# OLED display setup
i2c = SoftI2C( Pin(22), Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Initialize buttons
button_auto_manual = Pin(BUTTON_AUTO_MANUAL_PIN, Pin.IN, Pin.PULL_UP)
button_settings = Pin(BUTTON_SETTINGS_PIN, Pin.IN, Pin.PULL_UP)
button_select = Pin(BUTTON_SELECT_PIN, Pin.IN, Pin.PULL_UP)
# Initialize ultrasonic sensor
ultrasonic_trigger = Pin(ULTRASONIC_TRIGGER_PIN, Pin.OUT)
ultrasonic_echo = Pin(ULTRASONIC_ECHO_PIN, Pin.IN)
# Initialize pump control pin
pump = Pin(PUMP_PIN, Pin.OUT)
pump.off()
# Variables
auto_mode = True
selected_setting = 0
# Function to update OLED display
# Function to measure water level using ultrasonic sensor
def measure_water_level():
ultrasonic_trigger.on()
time.sleep_us(10)
ultrasonic_trigger.off()
while not ultrasonic_echo.value():
pulse_start = time.ticks_us()
while ultrasonic_echo.value():
pulse_end = time.ticks_us()
pulse_duration = time.ticks_diff(pulse_end, pulse_start)
distance = pulse_duration / 58.0 # Convert to distance in cm
return distance
# Function to control the water pump
def pump_control():
global auto_mode
if auto_mode:
# Implement automatic control based on water level
water_level = measure_water_level()
if water_level < 10: # Adjust threshold as needed
pump.on()
else:
pump.off()
else:
pump.off()
# Attach interrupt handlers to buttons
#button_auto_manual.irq(trigger=Pin.IRQ_FALLING, handler=button_pressed)
#button_settings.irq(trigger=Pin.IRQ_FALLING, handler=button_pressed)
#button_select.irq(trigger=Pin.IRQ_FALLING, handler=button_pressed)
"""
oled.show()
time.sleep(delay)
# Main loop
while True:
pump_control()
time.sleep(1) # Adjust interval as needed
my_mat=[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,1,1,1,1,1,1,1,1,1,0,0,0],[0,0,1,0,0,0,0,0,0,0,0,0,1,0,0],[0,1,0,0,1,1,1,1,1,1,1,0,0,1,0],[1,0,0,1,0,0,0,0,0,0,0,1,0,0,1] ,[0,0,1,0,0,1,1,1,1,1,0,0,1,0,0],[0,1,0,0,1,0,0,0,0,0,1,0,0,1,0],[0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0],[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0]]
for y in range(len(my_mat)):
for x in range(len(my_mat[y])):
oled.pixel(x,y,my_mat[y][x])
my_mat=[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,1,1,1,1,1,1,1,1,1,0,0,0],[0,0,1,0,0,0,0,0,0,0,0,0,1,0,0],[0,1,0,0,1,1,1,1,1,1,1,0,0,1,0],[0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],[0,0,1,0,0,1,1,1,1,1,0,0,1,0,0],[0,0,0,0,1,0,0,0,0,0,1,0,0,0,0],[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0],[0,0,0,0,0,1,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
for y in range(len(my_mat)):
for x in range(len(my_mat[y])):
oled.pixel(x+114,y,my_mat[y][x])
oled.text('ioT',0,3)
oled.show()
time.sleep(1)
oled.text(' ',20,3,0)
oled.line(0, 12, 128, 12, 1)
my_mat=[[0,0,0,0,0,0,0,0,0,0],[0,0,1,1,1,1,1,1,1,0],[0,1,0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0,0,0],[0,0,0,1,1,1,1,1,0,0],[0,0,1,0,0,0,0,0,1,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,1,1,1,0,0,0],[0,0,0,1,0,0,0,1,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,1,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]]
for y in range(len(my_mat)):
for x in range(10):
oled.pixel(x+118,y,my_mat[y][x])
"""
'''
def display_loading_bar(oled, duration, steps=20):
oled.fill(0) # Clear the display
oled.show()
interval = duration / steps
for i in range(steps + 1):
oled_width=128
percent = i * 100 // steps
# Calculate the width of the progress bar
bar_width = int((i / steps) * oled_width)
oled.fill_rect(0, 0, bar_width, 10, 1) # Draw filled part of the progress bar
oled.rect(0, 0, oled_width, 10, 1) # Draw outline of the progress bar
oled.text("Loading...", 0, 20) # Display loading text
oled.text(f"{percent}%", oled_width - 30, 20) # Display percentage
oled.show()
time.sleep(interval)
#oled.fill(0) # Clear the display after loading complete
oled.show()
#display_loading_bar(oled,1)
#oled.text('Tank Level:{} %'.format(199),0,13)
#oled.rect(0,21,128,10,1)
#oled.text('SumP Level:{} %'.format(199),0,33)
#oled.rect(0,42,128,10,1)
#oled.text('motor:{}valve:ON'.format('ON'),0,53)
oled.text('MANNUAL',0,2)
oled.text('iOT',86,2)
my_mat=[[0,0,0,1,1,1,1,1,1,1,1,1,0,0,0],[0,0,1,0,0,0,0,0,0,0,0,0,1,0,0],[0,1,0,0,1,1,1,1,1,1,1,0,0,1,0],[0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],[0,0,1,0,0,1,1,1,1,1,0,0,1,0,0],[0,0,0,0,1,0,0,0,0,0,1,0,0,0,0],[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0],[0,0,0,0,0,1,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
for y in range(len(my_mat)):
for x in range(len(my_mat[y])):
oled.pixel(x+114,y,my_mat[y][x])
oled.fill(0)
oled.scroll(20,5)
oled.show()
'''
def update_display():
oled.fill(0)
oled.text("Water Level:", 0, 0)
oled.text("Auto Mode: {}".format("ON" if auto_mode else "OFF"), 0, 20)
oled.text("Settings Menu", 0, 40)
oled.text("Select: {}".format(selected_setting), 0, 50)
oled.show()
#my_mat=[[0,0,0,1,1,1,1,1,1,1,1,1,0,0,0],[0,0,1,0,0,0,0,0,0,0,0,0,1,0,0],[0,1,0,0,1,1,1,1,1,1,1,0,0,1,0],[0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],[0,0,1,0,0,1,1,1,1,1,0,0,1,0,0],[0,0,0,0,1,0,0,0,0,0,1,0,0,0,0],[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0],[0,0,0,0,0,1,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
my_buff={0:(118,119,120,121,122,123,124,125,126),1:(117,127),2:(116,119,120,121,122,123,124,125,128),3:(118,126),4:(117,120,121,122,123,124,127),5:(119,125),6:(121,122,123),7:(120,124),9:[122]}
#for y in range(len(my_mat)):
# for x in range(len(my_mat[y])):
# oled.pixel(x+114,y,my_mat[y][x])
oled.fill(0)
for x in my_buff.keys():
for y in my_buff[x]:
oled.pixel(y,x,1)
oled.show()