from machine import Pin, I2C
import time
import ssd1306
# ========== Pins ==========
IN1 = Pin(2, Pin.OUT)
IN2 = Pin(3, Pin.OUT)
IN3 = Pin(4, Pin.OUT)
IN4 = Pin(5, Pin.OUT)
LIMIT = Pin(6, Pin.IN, Pin.PULL_UP) # Limit switch (active LOW)
# ========== OLED ==========
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# ========== Stepper sequence for 28BYJ-48 ==========
seq = [
[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]
]
# Global position counter
position = 0
STEP_DELAY = 0.003 # Adjust speed (increase for slower movement)
# ========== Functions ==========
def step_motor(steps, direction=1):
global position
for _ in range(steps):
if direction == 1:
sequence = seq
else:
sequence = reversed(seq)
for s in sequence:
IN1.value(s[0])
IN2.value(s[1])
IN3.value(s[2])
IN4.value(s[3])
time.sleep(STEP_DELAY)
position += direction
oled.fill(0)
oled.text("Eye Scan Position", 0, 0)
oled.text("Step: {}".format(position), 0, 20)
oled.show()
def home():
global position
oled.fill(0)
oled.text("Homing...", 0, 0)
oled.show()
while LIMIT.value() == 1: # not pressed
step_motor(1, direction=-1)
oled.fill(0)
oled.text("Homed!", 0, 0)
oled.show()
time.sleep(1)
position = 0
# ========== Main ==========
home()
while True:
# Move step-by-step forward
step_motor(50, direction=1)
time.sleep(0.5)