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

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Nick C
  • 79
  • 8
  • All that has been posted is a program description. However, we need you to [ask a question](//stackoverflow.com/help/how-to-ask). 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](//stackoverflow.com/help/on-topic); asking us to write the program for you, suggestions, and external links are off-topic. – Patrick Artner Jan 01 '19 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?](//meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). – Patrick Artner Jan 01 '19 at 12:28
  • Thanks. Points noted. I have actually just posted another question which attempt to break the problem down - [link]https://stackoverflow.com/questions/53995417/trigger-event-when-variable-equals-x-python?noredirect=1#comment94828201_53995417 – Nick C Jan 01 '19 at 12:35

0 Answers0