Skip to content

Instantly share code, notes, and snippets.

@BraveTea
Last active January 24, 2017 08:10
Show Gist options
  • Save BraveTea/aca3fda1cca64b3432aaccf481756c36 to your computer and use it in GitHub Desktop.
Save BraveTea/aca3fda1cca64b3432aaccf481756c36 to your computer and use it in GitHub Desktop.
Code Library: B : Button-to-Switch
/* This piece of code changes a button to a switch
* Presses of the button switches between ON and OFF
*/
const int inPin = 2;
const int outPin = 6;
int state = HIGH;
int reading;
int previous = LOW;
long time = 0; //longs, because when measured in ms they quickly become to large for ints
long debounce = 200;
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce)
{
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}
@BraveTea
Copy link
Author

img_8468

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment