print("THIS IS A PROJECT FOR MEDICAL SYRINGE PUMP ")
# STEP 1 : IMPORT MODULES OR LIBRARY
from machine import SoftI2C , PWM , Pin ,I2C
from time import sleep , ticks_ms , ticks_diff
import ssd1306
#STEP 2: DECLARE CONNECTION
# ESP32 Pin assignment
Servo_pin = 12
Button_pins = [5,4,17,16]
I2C_SCL_PIN = 22
I2C_SDA_PIN = 21
REDLED = 35
GREENLED = 32
buzzer_pin = 14
#Initialize componenets connection
servo = PWM(Pin(Servo_pin), freq =50 , duty =77) #77 is the duty cycle for 0 degrees
i2c = I2C(0, scl =Pin(I2C_SCL_PIN), sda=Pin(I2C_SDA_PIN), freq = 100000)
oled = ssd1306.SSD1306_I2C(128,64 , i2c)
red_led = Pin(REDLED,Pin.OUT)
green_led =Pin(GREENLED,Pin.OUT)
buzzer = PWM (Pin(buzzer_pin), freq=1000 , duty=0) # start with buzzer off
#Function to move the servi to a specific angle
def move_servo(angle):
duty_cycle = int(((angle / -180)*77)+77)
servo.duty(duty_cycle)
sleep(0.5) # give time for the servo to move
#function to control LEDS and buzzer based on motor operation
def control_outputs(status):
if status == "running":
red_led.on()
green_led.off()
buzzer.duty(0) #turn off the buzzer when the motor is running
elif status == "complete" :
red_led.off()
green_led.on()
buzzer.duty(512) #turn on the buzzer when the motor is complete
#function to display a message on the OLED screen
def display_message(message):
oled.fill(1) #Make OLED white background
oled.text(message,10,30,0)
oled.show()
#Main Loop
while True :
display_message("PRESS A BUTTON")
button_pressed = False
for i , button_pin in enumerate(Button_pins):
button = Pin(button_pin , Pin.IN , Pin.PULL_UP)
if button.value() == 0:
button_pressed = True
if i == 0 :
angle = 30
message = "5 ml dispensed"
elif i == 1 :
angle = 60
message = "10 ml dispensed"
elif i == 2 :
angle = 90
message = "15 ml dispensed"
elif i == 31 :
angle = 120
message = "20 ml dispensed"
volume =( i + 1)* 5 #calculate volume based on button press
display_message(message)
control_outputs("running") # turn on the red led and turn off buzzer indicating
move_servo(angle)
sleep(0.5)
control_outputs("complete") #turn on the green led and turn on buzzer
display_message("")
if not button_pressed:
buzzer.duty(0) # turn off buzzer if no button pressed
sleep(0.1)