this is a beginners question, I suppose. How can I write a python code that allows the use of 2 (or more) push buttons connected to the GPIO? Each button should start a different shell script. The buttons are connected to the 3.3V pin and through a resistor to a GPIO pin. Also, I think, de-bouncing is necessary for the plan. All the simple tutorials deal only with one button and the more complex projects have a lot of code I don't understand. I tried a bit of copy/pasting, but without success. Help would be appreciated. Bye SG
-
3Do the simplest project for one button. Get that working. Understand how it works. It will then be trivial to add another button/script.joan– joan2020年12月04日 11:19:09 +00:00Commented Dec 4, 2020 at 11:19
-
post your code and explain hiw it does not work as expectedjsotola– jsotola2020年12月04日 17:21:48 +00:00Commented Dec 4, 2020 at 17:21
1 Answer 1
Almost sounds like a homework issue :-) As such (and that this is not really a programming site) I would break the problem down as follows:
- Understand how to import the GPIO Zero, time and subprocess modules into your program
- Understand how to use the sys module to call to trigger the shell scripts AND report their return codes (assuming they do)
- Understand how to use the basic button function of GPIO Zero using the wait option. Test pull up / down, wait and bounce options. Check both switches work the way you want by testing each in its own program
- Create a basic loop in Python that waits till you press ctrl-c and end without an error message
- Change the loop with
sleep
so other things can run - Change one of the
button
functions to use the callback capability to action the call to the script - Change the final
button
function to use the callback to run the other script
Try not to use copy and paste (though I guess we all do) - it never teaches you the basics of coding or how to solve problems...
Done - voted close as the issue was way too wide ranging.