Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stepper Motor key control
#1
Hello,
I'm writing a program to control a stepper motor using keyboard presses. The problem I'm having is that once I press a key to start the motor turning in one direction, I don't know how to interrupt the movement to either stop it or turn it in a different direction. I tried adding an if statement within one of the for loops but that did not work.

import curses
import time
import RPi.GPIO as GPIO

screen = curses.initscr()
curses.noecho() 
curses.cbreak()
screen.keypad(True)

GPIO.setmode(GPIO.BOARD)

ControlPin = [7,11,13,15]

for pin in ControlPin:
	GPIO.setup (pin, GPIO.OUT)
	GPIO.output (pin, 0)

seq2 = [[1,0,0,0],
	[1,0,0,0],
	[0,1,0,0],
	[0,1,0,0],
	[0,0,1,0],
	[0,0,1,0],
	[0,0,0,1],
	[0,0,0,1],
	[1,0,0,0],]

seq3 = [[1,0,0,0],
	[0,0,0,1],
	[0,0,0,1],
	[0,0,1,0],
	[0,0,1,0],
	[0,1,0,0],
	[0,1,0,0],
	[1,0,0,0],
	[1,0,0,0],]

try:
        while True:   
            char = screen.getch()
            if char == ord('q'):
                break
                        # right turning
            elif char == curses.KEY_RIGHT:
                for i in range(512):
                    print("right")
                    if char == curses.KEY_LEFT or char == 10:
                        break
                    for halfstep in range(9):
                        for pin in range(4):
                            GPIO.output(ControlPin[pin], seq2[halfstep][pin])
                        time.sleep(0.001)
                        
                        # left turning
            elif char == curses.KEY_LEFT:
                for i in range(512):
                    print("left")
                    if char == curses.KEY_RIGHT or char == 10:
                        break
                    for halfstep in range(9):
                        for pin in range(4):
                            GPIO.output(ControlPin[pin], seq3[halfstep][pin])
                        time.sleep(0.001)
                        
                        # stop
            elif char == 10:
                for i in range(512):
                    print("stop")
                    if char == curses.KEY_LEFT or char == curses.KEY_RIGHT:
                        break
                    for halfstep in range(9):
                        for pin in range(4):
                            GPIO.output(ControlPin[pin], seq2[halfstep][pin])
                        time.sleep(0.001)

        GPIO.cleanup()
except:
        GPIO.cleanup()
	print("error")
Reply


Messages In This Thread
Stepper Motor key control - by cel - Jul-24-2020, 04:53 AM
RE: Stepper Motor key control - by DPaul - Jul-24-2020, 06:28 AM
RE: Stepper Motor key control - by cel - Jul-25-2020, 12:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  micropython-stepper 1.0.3 trix 2 614 Nov-20-2023, 06:00 AM
Last Post: trix
  Stepper motor/Easy Driver/HELP Harney 1 1,897 Jul-31-2021, 08:05 AM
Last Post: Harney
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,990 Mar-21-2021, 05:17 PM
Last Post: yome
  stepper motor and servo key control cel 4 2,641 Jul-27-2020, 06:26 AM
Last Post: DPaul
  EOF error using stepper motor cel 1 1,726 Jul-24-2020, 01:27 PM
Last Post: Larz60+
  Control 2 stepper motor simultaneously jihene 2 4,044 May-08-2019, 05:27 PM
Last Post: DeaD_EyE
  How to use servo motor with TFMini Distance Sensor in python script? programerguy 1 3,258 Dec-04-2018, 04:57 PM
Last Post: Larz60+
  stepper motor with raspberry pi bowen73 3 4,637 Aug-15-2017, 04:46 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

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