1

Hello I have been working on a Bluetooth authorized push to start ignition in my truck. Right now its installed and working but I would like to change the function of the button and shorten the time it takes to shut off after I get out, maybe change it to shut down at low BT signal instead of no signal.

As it stands I can go get in my truck and when the BT rpi verifies that an authorized BT devices is within range (usually done by the time I get in), then I can press/hold the button to start my truck... which is fine but I think it could be better with nothing but better code.

This is the first program/code I have attempted and it works so that's cool but I don't know how and can't seen to understand the info online of how to implement my final touches.

I am currently running 2 different programs, Button.py and Key.py at boot up because I couldn't get it to function right as 1.

So to begin lets start with the button and its function, Button.py

As I stated above i can press/hold the button to start. Meaning however long I press or hold the button is how long the starter is engaged.

Button.py:


import RPi.GPIO as GPIO

# GPIO Setup
GPIO.setwarnings(False)
# GPIO Identification
GPIO.setmode(GPIO.BOARD)
Button = 40
On = 5
Start = 7
# GPIO Functions
GPIO.setup(Button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(On, GPIO.OUT)
GPIO.setup(Start, GPIO.OUT)

while True:
    if (GPIO.input(Button) == False):
                GPIO.output(On,0)
        GPIO.output(Start,0)

    else:
         GPIO.output(On,1)
         GPIO.output(Start,1)

On and start are relays I'm controlling with GPIO.

What I'd like is to have three different possible actions for the button.

1st press-On-turn on power to truck by making:

GPIO.output(On,0)
GPIO.output(Start,1)

2nd press-off-turn off power/kill truck by setting:

GPIO.output(On,1)
GPIO.output(Start,1)

3rd,4th,5th...would continuously cycle on off forever.

The third function would be to start the truck and would be done by holding instead of pressing the button(at any point in the on off cycle) and it would set:

GPIO.output(ON,0)
GPIO.output(Start,0)

Only while holding and the once released the starter would the cut off while leaving truck on going back to:

GPIO.output(On,0)
GPIO.output(Start,1)

BTW my relays are triggered low so the 1s are off and 0s are on.

To clarify I need help with interfacing with the button to do functions mentioned above. I read a button press counter could help to cycle through on/off but I would like to know how to implement it and I have no idea about identifying between press and hold to do different functions.

For now this is my main focus I'll worry about the auto kill time after i get this worked out so that at least I can manually kill the truck via button. Any help is appreciated!

my question is, how can i make button presses of a single button cycle through 2 funtions and also perform a third function by holding instead of just pressing?

Thanks!

3
  • upvote for updating the post ... you can delete the comment
    – jsotola
    Apr 30 at 18:29
  • 1
    use a variable to keep track of the state that your system is in ... do not call functions on button press ... call functions on the value of state variable ... measure how long the button is pressed ... if it is pressed a short time, then change state at button release ... if it is pressed longer, then change to third state at a timeout point, or at button release
    – jsotola
    Apr 30 at 18:33
  • i get what you are saying but i am very new to this so even though i can understand what needs to be done i have no idea how to implement it in the program im not asking any one to do it for me but if you or any one could point me in the directions to learn how to do each step of your suggestion that would be grand and greatly appriciated because ive done some searching and its all too new and still very complex for me to actually understand what is going on but im going to continue trying to learn it for my self but currently i need some major guidence thanks
    – Citsk6
    May 3 at 1:02
0

You might want to have a look at the GPIOzero library that has a hold_time option to achieve what you want.

https://gpiozero.readthedocs.io/en/latest/api_input.html#gpiozero.Button

from gpiozero import Button

def btn_handler():
    print("Long Hold")

button = Button(40, hold_time=2)

button.when_held = btn_handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.