So as the question says, after pressing a button 3 times the LED needs to blink 3 times. I have already wrote some code that if you press it once it blinks 3 times, but I don't really know yet how to make it for 3 clicks.
void loop() {
b = digitalRead(PIN_BUTTON_1);
if (b == LOW)
{
for (i = 1; i <= 3; i=i+1)
{
digitalWrite(PIN_LED_RED, HIGH);
delay(1000);
digitalWrite(PIN_LED_RED, LOW);
delay(1000);
}
}
}
Try this code
setting blinkCounter to >0 starts blinking that many times
//https://forum.arduino.cc/index.php?topic=733312.0
// RepeatingMillisDelay.ino
#include <millisDelay.h>
// from SafeString library from Arduino library manager
// see https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html
int led = 13;
// Pin 13 has an LED connected on most Arduino boards.
bool ledOn = false; // keep track of the led state
millisDelay ledDelay;
unsigned long ledDelay_ms = 500;
millisDelay restartLedBlink;
unsigned long restartLedBlink_ms = 6000ul; // each 10 sec blink 3 times
int blinkCounter = 0;
void setup() {
Serial.begin(9600);
for (int i = 10; i > 0; i--) {
Serial.print(' '); Serial.print(i);
delay(1000);
}
Serial.println();
// initialize the digital pin as an output.
pinMode(led, OUTPUT); // initialize the digital pin as an output.
digitalWrite(led, LOW); // turn led off
ledOn = false;
// start delay
ledDelay.start(ledDelay_ms);
blinkCounter = 3; // blink only 3 times
restartLedBlink.start(restartLedBlink_ms);
}
void ledBlink() {
// check if delay has timed out
if (ledDelay.justFinished()) {
ledDelay.repeat(); // start delay again without drift
if (blinkCounter <= 0) {
return; // nothing to do
}
// toggle the led
ledOn = !ledOn;
if (ledOn) {
digitalWrite(led, HIGH); // turn led on
} else {
digitalWrite(led, LOW); // turn led off
blinkCounter--;
}
}
}
void loop() {
ledBlink(); // call this every loop
// set counter to > 0 to start blinking
if (restartLedBlink.justFinished()) {
restartLedBlink.repeat();
Serial.println(F("restart blink"));
blinkCounter = 3;
}
}
The below code includes debouncing, blink without delay, and counting
#include <ezButton.h> // ezButton library
#include <ezOutput.h> // ezOutput library
ezButton button(7); // create ezButton object that attach to pin 7;
ezOutput led(9); // create ezOutput object that attach to pin 9;
unsigned long lastCount = 0;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
button.setCountMode(COUNT_FALLING);
}
void loop() {
button.loop(); // MUST call the loop() function first
led.loop(); // MUST call the led1.loop() function in loop()
unsigned long count = button.getCount();
if (count != lastCount) {
Serial.println(count);
if ((count % 3) == 0)
led.blink(1000, 1000, 0, 6); // 0 milliseconds OFF, 100 milliseconds ON, start immediately,
// since toggle times is 6. it changes: OFF -> ON, ON -> OFF 6 times <=> blink 3 time
lastCount = count;
}
}
I managed to make it work that if I press the button 3 times the LED turns on, but now I want the opposite. So if I press the button 3 times again the LED turns off.
[pre][code]void loop(){
if (digitalRead(PIN_BUTTON_1) == LOW)
{
buttonPresses++;
delay(250);
}
if (buttonPresses == 3)
{
for (int i = 0; i <= buttonPresses; i++)
{
digitalWrite(PIN_LED_RED, HIGH);
}
lastPressCount = buttonPresses;
}
}