Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. With your switch connected, let’s now look at how to read its state from Python. Start Python (as root so you can access the GPIO pins) with
  2.  
  3. sudo python
  4.  
  5. In your Python console:
  6.  
  7. import RPi.GPIO as GPIO
  8. GPIO.setmode(GPIO.BCM)
  9.  
  10. This will import the necessary libraries in the GPIO namespace and set the pin numbering to correspond to your breakout board.
  11.  
  12. Now we need to set the pin as input, pin 17 is used in this example.
  13.  
  14. GPIO.setup(17,GPIO.IN)
  15.  
  16. Reading the pin is now as easy as:
  17.  
  18. input = GPIO.input(17)
  19.  
  20. If we want to print “Button Pressed” each time a button is pressed (and assuming we’ve set up the switch so the pin goes high when pressed):
  21.  
  22. while True:
  23. if (GPIO.input(17)):
  24. print("Button Pressed")
  25.  
  26. Easy, right?
  27.  
  28. But wait… if you tried that, you probably noticed it printed many times for just a single press. This may sometimes be what you want if you’re monitoring something which changes state continuously, but for a button we’re probably only interested in seeing each press as one event.
  29.  
  30. This means we’re only interested when our switch changes from being low to being high, A little extra problem will be that this will actually happen several times in a very brief period for a button press, as the inside of the switch will act like a tiny spring. This is called bouncing.
  31.  
  32. import time
  33. #initialise a previous input variable to 0 (assume button not pressed last)
  34. prev_input = 0
  35. while True:
  36. #take a reading
  37. input = GPIO.input(17)
  38. #if the last reading was low and this one high, print
  39. if ((not prev_input) and input):
  40. print("Button pressed")
  41. #update previous input
  42. prev_input = input
  43. #slight pause to debounce
  44. time.sleep(0.05)
  45.  
  46. Now we’ll write a very simple script that will be started at boot and run a program (start.py) when the button is pressed. This example calls the script run.py.
  47.  
  48. import RPi.GPIO as GPIO
  49. import time
  50. import os
  51.  
  52. #adjust for where your switch is connected
  53. buttonPin = 17
  54. GPIO.setmode(GPIO.BCM)
  55. GPIO.setup(buttonPin,GPIO.IN)
  56.  
  57. while True:
  58. #assuming the script to call is long enough we can ignore bouncing
  59. if (GPIO.input(buttonPin)):
  60. #this is the script that will be called (as root)
  61. os.system("python /home/pi/start.py")
  62.  
  63. That’s it! Test it out by running it with sudo python run.py. If everything works as expected, let’s have it run at boot. It would be nice to have some indicator of if this program has yet run or called the script so if you’ve been though the Morse code tutorial (or had a look at the cheat sheet) you might like to add a status LED.
  64.  
  65. We need to edit /etc/rc.local (as root since this is the owner).
  66.  
  67. sudo nano /etc/rc.local
  68.  
  69. At the bottom, just above exit 0 we’ll add a call to our script.
  70.  
  71. python /home/pi/run.py
  72.  
  73. Great! Now every time you boot, you’ll have a listener program waiting that can start another program. This is good if you need a way to start your robot with no keyboard or mouse, but want it to wait before doing anything. This gives the advantage over waiting for a button press in the program itself that another press will reload the program after it quits (or crashes!).
  74.  
  75. Here’s an example of a run.py suitable for Pis with our shield on that flashes an LED while waiting for the button to be pressed.
  76. EXTENSION: Control the Physical World
  77.  
  78. Using our LED tutorial, set up a system where pressing a button changes the state of one or more LEDs. You could make it so a button behaves more like a toggle-switch, or set up a binary counter which counts up whenever the button is pressed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement