| 1 | /* |
| 2 | This program is free software: you can redistribute it and/or modify |
| 3 | it under the terms of the GNU Affero General Public License as |
| 4 | published by the Free Software Foundation, either version 3 of |
| 5 | the License, or (at your option) any later version. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU Affero General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Affero General Public License |
| 13 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | */ |
| 15 | |
| 16 | // pin 13 controls the arduino's built-in LED |
| 17 | int led_pin = 13; |
| 18 | // connect the button to pin 2 |
| 19 | int button_pin = 2; |
| 20 | // initially, the button is not pressed. |
| 21 | // 0 means not pressed, 1 means pressed |
| 22 | int button_value = 0; |
| 23 | |
| 24 | void setup(){ |
| 25 | Serial.begin(9600);} |
| 26 | |
| 27 | void loop(){ |
| 28 | // see if you can figure out how this logic works |
| 29 | if(digitalRead(button_pin) != button_value){ |
| 30 | Serial.println("button state changed!");} |
| 31 | // read the value of the button pin during this loop, |
| 32 | // and save it in a box named "button_value" |
| 33 | button_value = digitalRead(button_pin); |
| 34 | // write the value to the led pin |
| 35 | digitalWrite(led_pin, button_value);} |