function not changing variable

I have a problem where a function doesn’t change a variable. This is the code:

void press(boolean state){
buttonState = digitalRead(BUTTON_PIN8);
if(state == pressed || (millis() - lastPressed <= debounceTime)){
if(buttonState != HIGH){
Serial.println(“button is pressed”);
mode++;
}
return; // Nothing to see here, folks
}

lastPressed = millis();

state ? Keyboard.press(key) : Keyboard.release(key);
pressed = state;
}

There’s a lot more code, but the code basically checks if one of the 8 buttons i have is pressed, and there after check if it is a specific one of the 8.

It all works as far as the Serial.println. It prints it, but after it just seems that it just stops and doesn’t change the variable.

I have tried changing the code so it says: mode = mode + 1;
Still doesn’t work.

No clue about what variable you are talking about and where it comes from…

you have a return, so don’t expect anything else from the function code to happen if you reach that statement (including lastPressed = millis();)
—————
Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

and Don’t post snippets (Snippets R Us!)

int mode = 1;

const int led1 = 10;
const int led2 = 16;
const int led3 = 14;

bool buttonState = 0;

// ---------------------------------
// Key definitions
#define BUTTON_KEY1 KEY_F13
#define BUTTON_KEY2 KEY_F14
#define BUTTON_KEY3 KEY_F15
#define BUTTON_KEY4 KEY_F16
#define BUTTON_KEY5 KEY_F17
#define BUTTON_KEY6 KEY_F18
#define BUTTON_KEY7 KEY_F19 
#define BUTTON_KEY8 KEY_F20
 
// Pin definitions
#define BUTTON_PIN1 2
#define BUTTON_PIN2 3
#define BUTTON_PIN3 4
#define BUTTON_PIN4 5
#define BUTTON_PIN5 6
#define BUTTON_PIN6 7
#define BUTTON_PIN7 8
#define BUTTON_PIN8 9
// ---------------------------------
 
#include "Keyboard.h"
 
// Button helper class for handling press/release and debouncing
class button {
  public:
  const char key;
  const uint8_t pin;
 
  button(uint8_t k, uint8_t p) : key(k), pin(p){}
  
  void press(boolean state){
    buttonState = digitalRead(BUTTON_PIN8);
    if(state == pressed || (millis() - lastPressed  <= debounceTime)){
      if(buttonState != HIGH){
        Serial.println("button is pressed");
        mode++;
      }
      return; // Nothing to see here, folks
    }
 
    lastPressed = millis();
 
    state ? Keyboard.press(key) : Keyboard.release(key);    
    pressed = state;
  }
 
  void update(){
    press(!digitalRead(pin));
  }
 
  private:
  const long debounceTime = 30;
  unsigned long lastPressed;
  boolean pressed = 0;
} ;
 
// Button objects, organized in array
button buttons[] = {
  {BUTTON_KEY1, BUTTON_PIN1},
  {BUTTON_KEY2, BUTTON_PIN2},
  {BUTTON_KEY3, BUTTON_PIN3},
  {BUTTON_KEY4, BUTTON_PIN4},
  {BUTTON_KEY5, BUTTON_PIN5},
  {BUTTON_KEY6, BUTTON_PIN6},
  {BUTTON_KEY7, BUTTON_PIN7},
  {BUTTON_KEY8, BUTTON_PIN8},
};
 
const uint8_t NumButtons = sizeof(buttons) / sizeof(button);
const uint8_t ledPin = 17;
 
void setup() { 
  // Safety check. Ground pin #1 (RX) to cancel keyboard inputs.
  pinMode(1, INPUT_PULLUP);
  if(!digitalRead(1)){
    failsafe();
  }
 
  // Set LEDs Off. Active low.
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  TXLED0;
 
  for(int i = 0; i < NumButtons; i++){
    pinMode(buttons[i].pin, INPUT_PULLUP);
  }

  Serial.begin(9600);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}
 
void loop() {
  for(int i = 0; i < NumButtons; i++){
    buttons[i].update();
  }
  
  sort();
}

void sort() {
    if(mode = 1){
      Serial.println(mode);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
    }else if(mode = 2){
      Serial.println(mode);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
    }else if(mode = 3){
      Serial.println(mode);
      digitalWrite(led3, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led1, LOW);
    }else if(mode = 4){
      mode = 1;
      Serial.println("mode reset to 1");
    }
}
 
void failsafe(){
  for(;;){} // Just going to hang out here for awhile :D
}

Here is the full code, hope it helps.

oops - Here is at least a bug, hope it helps.

  if (mode = 1) {
...
  } else if (mode = 2) {
...
  } else if (mode = 3) {
...

Your debounce logic looks majorly flawed with the inclusion of state == pressed, not to mention incrementing mode when you are debouncing ?

The whole idea of debounce is to ignore any state changes of the button until it has stopped bouncing. So I’ve really no idea what that’s supposed to do or work?

But without seeing the rest of your code it’s impossible to say.

state ? Keyboard.press(key) : Keyboard.release(key);

You're just throwing the result of this away instead of assigning it to something useful.

Steve

slipstick:

state ? Keyboard.press(key) : Keyboard.release(key);

You're just throwing the result of this away instead of assigning it to something useful.

This is not the typical use of the ternary operator - one would usually prefer

if (state) Keyboard.press(key); else Keyboard.release(key);

but it is still a legit C++ construct.

I guess OP does not care about the return value, the correct Keyboard action is still executed (and possibly failing which would not be captured)

You're right of course.

I really hate that ternary operator. In future I'll just ignore any problem that contains it.

Steve