loop dosn't reflect Functions return val.

Hi

I have made a function for button, but it dosn’t do as expected.
as a Nano can’t do multi thred, i thought it would go to BTN function, and the react on the return val. , but it only works inttermittend.

It is not a big problem,i am just curious if someone now why?

Tested on Nano

#define __FILENAME__ strrchr("\\" __FILE__, '\\') + 1 //Remove PATH from filename

// Button States
#define CLICK  1
#define DOBBLECLICK 2
#define HOLD 3
#define SAME 0


void setup() {
  Serial.begin(115200);
  Serial.print(F("\r\nFilename: ")); Serial.println(__FILENAME__);
  Serial.print(F(" Compiled:  ")); Serial.print(__DATE__); Serial.print(F(" ")); Serial.println(__TIME__);
  pinMode(6, INPUT_PULLUP);
}

static int button = 6;

void loop() {
  if (BTN() ==  CLICK) {
    Serial.print(F(" Button State  ")); Serial.println("Clicked");
  }
  if (BTN() ==  DOBBLECLICK) {
    Serial.print(F(" Button State  ")); Serial.println("Dobble Clicked");
  }
  if (BTN() ==  HOLD) {
    Serial.print(F(" Button State  ")); Serial.println("Hold");
  }
}

int BTN() {
  unsigned long duration;
  volatile int RetVal = 0;
  if (!digitalRead(button)) {
  //  Serial.println(F(" Pressed: "));
    duration = millis();
    while (!digitalRead(button));
   // Serial.println(F(" Released: "));
    if (millis() - duration > 1000) {
      RetVal = 3; // Hold
    } else {
      bool dobbleClicked = false;
      do {
        if (!digitalRead(button) and !dobbleClicked) {
          while (!digitalRead(button));
          dobbleClicked = true;
          RetVal = 2; // Dobble Click
        }
      } while (millis() - duration < 500);
      if (!dobbleClicked ) {
        RetVal = 1; // Clicked
      }
    }
  }
  return RetVal;
}

if i change loop to this, it works.

void loop() {
  int x = BTN();
  if (x ==  CLICK) {
    Serial.print(F(" Button State  ")); Serial.println("Clicked");
  }
  if (x ==  DOBBLECLICK) {
    Serial.print(F(" Button State  ")); Serial.println("Dobble Clicked");
  }
  if (x ==  HOLD) {
    Serial.print(F(" Button State  ")); Serial.println("Hold");
  }
}

If you call the function repeatedly, it every time goes through all motions. So e.g. if you detect a double click, the first call to BTN() ( in if (BTN() == CLICK) ) will return the double click and will evaluate to false.
But a second call ( if (BTN() == DOBBLECLICK) ) will not unless you double-clicked fast enough :wink:

In the example that works, you only call button once so that result will be used in all if statements.

//Edit
PS: why do you assign numeric values to RetVal instead of the defined values like HOLD?

thanks sterrjte

Why didnt i see that ......

How should assig eg HOLD to RetVal without defining the function as char that would use more memory?

Hi sterrjte
if I change function to char BTN() , and the return values to txt, it consumes 22 bytes more

changed to switch

void loop() {
  switch (BTN()) {
    case   CLICK:       Serial.println("Clicked");        break;
    case   DOBBLECLICK: Serial.println("Dobble Clicked"); break;
    case   HOLD:        Serial.println("Hold");           break;
  }
}

I did not mention to change it to char.

This is what I had in mind for the BTN() function

int BTN() {
  unsigned long duration;
  volatile int RetVal = SAME;
  if (!digitalRead(button)) {
  //  Serial.println(F(" Pressed: "));
    duration = millis();
    while (!digitalRead(button));
   // Serial.println(F(" Released: "));
    if (millis() - duration > 1000) {
      RetVal = HOLD; // Hold
    } else {
      bool dobbleClicked = false;
      do {
        if (!digitalRead(button) and !dobbleClicked) {
          while (!digitalRead(button));
          dobbleClicked = true;
          RetVal = DOBBLECLICK; // Dobble Click
        }
      } while (millis() - duration < 500);
      if (!dobbleClicked ) {
        RetVal = CLICK; // Clicked
      }
    }
  }
  return RetVal;
}

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