#include <ezButton.h>
#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1
int buttonPin = 4;
ezButton button(4); // create ezButton object that attach to pin 7;
int loopState = LOOP_STATE_STOPPED;
unsigned long lastMillis = 0;
int ledState = LOW;
int ledPin = 7;
int ledPin2 = 3;
unsigned long count = 0;
byte counter = 0;
void Blink() {
unsigned long currentMillis = millis();
if (currentMillis - lastMillis >= 250) {
// save the last time you blinked the LED
lastMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
counter ++;
Serial.println(counter);}
if (counter == 6){ledPin = 3;}
if (counter == 12){counter = 0; ledPin = 7; }
//if (counter == 12){}
}
void setup() {
/*******************
your setup code
*******************/
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600);
pinMode(buttonPin, INPUT);
button.setCountMode(COUNT_RISING);
button.setDebounceTime(10); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
//Get button press count and after 1 Press Reset count.
unsigned long count = button.getCount();
if (count >= 2){
button.resetCount();
}
//If button is pressed (NOT Released) Do Something
//and change button state to opposite the last state.
if (button.isReleased()) {
Serial.print("Pressed: ");
Serial.println(count);
if (loopState == LOOP_STATE_STOPPED) {
loopState = LOOP_STATE_STARTED;
}
else
{ loopState = LOOP_STATE_STOPPED;
Serial.println("Stop");
}
ledPin = 7;
ledPin2 = 3;
counter = 0;
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
//If Button pressed and loopstate set to true, Run
//Code in this IF looping untill button is pressed
//again. Like a Flip Flop.
if (loopState == LOOP_STATE_STARTED) {
/******************
your loop code
******************/
Blink();
}
}