Raspberry Pico While True Loop

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
bdennis8
 
Posts: 5
Joined: Thu Jun 02, 2022 3:57 pm

Raspberry Pico While True Loop

Post by bdennis8 »

Hello there,

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.

Thanks

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)

User avatar
dastels
 
Posts: 15674
Joined: Tue Oct 20, 2015 3:22 pm

Re: Raspberry Pico While True Loop

Post by dastels »

The basic ideas are covered here: https://learn.adafruit.com/multi-taskin ... cuitpython.

Dave

User avatar
bdennis8
 
Posts: 5
Joined: Thu Jun 02, 2022 3:57 pm

Re: Raspberry Pico While True Loop

Post by bdennis8 »

This cleared things up a lot, and I got the pico to execute the code.

Much appreciated Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”