2

I have a setup in which I have a motor turning a 5cm diameter shaft at about 1 revolution a second. I need to stop the motor after a predetermined number of revolutions - lets say 10 for now.

The sensor mechanism I am using is simply a magnet and reed switch. The following script works well to record each time the switch is triggered.

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
button1=22
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while(1):
        if GPIO.input(button1)==0:
                print "Button 1 Pressed"
                sleep(0.5)

Whilst this script runs the motor -

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor1A = 19
Motor1B = 21
Motor1E = 23

GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)

print "Going forwards"
GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor1E,GPIO.HIGH)

GPIO.cleanup()

In a nutshell then what I am seeking is a combined script which counts the number of event inputs on pin 22 and then turns pin 23 (the motor enable pin) to LOW on the count of 10.

Many thanks

Nick

3
  • All that has been posted is a program description. However, we need you to ask a question. We can't be sure what you want from us. Please edit your post to include a valid question that we can answer. Reminder: make sure you know what is on-topic here; asking us to write the program for you, suggestions, and external links are off-topic. Commented Jan 1, 2019 at 12:26
  • You have two working programs - what hinders you to use a variable to store the loop-count and increment it inside a loop until it reaches 10 and then turn off the motor? Questions that ask "please help me" tend to be looking for highly localized guidance, or in some cases, ongoing or private assistance, which is not suited to our Q&A format. It is also rather vague, and is better replaced with a more specific question. Please read Why is “Can someone help me?” not an actual question?. Commented Jan 1, 2019 at 12:28
  • Thanks. Points noted. I have actually just posted another question which attempt to break the problem down - [link]stackoverflow.com/questions/53995417/… Commented Jan 1, 2019 at 12:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.