Python Forum
Halting if command if gpio triggered event triggered
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Halting if command if gpio triggered event triggered
#1
I am tweaking this code to get it ready for full integration into an animatronics costume setup. The individual pieces work. basically the GPIO triggers are limit switches. I am trying to build things such that if things slip or move that it will move forward if I hit the forward or backward button again and stop once the limit switch is triggered.

The way the libraries are it appears that it will stop as it is running through a for I range loop but will backup once that loop finishes if the gpio gets triggered.

I will keep researching and will probably sleep on things after I spend all evening researching different approaches on this conundrum. I just need to chew on things but more minds can help come up with unexpected solutions.

This is the code:
#!/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)
Reply


Messages In This Thread
Halting if command if gpio triggered event triggered - by knoxvilles_joker - Jun-13-2021, 03:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  GPIO Bug? Rchrd 6 1,304 Nov-17-2024, 10:46 PM
Last Post: Rchrd
  function return boolean based on GPIO pin reading caslor 2 2,188 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 1,862 Jan-30-2023, 08:05 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 2,043 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  How would I use Watchdog to get triggered when DVD is inserted? Daring_T 12 7,507 Aug-17-2021, 01:49 PM
Last Post: Daring_T
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 5,987 Feb-19-2021, 06:29 AM
Last Post: LouF
  Process halting JarredAwesome 0 2,193 Dec-24-2020, 11:46 PM
Last Post: JarredAwesome
  Picture changing triggered by GPIO q_nerk 2 3,538 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 3,311 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  raspberry pi tank gpio help jatgm1 1 3,482 May-06-2020, 09:00 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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