"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Raspberry Pi Pico SSD1306 OLED Display (MicroPython) ┃
┃ ┃
┃ A program to display Raspberry Pi logo, text, and a ┃
┃ simple timer animation on an SSD1306 OLED display ┃
┃ connected to a Raspberry Pi Pico. ┃
┃ ┃
┃ Copyright (c) 2023 Anderson Costa ┃
┃ GitHub: github.com/arcostasi ┃
┃ License: MIT ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
from machine import Pin, I2C, freq, time_pulse_us, ADC
from ssd1306 import SSD1306_I2C
from hx711 import HX711
import framebuf, sys
import utime
import time
ECHO_PIN = 26
TRIGGER_PIN = 27
SWITCH_A = 9
START = 8
STOP = 7
BUTTON_B = 6
LED_GREEN = 15
MOTOR = 14
trigger = Pin(TRIGGER_PIN, Pin.OUT)
led_green = Pin(LED_GREEN, Pin.OUT)
motor = Pin(MOTOR, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
button_b = Pin(BUTTON_B, Pin.IN, Pin.PULL_DOWN)
start = Pin(START, Pin.IN, Pin.PULL_DOWN)
stop = Pin(STOP, Pin.IN, Pin.PULL_DOWN)
switch_a = Pin(SWITCH_A, Pin.IN, Pin.PULL_DOWN)
WIDTH = 128
HEIGHT = 64
LED_STATE = 0
MOTOR_STATUS = 1
DEBOUNCE_TIME = 0
MODE = switch_a.value() # 0:manual, 1:automático
hx = HX711(d_out=5, pd_sck=4)
adc = ADC(Pin(28))
i2c_dev = I2C(0, scl=Pin(1), sda=Pin(0), freq=200000)
display = SSD1306_I2C(WIDTH, HEIGHT, i2c_dev)
#
def button_b_callback(pin):
global DEBOUNCE_TIME, MOTOR_STATUS
if (time.ticks_ms()-DEBOUNCE_TIME>100):
print("Button B")
MOTOR_STATUS = 1
DEBOUNCE_TIME = time.ticks_ms()
def switch_a_callback(pin):
global DEBOUNCE_TIME, MODE, switch_a
if (time.ticks_ms()-DEBOUNCE_TIME>100):
MODE = switch_a.value()
DEBOUNCE_TIME = time.ticks_ms()
def start_callback(pin):
global DEBOUNCE_TIME
if (time.ticks_ms()-DEBOUNCE_TIME>100):
print("START")
DEBOUNCE_TIME = time.ticks_ms()
def stop_callback(pin):
global DEBOUNCE_TIME
if (time.ticks_ms()-DEBOUNCE_TIME>100):
print("STOP")
DEBOUNCE_TIME = time.ticks_ms()
def get_distance():
trigger.low()
time.sleep_us(2)
trigger.high()
time.sleep_us(10)
trigger.low()
pulse_duration = time_pulse_us(echo, Pin.high)
distance = pulse_duration*0.0343/2
return distance
def main():
button_b.irq(trigger=Pin.IRQ_RISING, handler=button_b_callback)
start.irq(trigger=Pin.IRQ_RISING, handler=start_callback)
stop.irq(trigger=Pin.IRQ_RISING, handler=stop_callback)
switch_a.irq(trigger=Pin.IRQ_RISING or Pin.IRQ_FALLING, handler=switch_a_callback)
weight = hx.read()/420
distance = round(get_distance(), 2)
adc_value = round(adc.read_u16()/1311, 2)
while True:
while MODE==0: # Funcionamiento manual
print("Manual")
display.text("Peso: "+str(weight)+" kg", 0, 0)
display.text("Objetivo: "+str(adc_value)+"kg", 0, 15)
display.text("-----", 0, 30)
if weight>adc_value:
MOTOR_STATUS = 0
motor.value(MOTOR_STATUS)
time.sleep(0.5)
while MODE==1:
print("Automático")
print(MOTOR_STATUS)
time.sleep(0.5)
# while MODE==1: # Functionamiento automático
# print("Automático")
# # Motor
# if distance>10 and distance<99:
# time.sleep(3)
# MOTOR_STATE = 1
# motor.value(MOTOR_STATE)
# elif distance>=100:
# MOTOR_STATE = 0
# motor.value(MOTOR_STATE)
# # Pantalla OLED
# if distance>=90 and distance<=100:
# display.fill(0)
# display.text("NIVEL BAJO", 0, 0)
# display.text("Altura: "+str(distance)+" cm", 0, 15)
# display.text("-----", 0, 45)
# display.show()
# else:
# display.fill(0)
# display.text("Peso: "+str(weight)+" kg", 0, 0)
# display.text("Altura: "+str(distance)+" cm", 0, 15)
# display.text("Pot: "+str(adc_value)+" kg", 0, 30)
# display.text("-----", 0, 45)
# display.show()
print('.')
time.sleep(1)
if __name__ == '__main__':
main()