save it
parent
08f60d3e36
commit
3d86519e73
@ -0,0 +1,2 @@
|
|||||||
|
# MP Remote
|
||||||
|
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
import time
|
||||||
|
from machine import Timer, Pin
|
||||||
|
|
||||||
|
|
||||||
|
def debounce(pin, callback, delay_ms=50):
|
||||||
|
def _debounce_handler(timer):
|
||||||
|
if pin.value() == 0: # Assuming active low button
|
||||||
|
callback()
|
||||||
|
|
||||||
|
debounce_timer = Timer(-1)
|
||||||
|
|
||||||
|
def _pin_handler(pin):
|
||||||
|
debounce_timer.init(
|
||||||
|
mode=Timer.ONE_SHOT, period=delay_ms, callback=_debounce_handler
|
||||||
|
)
|
||||||
|
|
||||||
|
return _pin_handler
|
||||||
|
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
|
||||||
|
|
||||||
|
def button_pressed():
|
||||||
|
print("Button pressed!")
|
||||||
|
counter = 0
|
||||||
|
|
||||||
|
|
||||||
|
button_pin = Pin(14, Pin.IN, Pin.PULL_UP)
|
||||||
|
button_handler = debounce(button_pin, button_pressed)
|
||||||
|
button_pin.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if counter <= 10:
|
||||||
|
print("Press the button")
|
||||||
|
elif counter > 10 and counter < 20:
|
||||||
|
print("Come on....Press the button.")
|
||||||
|
else:
|
||||||
|
print("PRESS THE BUTTON!")
|
||||||
|
time.sleep(5)
|
||||||
|
counter += 1
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
# Sea Monkey Air
|
||||||
|
|
||||||
|
|
||||||
|
## Equipment
|
||||||
|
|
||||||
|
- RP2040
|
||||||
|
- Hiletgo L9110S DC Motor drive module
|
||||||
|
- 12v DC peristaltic pump
|
||||||
|
- 9v power supply
|
||||||
|
- RP2040 Breakout board (KEYESTUDIO Raspberry Pi PICO IO Shield Pico Breakout Board for Raspberry Pi Pico)
|
||||||
|
|
||||||
|
##
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
import time
|
||||||
|
from motor import PWMMotor
|
||||||
|
|
||||||
|
|
||||||
|
MOTOR_PIN_A = 2
|
||||||
|
MOTOR_PIN_B = 3
|
||||||
|
PUMP_ON_TIME = 60
|
||||||
|
PUMP_SLEEP_TIME = 60 * 9
|
||||||
|
|
||||||
|
pump = PWMMotor(MOTOR_PIN_A, MOTOR_PIN_B)
|
||||||
|
|
||||||
|
print("Initialized motor. Beginning pump service.")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print(f"Staring pump for {PUMP_ON_TIME}")
|
||||||
|
pump.set_speed(1.0)
|
||||||
|
time.sleep(PUMP_ON_TIME)
|
||||||
|
print(f"Stopping pump and sleeping. {PUMP_SLEEP_TIME}")
|
||||||
|
pump.stop()
|
||||||
|
time.sleep(PUMP_SLEEP_TIME)
|
||||||
@ -0,0 +1,56 @@
|
|||||||
|
from machine import Pin, PWM
|
||||||
|
|
||||||
|
|
||||||
|
class PWMMotor:
|
||||||
|
def __init__(self, pin_ia, pin_ib, freq=1000, flip_dir=False):
|
||||||
|
"""
|
||||||
|
:param pin_ia: Pin connected to IA on L9110S
|
||||||
|
:param pin_ib: Pin connected to IB on L9110S
|
||||||
|
:param freq: PWM frequency (default 1000Hz)
|
||||||
|
:param flip_dir: Boolean to reverse logical forward/backward
|
||||||
|
"""
|
||||||
|
# Initialize PWM on both pins
|
||||||
|
self._pwm_a = PWM(Pin(pin_ia), freq=freq)
|
||||||
|
self._pwm_b = PWM(Pin(pin_ib), freq=freq)
|
||||||
|
|
||||||
|
# Start with motor off
|
||||||
|
self._pwm_a.duty_u16(0)
|
||||||
|
self._pwm_b.duty_u16(0)
|
||||||
|
|
||||||
|
self._flip_dir = flip_dir
|
||||||
|
self._speed = 0.0 # Range -1.0 to 1.0
|
||||||
|
|
||||||
|
def set_speed(self, speed):
|
||||||
|
"""
|
||||||
|
Sets motor speed and direction.
|
||||||
|
:param speed: Float between -1.0 (reverse) and 1.0 (forward)
|
||||||
|
"""
|
||||||
|
self._speed = max(-1.0, min(1.0, speed))
|
||||||
|
|
||||||
|
# Adjust for flip_dir preference
|
||||||
|
effective_speed = -self._speed if self._flip_dir else self._speed
|
||||||
|
|
||||||
|
# Convert 0.0-1.0 to 16-bit duty cycle (0-65535)
|
||||||
|
duty = int(abs(effective_speed) * 65535)
|
||||||
|
|
||||||
|
if effective_speed > 0:
|
||||||
|
# Forward: PWM on A, B held at 0
|
||||||
|
self._pwm_a.duty_u16(duty)
|
||||||
|
self._pwm_b.duty_u16(0)
|
||||||
|
elif effective_speed < 0:
|
||||||
|
# Reverse: B held at 0, PWM on B
|
||||||
|
self._pwm_a.duty_u16(0)
|
||||||
|
self._pwm_b.duty_u16(duty)
|
||||||
|
else:
|
||||||
|
# Stop
|
||||||
|
self.stop()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self._pwm_a.duty_u16(0)
|
||||||
|
self._pwm_b.duty_u16(0)
|
||||||
|
self._speed = 0.0
|
||||||
|
|
||||||
|
def deinit(self):
|
||||||
|
"""Release the PWM hardware"""
|
||||||
|
self._pwm_a.deinit()
|
||||||
|
self._pwm_b.deinit()
|
||||||
Loading…
Reference in New Issue