# IMPORTS
from machine import Pin
import time
import network
import socket
# CONSTANTS
DIR_PIN_M1 = 1
STEP_PIN_M1 = 2
DIR_PIN_M2 = 4
STEP_PIN_M2 = 5
BUTTON_ONE_PIN = 7
BUTTON_TWO_PIN = 10
LED_PIN = 21
TIME_BETWEEN_STEPS = 0.005 # 5 ms
SSID = 'your_SSID' # Replace with your SSID
PASSWORD = 'your_password' # Replace with your network password
def connect_to_wlan(ssid, password):
wlan = network.WLAN(network.STA_IF) # Create a WLAN station object
wlan.active(True) # Activate the interface
if not wlan.isconnected():
print('Connecting to network...')
wlan.connect(ssid, password)
# Wait for connection with a timeout
timeout = 10 # seconds
start = time.time()
while not wlan.isconnected() and (time.time() - start) < timeout:
time.sleep(1)
if wlan.isconnected():
print('Network connected!')
print('IP address:', wlan.ifconfig()[0])
else:
print('Failed to connect to the network.')
def listen_for_data(port=12345):
# Create a socket
addr = socket.getaddrinfo('0.0.0.0', port)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('Listening on', addr)
while True:
cl, addr = s.accept()
print('Client connected from', addr)
data = cl.recv(1024) # Receive data (up to 1024 bytes)
cl.close()
return data.decode('utf-8')
class STEPPER:
def __init__(self, dir_pin, step_pin, steps_one_length=200):
self.dir_pin = Pin(dir_pin, Pin.OUT)
self.step_pin = Pin(step_pin, Pin.OUT)
self.steps_one_length = steps_one_length
self.current_pos = 0
self.step_pin.value(0) # Initial state
self.time_between_steps = TIME_BETWEEN_STEPS
def move_steps(self, steps):
for _ in range(int(steps)):
self.step_pin.value(1)
time.sleep(self.time_between_steps) # 5 ms * 200 = 1 second
self.step_pin.value(0)
time.sleep(self.time_between_steps) # Adding delay here to ensure a proper step pulse
def move_to_new_position(self, target):
if target < 0 or target > 100:
print("Invalid input! Please use numbers between 0% and 100%.")
return
# calculate the delta between current position and desired position
des_steps = (target / 100.0) * self.steps_one_length
diff = des_steps - self.current_pos
# check if target is current position
if diff == 0:
print("No Position change needed. Current position is the target position!")
return
# determine the direction and move
if diff > 0:
self.dir_pin.value(0)
print("MOVING DOWN!")
else:
self.dir_pin.value(1)
print("MOVING UP!")
diff = -diff # Make diff positive for the loop
self.move_steps(diff)
self.current_pos = des_steps # Update the current position to the new position
print("New Position:", self.current_pos)
def setup(self, B1=None, B2=None):
if B1 is None or B2 is None:
raise ValueError("Button pins B1 and B2 must be provided for setup.")
print("SETUP")
steps = 0
self.dir_pin.value(1)
while B1.value() == 1:
self.move_steps(1)
steps += 1
upper_limit = steps
print("Upper limit:", upper_limit)
steps = 0
self.dir_pin.value(0)
while B2.value() == 1:
self.move_steps(1)
steps += 1
lower_limit = steps - upper_limit
print("Lower limit:", lower_limit)
self.steps_one_length = steps
self.dir_pin.value(1)
self.move_steps(self.steps_one_length)
print("TOTAL STEPS:", self.steps_one_length)
def main():
# Setup
B1 = Pin(BUTTON_ONE_PIN, Pin.IN, Pin.PULL_UP)
B2 = Pin(BUTTON_TWO_PIN, Pin.IN, Pin.PULL_UP)
LED = Pin(LED_PIN, Pin.OUT)
stepper1 = STEPPER(DIR_PIN_M1, STEP_PIN_M1, steps_one_length=200)
stepper2 = STEPPER(DIR_PIN_M2, STEP_PIN_M2, steps_one_length=400)
#connect_to_wlan(SSID, PASSWORD)
stepper1.setup(B1, B2)
stepper2.setup(B1, B2)
# Main loop
while True:
try:
#received_str = listen_for_data()
#print('Received data:', received_str)
pos = int(input("Move to Position: "))
stepper1.move_to_new_position(pos)
stepper2.move_to_new_position(pos)
except ValueError:
print("Please enter a valid number between 0 and 100.")
time.sleep(0.1) # Delay to prevent excessive CPU usage
if __name__ == "__main__":
main()
Loading
pi-pico-w
pi-pico-w