Python Forum
Limit test for linear travel setup
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Limit test for linear travel setup
#1
This is something I did for a blade gauntlet on a costume. The code works:

#!/usr/bin/python

import cwiid
import RPi.GPIO as GPIO
import sys
from time import *
import subprocess
import board
import pygame
import pynput
import keyboard

from adafruit_motorkit import MotorKit
from adafruit_motor import stepper
kit1 = MotorKit()
# this is for stepper motors
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)
pygame.init()

# These defs are function definitions and are function calls
def add_print12(channel):
        print("BCM 12 hit")
        for i in range (200):
          kit1.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
def add_print13(channel):
        print("BCM 13 hit")
        for i in range (200):
          kit1.stepper1.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
def add_print27(channel):
        print("BCM 27 hit")
        for i in range (200):
          kit1.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
def add_print5(channel):
        print("BCM 5 hit")
        for i in range (200):
          kit1.stepper2.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)

try: 
  GPIO.setmode(GPIO.BCM);
  GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP);
  GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP);
  GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP);
  GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP);

while True:
# GPIO add event callback.  Callbacks are a fancy way to run a custom command set on detection of an event
#  if not 'event' in locals():
#    event = 
  GPIO.add_event_detect(12, GPIO.FALLING, callback=add_print12, bouncetime=300)
#    event = 
  GPIO.add_event_detect(13, GPIO.FALLING, callback=add_print13, bouncetime=300)
  if keyboard.is_pressed('f'):
#  if key == Key.f:   # this uses pynput
#  for event in pygame.event.get():
 #   if event.type == pygame.KEYDOWN:
  #    if event.key == pygame.K_f:
    for i in range (200):
      kit1.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)    
  for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_b:
        for i in range (200):
          kit1.stepper2.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)    
   
#  else:
  sleep(0.2)
finally:
  GPIO.cleanup()

#A05/TQ2.0.32141E1 Pt Vos, GLV Joker Tennessee USA 6/24/2017

#A04/TQ0.0.82139E1 Sgt Quinn, HQ HQ Tennessee USA 6/24/2017
# https://gpiozero.readthedocs.io/en/stable/recipes.html

from gpiozero import Button
button1 = Button(2)
button2 = Button(3)
button3 = Button(4)
button4 = Button(1)
while True:
  if button1.is_pressed:
    print("button1 pressed")
Reply
#2
Do you have a question? If not - I will move this to Code Review or Code Share section of the forum
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Apr-19-2021, 05:23 AM)buran Wrote: Do you have a question? If not - I will move this to Code Review or Code Share section of the forum

Ah, I did not see that section, please move there if you would be so kind. Thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020