Skip to content

Instantly share code, notes, and snippets.

@BraveTea
Created January 23, 2017 15:34
Show Gist options
  • Save BraveTea/0593c357956208694055b69d971201f0 to your computer and use it in GitHub Desktop.
Save BraveTea/0593c357956208694055b69d971201f0 to your computer and use it in GitHub Desktop.
Code Library: B : Button-State-Counter
const int buttonPin = 2;
const int ledPin = 13;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else
{
Serial.println("off");
}
delay(500);
}
if (buttonPushCounter % 4 == 0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment