Skip to content

Instantly share code, notes, and snippets.

@SLOLNE
Last active January 19, 2016 19:52
Show Gist options
  • Save SLOLNE/c9f461b36515665a450e to your computer and use it in GitHub Desktop.
Save SLOLNE/c9f461b36515665a450e to your computer and use it in GitHub Desktop.
Button Pressed (on and off)
// BLINKING LED on/off
//Camille Kauffman
//excercise 4-5 from Getting Started with Arduino
const int LED = 12; //the pin for the LED
const int BUTTON = 7;//the input pin whrtr the pushbutton is connected
int val = 0;//val will be used to store the state of the input pin
int old_val = 0;//this variable stores the previous value of "val"
int state = 0;// 0 = LED off and 1 = LED on
void setup() {
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT); // and button is an input
}
void loop(){
val = digitalRead(BUTTON); // read input value and store it yum, fresh?! lol
// check if there was a transition
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
delay(20);
}
old_val = val;//val is now old, let's store it
if (state == 1) {
digitalWrite(LED, HIGH); // turn LED ON
}else{
digitalWrite(LED, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment