james15315
Posts: 1
Joined: Thu Jan 03, 2019 9:25 pm

Motor control

Thu Jan 03, 2019 9:36 pm

Hi everyone. Not really sure how this whole forum thing works but I have a motor shield made by sb components for my raspberry pi. I am a novice programmer and have been searching the web to try and find help with making a simple code for my raspberry pi to be able to control a motor. Could anyone help me?

drgeoff
Posts: 10109
Joined: Wed Jan 25, 2012 6:39 pm

Re: Motor control

Thu Jan 03, 2019 10:58 pm

Did you try putting "sb components motor shield" into a search engine? I did that and clicked on https://sb-components.co.uk/motor-shield.html. Then looked at that page. Guess what is at the bottom.

joep6120
Posts: 1
Joined: Sun Mar 13, 2016 1:04 pm

Re: Motor control

Sun Sep 01, 2019 10:55 pm

i have this motor control hat as well and have downloaded the sample codes from github but for some reason the stepper motor code won't work
this is the code:
import PiMotor
import time
import RPi.GPIO as GPIO

m1 = PiMotor.Stepper("STEPPER1")

# Rotate Stepper 1 Contiously in forward/backward direction
while True:
m1.forward(0.1,10) # Delay and rotations
time.sleep(2)
m1.backward(0.1,10)
time.sleep(2)

except KeyboardInterrupt:
GPIO.cleanup()

this is the error i get:%Run Stepper_Test.py
Traceback (most recent call last):
File "/home/pi/Downloads/MotorShield-master/Stepper_Test.py", line 14
except KeyboardInterrupt:
^
SyntaxError: invalid syntax

can someone tell my whats wrong as i am new to this programming stuff :?

markkuk
Posts: 149
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

Re: Motor control

Mon Sep 02, 2019 5:41 am

joep6120 wrote:
Sun Sep 01, 2019 10:55 pm
this is the error i get:%Run Stepper_Test.py
Traceback (most recent call last):
File "/home/pi/Downloads/MotorShield-master/Stepper_Test.py", line 14
except KeyboardInterrupt:
^
SyntaxError: invalid syntax
You don't have a "try" keyword before "except". See the Python tutorial on exception handling: https://docs.python.org/3.7/tutorial/er ... exceptions
The code should look like this:

Code: Select all

# Rotate Stepper 1 Contiously in forward/backward direction
try:
  while True:
    m1.forward(0.1,10) # Delay and rotations
    time.sleep(2)
    m1.backward(0.1,10)
    time.sleep(2)

except KeyboardInterrupt:
  GPIO.cleanup()

Return to “Beginners”