Counting Blinks/Switch off when it's really off

Hi Community,

i have the next question. Got multiple buttons working with multiple clicks recognition - and also the blinking in a special frequency - BUT - it should only blink for a special time. I got a millis() query to get an interval but its switch sometime of when its still on and the LED keeps switched on.

So i need either a process which is looking "is something still on" or a process which is counting the blinks and i delete the millis() query. But i dont know how to get it working.

#include "ClickButton.h"

// Button Array
const int BlinkButtons = 2;
const int LichtButtons = 2;
const int HupeButton = 1;

// LED
const int BlinkLedPin[BlinkButtons] = { 7, 11 }; // Arduino pins to the LEDs
int BlinkLedState[BlinkButtons]     = { 0, 0 };
int BlinkLEDfunction[BlinkButtons]  = { 0, 0 };
unsigned long lastActionMillis[BlinkButtons] = { 0,0 };

// Input Pin
const int BlinkButtonPin1 = 14;
const int BlinkButtonPin2 = 19;

//Intervalle
unsigned long lastActionMillis2 = 0;
const long Interval2 = 3000;

// Instantiate ClickButton objects in an array
ClickButton BlinkButton[BlinkButtons] = {
  ClickButton (BlinkButtonPin1, LOW, CLICKBTN_PULLUP), // HABEN ALLE PULLUPS AM NANO?
  ClickButton (BlinkButtonPin2, LOW, CLICKBTN_PULLUP),
};

void setup()
{
  for (int i=0; i<BlinkButtons; i++)
  {
    pinMode(BlinkLedPin[i],OUTPUT);  
    BlinkButton[i].debounceTime   = 20;   // Debounce timer in ms
    BlinkButton[i].multiclickTime = 250;  // Time limit for multi clicks
    BlinkButton[i].longClickTime  = 1000; // Time until long clicks register
  }
}

void loop()
{
  for (int i=0; i<BlinkButtons; i++)
  {
    BlinkButton[i].Update();
    if (BlinkButton[i].clicks != 0) {      
      BlinkLEDfunction[i] = BlinkButton[i].clicks;
      lastActionMillis[i] = millis();     
    }
    if(BlinkButton[i].clicks == 1) { 
      BlinkLedState[i] = !BlinkLedState[i];
    }
    if(BlinkLEDfunction[i] == 2 && millis() - lastActionMillis[i] < Interval2) { /here it has to happen
      BlinkLedState[i] = millis() % (650*2) >= 650;  
    }
  }

  // update the LEDs
  for (int i=0; i<BlinkButtons; i++)
  {
    digitalWrite(BlinkLedPin[i],BlinkLedState[i]);
  }
}

Beste

Z

I got a millis() query to get an interval but its switch sometime of when its still on and the LED keeps switched on.

I think you mean that when the blinking period ends the LED is sometimes on and sometimes off. If so, why not turn the LED off (set BlinkLedState[­i] to off) at the end of the period

Of course! That makes sense! thanks a lot. sometimes im losing the sight of the wood for the trees.

Found the solution with:

    if(BlinkButton[i].clicks == 1) {
      
      BlinkLedState[i] = !BlinkLedState[i];
    }

    //if(BlinkLEDfunction[i] == 2 && millis() - lastActionMillis[i] < Interval2) { 
    if(BlinkLEDfunction[i] == 2) {
      
      if(millis() - lastActionMillis[i] < Interval2) {
      
        BlinkLedState[i] = millis() % (650*2) >= 650; 
        }

        else { BlinkLedState[i] = 0;
        }
        
    }

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.