buttons.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * buttons.c:
  3. * Read the Gertboard buttons. Each one will act as an on/off
  4. * tiggle switch for 3 different LEDs
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <stdio.h>
  26. #include <wiringPi.h>
  27. // Array to keep track of our LEDs
  28. int leds [] = { 0, 0, 0 } ;
  29. // scanButton:
  30. // See if a button is pushed, if so, then flip that LED and
  31. // wait for the button to be let-go
  32. void scanButton (int button)
  33. {
  34. if (digitalRead (button) == HIGH) // Low is pushed
  35. return ;
  36. leds [button] ^= 1 ; // Invert state
  37. digitalWrite (4 + button, leds [button]) ;
  38. while (digitalRead (button) == LOW) // Wait for release
  39. delay (10) ;
  40. }
  41. int main (void)
  42. {
  43. int i ;
  44. printf ("Raspberry Pi Gertboard Button Test\n") ;
  45. wiringPiSetup () ;
  46. // Setup the outputs:
  47. // Pins 3, 4, 5, 6 and 7 output:
  48. // We're not using 3 or 4, but make sure they're off anyway
  49. // (Using same hardware config as blink12.c)
  50. for (i = 3 ; i < 8 ; ++i)
  51. {
  52. pinMode (i, OUTPUT) ;
  53. digitalWrite (i, 0) ;
  54. }
  55. // Setup the inputs
  56. for (i = 0 ; i < 3 ; ++i)
  57. {
  58. pinMode (i, INPUT) ;
  59. pullUpDnControl (i, PUD_UP) ;
  60. leds [i] = 0 ;
  61. }
  62. for (;;)
  63. {
  64. for (i = 0 ; i < 3 ; ++i)
  65. scanButton (i) ;
  66. delay (1) ;
  67. }
  68. }