Command-Process isn't running in the right way

Hey girls and guys,

learning is going on but there are still some stumbling blocks. I got solution for several problems but now i don’t find any reason why it’s not working.

The command process in “2 clicks” is working. When i copy that from “2 clicks” to

BlinkLedState[i] = millis() % (650*2) >= 650;

to the section of “1 click”

//1 click

      if(BlinkButton[i].clicks == 1) {
         BlinkLedState[i] = millis() % (650*2) >= 650;
      }

It’s not working. Don’t know why. It’s only doing strange things. But when i see the code i don’t find any thing why that is like it is.

That is true

if(BlinkButton[i].clicks == 1)

so everything should run. But it isn’t…WHY?

Here also the whole code.

#include "ClickButton.h"

// Button Array
const int BlinkButtons = 2;

// 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 = 6000;

// 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()
{

//Blinkersektion

  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()
{

//Blinksection
  for (int i=0; i<BlinkButtons; i++) {
    BlinkButton[i].Update();

     if (BlinkButton[i].clicks != 0) {
        BlinkLEDfunction[i] = BlinkButton[i].clicks;
        lastActionMillis[i] = millis();
      }

//1 click

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

//2 clicks

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

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

Thanks a lot
Z

You should post a link to any libraries used if the library is not included with the IDE.

Oh of course!

library link --> ClickButton Library

This seems a very strange way to use millis().

BlinkLedState[i] = millis() % (650*2) >= 650;

what is it trying to do?

It won't work when millis() rolls over. The usual way to use millis() is like this

if (millis() - previousTime >= 650) {
   previousTime = millis();

which does work properly when millis() rolls over.

...R

Thanks for your answer. But this code is working right. I had another threat for it. its only one possibility to have a cycle oh 1,5Hz. It's working when its in section "2"

 if(BlinkLEDfunction[i] == 2) {
          BlinkLedState[i] = millis() % (650*2) >= 650;
          }

But not when i copy it to the section "1"

//1 click
      if(BlinkButton[i].clicks == 1) {
         BlinkLedState[i] = millis() % (650*2) >= 650;
      }

That was my question: Why is it working in one case but not in the second?

PS. rollover should be no problöem because modulo is deviding and giving the rest as result so it doesnt care about a rollover...

Found a solution even if the designer of the library wrote

// Simply toggle LED on single clicks
    // (Cant use BlinkLEDfunction like the others here,
    //  as it would toggle on and off all the time)
    if(button[i].clicks == 1) BlinkledState[i] = !BlinkledState[i];

i replaced the

BlinkButton[i].clicks == 1

with

BlinkLedFunction[i] == 1

and now it works - contrary to the expatations.

gnom:
Thanks for your answer. But this code is working right.

Perhaps you meant to write "hasn't gone wrong yet" :slight_smile:

...R

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