I have been working on a Bluetooth authorized push to start the ignition in my truck. Right now it's 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 a 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 device 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 seem to understand the info online on how to implement my final touches.
I am currently running 2 different programs,
Key.py at boot up because I couldn't get them to function right as 1.
So to begin let's start with the button and its function,
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.
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:
2nd press-off-turn off power/kill truck by setting:
3rd,4th,5th...would continuously cycle on and off forever.
The third function would be to start the truck which would be done by holding instead of pressing the button(at any point in the on-off cycle) and it would set:
Only while holding, the once released the starter would then cut off while leaving the truck on going back to:
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 functions and also perform a third function by holding instead of just pressing?