This is part of a main project I have going but the limit switch stepper motor travel setup is not something I have seen posted anywhere. Enjoy.
#!/usr/bin/python
import cwiid
import RPi.GPIO as GPIO
import sys
from time import *
import subprocess
import board
#this is for stepper motor controls, keyboard initiates movement, Button is limit switches
import keyboard
#gpiozero is the only library I have found that functions with the current pi and hardware setup I am using
from gpiozero import Button
#these are part of the Blinka libraries Adafruit has.
from adafruit_motorkit import MotorKit
from adafruit_motor import stepper
kit1 = MotorKit()
# this is for stepper motors and servo board
from board import SCL, SDA
import busio
from adafruit_pca9685 import PCA9685
i2c_bus = busio.I2C(SCL, SDA)
pca = PCA9685(i2c_bus)
pca.frequency = 50
# standard servo declearations for controls
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)
#12 and 13 are for one motion assembly backing up one full turn
button12 = Button(12)
button13 = Button(13)
#27 and 5 are for the other motion assembly
button27 = Button(27)
button5 = Button(5)
# while True loops endlessly. This can be run as a service using systemd or setup as an init.d script or as an rc.local script.
while True:
if button12.is_pressed:
print("button12 pressed")
for i in range (200):
kit1.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
if button13.is_pressed:
print("button13 pressed")
for i in range (200):
kit1.stepper1.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
if keyboard.is_pressed('f'):
print("f is pressed")
for i in range (200):
kit1.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
if keyboard.is_pressed('b'):
print("b is pressed")
for i in range (200):
kit1.stepper1.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
# This is for channel 2 stepper motor.
if button27.is_pressed:
print("button12 pressed")
for i in range (200):
kit1.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
if button5.is_pressed:
print("button13 pressed")
for i in range (200):
kit1.stepper2.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
if keyboard.is_pressed('r'):
print("f is pressed")
for i in range (200):
kit1.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
if keyboard.is_pressed('l'):
print("b is pressed")
for i in range (200):
kit1.stepper2.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)