I'm currently working on a project of mine and am closing in on a rather crude first prototype, but am having trouble wrapping up the last bit of my code as I'm rather new to this. I'm currently using a while true loop to continuously read pressure values from an MPRLS sensor and then adjust the duty cycle of a pump to pull a vacuum of a specified pressure. I also need to have a pump that turns on at a set duty cycle for say 1 second and then shuts off for 2 seconds, and repeats this process over and over. I tried placing some simple commands of turning the duty cycle on and then off separated by a couple of time.sleep commands, but that then throws off how often pressure is read. Any ideas on how to accomplish that? I'll attach the code below.
Code: Select all
import board import pwmio import adafruit_mprls import busio import time #initializing pins and i2c bus Vac_Pump = pwmio.PWMOut(board.GP17, frequency=100, duty_cycle=0) Dos_Pump = pwmio.PWMOut(board.GP16, frequency=100, duty_cycle=0) i2c = busio.I2C(scl=board.GP13, sda=board.GP12) mpr = adafruit_mprls.MPRLS(i2c, psi_min=0, psi_max=25) #Reading in fist pressure value to set to atmospheric psi = mpr.pressure atm = psi* 51.7149 print(atm) #preffered pressure of -100mmHg pref = -120 pref_upper = pref-1 pref_lower = pref+1 #Initial estimate of correct duty cycle duty = 20000 #Loop to run pumps and print pressure values while True: psi = mpr.pressure mmHg = (psi* 51.7149)-atm print(mmHg) time.sleep(1) if mmHg > pref_upper: x=pref-mmHg duty = duty-(65535*(x/1000)) Vac_Pump.duty_cycle = int(duty) elif mmHg < pref_lower: x=pref-mmHg duty = duty-(65535*(x/1000)) Vac_Pump.duty_cycle = int(duty) else: Vac_Pump.duty_cycle = int(duty)