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
#2
For a stepper motor you need to know how many steps there are in 360 degrees, for simplicity say 360 steps.
(You may have one that does only 90 degrees or.180...)
You don't stop a stepper motor, you call your forward or backward routine with a number of steps, say 90.
It should make a quarter turn then.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Thank you DPaul for the advice and I fixed the problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  micropython-stepper 1.0.3 trix 2 554 Nov-20-2023, 06:00 AM
Last Post: trix
  Stepper motor/Easy Driver/HELP Harney 1 1,852 Jul-31-2021, 08:05 AM
Last Post: Harney
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,938 Mar-21-2021, 05:17 PM
Last Post: yome
  stepper motor and servo key control cel 4 2,551 Jul-27-2020, 06:26 AM
Last Post: DPaul
  EOF error using stepper motor cel 1 1,684 Jul-24-2020, 01:27 PM
Last Post: Larz60+
  Control 2 stepper motor simultaneously jihene 2 3,962 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,209 Dec-04-2018, 04:57 PM
Last Post: Larz60+
  stepper motor with raspberry pi bowen73 3 4,546 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