rajbuddy007
Posts: 43
Joined: Fri Apr 05, 2019 3:03 pm

Coding mechanical button presser - how to fix my code?

Fri May 24, 2019 12:55 pm

Hello Guys,

I have made a mechanical button presser circuit with stepper motor. I am so beginner in programming. My task is to press the button in different interval of time. So i need to write a code in which if we run it will ask the predefined function (ex Long press, 8 sec press, 2 sec press). How can i call the function by user input? any leads would be appreciated.

Andyroo
Posts: 2964
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: regarding code

Fri May 24, 2019 12:59 pm

There is a old piece of code viewtopic.php?p=846026#p846026 that you could use as a starting point for the button press...
Need Pi spray - these things are breeding in my house...

rajbuddy007
Posts: 43
Joined: Fri Apr 05, 2019 3:03 pm

Re: regarding code

Fri May 24, 2019 1:26 pm

Thanks for the reply but I already have my code. I just need to call various function by just manually giving the user input and then it calls that particular function. Could you help me with that?

User avatar
bensimmo
Posts: 3971
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: regarding code

Fri May 24, 2019 1:43 pm

did you read the linked thread? It's code that does stuff on different length presses (I think they are looking at similar for gpiozero module too)
Also you're asking for code, but already have the code

rajbuddy007
Posts: 43
Joined: Fri Apr 05, 2019 3:03 pm

Re: regarding code

Fri May 24, 2019 2:09 pm

I do have the code but i dont know how to modify it as per my requirements. In that code it is pressing the button simultaneously as per the code writte. I want my code to be user input. For ex If i run the code it should ask which function you have to run and if i put 1 press) then the code should call that function where i am using 1 time press

Code: Select all

from time import sleep    
import RPi.GPIO as GPIO    

DIR = 26   # Direction GPIO Pin    
STEP = 19  # Step GPIO Pin    
CW = 1     # Clockwise Rotation    
CCW = 0    # Counterclockwise Rotation    
SPR = 15   # Steps per Revolution (360 / 1.8)    

GPIO.setmode(GPIO.BCM)    
GPIO.setup(DIR, GPIO.OUT)    
GPIO.setup(STEP, GPIO.OUT)    
GPIO.output(DIR, CW)    

MODE = (14, 15, 18)   # Microstep Resolution GPIO Pins     
GPIO.setup(MODE, GPIO.OUT)    
RESOLUTION = {'Full': (0, 0, 0),    
              'Half': (1, 0, 0),    
              '1/4': (0, 1, 0),    
              '1/8': (1, 1, 0),    
              '1/16': (0, 0, 1),    
              '1/32': (1, 0, 1)}    

GPIO.output(MODE, RESOLUTION['Full'])    
step_count = SPR    
delay = .0208     

for x in range(step_count):    
    GPIO.output(DIR, CCW)    
    GPIO.output(STEP, GPIO.HIGH)    
    sleep(delay)    
    GPIO.output(STEP, GPIO.LOW)    
    sleep(delay)    

sleep(2)    
GPIO.output(DIR, CW)    
for x in range(step_count):    
    GPIO.output(STEP, GPIO.HIGH)    
    sleep(delay)    
    GPIO.output(STEP, GPIO.LOW)    
    sleep(delay)    

for x in range(step_count):    
    GPIO.output(DIR, CCW)    
    GPIO.output(STEP, GPIO.HIGH)    
    sleep(delay)    
    GPIO.output(STEP, GPIO.LOW)    
    sleep(delay)

sleep(8)    
GPIO.output(DIR, CW)     
for x in range(step_count):    
    GPIO.output(STEP, GPIO.HIGH)    
    sleep(delay)    
    GPIO.output(STEP, GPIO.LOW)    
    sleep(delay)    



GPIO.cleanup()
In my code i have included 2 intervals ( 2 sec and 8 sec). I want to call a single function at a time by giving user input.

pcmanbob
Posts: 5948
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Coding mechanical button presser - how to fix my code?

Fri May 24, 2019 2:51 pm

This is an example of taking user input for time to wait and then acting upon it.

Code: Select all

from time import sleep 
push_length = input(" enter value 2 or 8 ")
push_length =int(push_length)
print ("you entered ", push_length)
sleep(push_length)
print (" So I waited ", push_length, " seconds before printing this")
of course there is no check to see if the correct value has been entered but you could add that.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “General discussion”