Advertisement
metalx1000

Raspberry Pi GPIO button press

May 29th, 2015
5,696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [[ $EUID -ne 0 ]]; then
  4.    echo "This script must be run as root"
  5.    echo "Trying to restart with sudo"
  6.    sudo $0 $@
  7.    exit
  8. fi
  9.  
  10. pin=23
  11.  
  12. #Enable GPIO23
  13. echo $pin > /sys/class/gpio/export
  14.  
  15. #Set GPIO23 as input
  16. echo in > /sys/class/gpio/gpio$pin/direction
  17.  
  18. clear
  19. b=0
  20. echo "Not Pressed"
  21.  
  22. while [ 1 ];
  23. do
  24.   v="$(cat /sys/class/gpio/gpio$pin/value)"
  25.   if [ "$b" -ne "$v" ]
  26.   then
  27.     clear
  28.     b=$v
  29.     if [ "$b" -eq "0" ]
  30.     then
  31.       echo "Not Pressed"
  32.     else
  33.       echo "Pressed"
  34.     fi
  35.   fi
  36.  
  37. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement