Button array

Hi,

I have 3 buttons in my array. My goal is when button 1 is pressed i see for example ‘button1’ in the serial monitor, if button 2 is pressed ‘button2’ … And I want to see this when the button state is PRESSED in the switch function. Only one button is working right now.
I dont know exactly where its going wrong, I hope some u can help me. Thanks for the help!

This is my code:

const int BUTTONLEFT = 2;
const int BUTTONMID = 3;
const int BUTTONRIGHT = 4;
const int BUTTONPINARRAY[] = {BUTTONLEFT, BUTTONMID, BUTTONRIGHT};
int SIZEARRAY = sizeof(BUTTONPINARRAY) / sizeof(BUTTONPINARRAY[0]);

int buttonleft = 0;
int buttonmid = 1;
int buttonright = 2;


unsigned long buttonCurrentMillis, buttonPreviousMillis;

const int RELEASED     = 0;
const int PRESSEDONCE  = 1;
const int PRESSED    = 2;
const int RELEASEDONCE = 3;

int buttonState, buttonEvent;

const int DELAYTIMEBUTTON = 50;

int counter;

void setup() {
  buttonSetup();
  counter = 0;
  Serial.begin(9600);
  buttonPreviousMillis = 0;
}

void loop() {
  buttonLoop();
  if(buttonCurrentMillis > buttonPreviousMillis + DELAYTIMEBUTTON)
  {
    buttonCurrentMillis = buttonPreviousMillis;
    buttonLoop();
  }
  if (getButtonState() == 1) {
    counter++;
    Serial.println(counter);
  }
}

 

void buttonSetup() {
  for(int button = 0; button < SIZEARRAY; button++)
  {
    pinMode(BUTTONPINARRAY[button], INPUT);
  }
  
    buttonState = RELEASED;
    buttonEvent = 0;
}

int getButtonState() {
  int returnValue;
  if (buttonEvent == 1) {
    returnValue = 1;
    buttonEvent = 0;
  }
  else {
    returnValue = 0;
  }
  return returnValue;
} 

void buttonLoop() {
    if (digitalRead(BUTTONPINARRAY[2,1,0])) {                // button released detected
      switch (buttonState) {
        case PRESSED: {   // previous pressed
          Serial.print("111111111111111111");
          buttonState = RELEASEDONCE;      // wait another cycle to be released
          break;
        }
        case RELEASEDONCE: {                       // previous release pressed
          buttonState = RELEASED;                // finally released
          break;
        }
        case PRESSEDONCE: {                        // previous pressed detected
          buttonState = RELEASED;              // now released, so bounce found
          break;
        }
        default:                                                // buttonState RELEASED stays RELEASED
          break;
      }
    } 
    else {                                                        // button pressed detected
      switch (buttonState) {
        case RELEASED: {                              // previous not pressed
          buttonState = PRESSEDONCE;       // wait another cycle to be pressed
          break;
        }
        case PRESSEDONCE: {                       // previous not pressed
          buttonState = PRESSED;                 // finally pressed
          buttonEvent= 1;
          break;
        }
        case RELEASEDONCE: {                   // previous release pressed
          buttonState = PRESSED;               // now pressed, so bounce found
          break;
        }
        default:                                            // buttonState PRESSED stays PRESSED 
          break;
      }
    }
  }

Gosh, that is very convoluted code for what you are trying to do. However what do you think this line will do? if (digitalRead(BUTTONPINARRAY[2,1,0])) {

Read the definition of digitalRead, it does not work like that.

You might be able to make use of a button library like this.

I used it some time ago and it worked well, and it will certainly make your code cleaner with providing the same functionality.

It has pressed, released, and long press functionality, as well as debouncing

Grumpy_Mike: Gosh, that is very convoluted code for what you are trying to do. However what do you think this line will do? if (digitalRead(BUTTONPINARRAY[2,1,0])) {

Read the definition of digitalRead, it does not work like that.

Thanks for your response. This code is for deboucing the button. Ofcourse my goal is not to get a println. When button one is pressed a certain event will happen, other event when button two is pressed and also another event when button three is pressed. And this event can only happen when the state is pressed, and it has to be the button thats been pressed.

I have looked into the digitalread function, I see what I did wrong, but I still dont know the solution to this problem.

I have looked into the digitalread function, I see what I did wrong, but I still dont know the solution to this problem.

To solve your problem you use the digitalRead function correctly. As it stands you are only reading one push button, to solve your problem you need to read all three.

Grumpy_Mike: To solve your problem you use the digitalRead function correctly. As it stands you are only reading one push button, to solve your problem you need to read all three.

I really dont e how to do this. U have any suggestions?

You have to read them one at a time, one after the other.

Something like:

boolean blnButton0 = digitalRead(0);
boolean blnButton1 = digitalRead(1);
boolean blnButton2 = digitalRead(2);

if(blnButton0 ...  whatever your code logic requires

Does that make sense? Maybe you didn’t understand how it was phrased.