# mPy - stepper motor control 28BYJ-48
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-04-10  initial release

import	time
from	machine		import	Pin

# =========================== hardware =============================

coil_a = Pin(14, Pin.OUT, value=0)
coil_b = Pin(27, Pin.OUT, value=0)
coil_c = Pin(26, Pin.OUT, value=0)
coil_d = Pin(25, Pin.OUT, value=0)

coils = [coil_a, coil_b, coil_c, coil_d]

# ============================= test ===============================

# activate coils a, b, c and d
def setCoils(a=False, b=False, c=False, d=False):
    coil_a(a)
    coil_b(b)
    coil_c(c)
    coil_d(d)
    
    
# do 4 full steps; as full 32 steps is 1 motor rev --> 1/8 rev
def fullStep4():
    setCoils(True, False, False, False)
    time.sleep_ms(4)
    setCoils(False, True, False, False)
    time.sleep_ms(4)
    setCoils(False, False, True, False)
    time.sleep_ms(4)
    setCoils(False, False, False, True)
    time.sleep_ms(4)
    
    
# do 8 half steps; as 64 half steps is 1 motor rev --> 1/8 rev
def halfStep8():
    setCoils(True,  False, False, False)
    time.sleep_ms(2)
    setCoils(True,  True,  False, False)
    time.sleep_ms(2)
    setCoils(False, True,  False, False)
    time.sleep_ms(2)
    setCoils(False, True,  True,  False)
    time.sleep_ms(2)
    setCoils(False, False, True,  False)
    time.sleep_ms(2)
    setCoils(False, False, True,  True)
    time.sleep_ms(2)
    setCoils(False, False, False, True)
    time.sleep_ms(2)
    setCoils(True,  False, False, True)
    time.sleep_ms(2)


# do 1 revolution using full steps. gearing 1:64
def test1():
    for i in range(8 * 64):
        fullStep4()				# 4 x full step
    setCoils()					# motor off
    
    
# do 1 revolution using half steps. gearing 1:64
def test2():
    for i in range(8 * 64):
        halfStep8()				# 8 * half step
    setCoils()					# motor off


test2()
    
# ================================ end =================================
