chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / examples / Gertboard / buttons.c
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
26 #include <stdio.h>
27 #include <wiringPi.h>
28
29 // Array to keep track of our LEDs
30
31 int leds [] = { 0, 0, 0 } ;
32
33 // scanButton:
34 //      See if a button is pushed, if so, then flip that LED and
35 //      wait for the button to be let-go
36
37 void scanButton (int button)
38 {
39   if (digitalRead (button) == HIGH)     // Low is pushed
40     return ;
41
42   leds [button] ^= 1 ; // Invert state
43   digitalWrite (4 + button, leds [button]) ;
44
45   while (digitalRead (button) == LOW)   // Wait for release
46     delay (10) ;
47 }
48
49 int main (void)
50 {
51   int i ;
52
53   printf ("Raspberry Pi Gertboard Button Test\n") ;
54
55   wiringPiSetup () ;
56
57 // Setup the outputs:
58 //      Pins 3, 4, 5, 6 and 7 output:
59 //      We're not using 3 or 4, but make sure they're off anyway
60 //      (Using same hardware config as blink12.c)
61
62   for (i = 3 ; i < 8 ; ++i)
63   {
64     pinMode      (i, OUTPUT) ;
65     digitalWrite (i, 0) ;
66   }
67
68 // Setup the inputs
69
70   for (i = 0 ; i < 3 ; ++i)
71   {
72     pinMode         (i, INPUT) ;
73     pullUpDnControl (i, PUD_UP) ;
74     leds [i] = 0 ;
75   }
76
77   for (;;)
78   {
79     for (i = 0 ; i < 3 ; ++i)
80       scanButton (i) ;
81     delay (1) ;
82   }
83 }