Buttons serial print

hello , I'm trying to figure out press a button and the serial print will show me which color it is
I want it to be pressed as much time as i pressed no matter what color button i choose
what happens with my code is that if i press any color button the program just print it by repeat when I'm trying to press it once.
any help to solve the problem?
here is the code:

#define btn_length 5
char btns_Print[btn_length]={'Y','B','R','G','W'};

#define BtnPin_B 5
#define BtnPin_G 7
#define BtnPin_Y 8
#define BtnPin_R 6
#define BtnPin_W 11

unsigned long LastBtnChangeTime[btn_length];

int Btns[btn_length] = { BtnPin_Y ,BtnPin_B ,BtnPin_R ,BtnPin_G, BtnPin_W };
bool IsBtnPressed[btn_length];

void setup() {
Serial.begin(9600);

}

void loop() {
for (int k = 0; k < btn_length; k++) {
pinMode(Btns[k], INPUT_PULLUP);
IsBtnPressed[k] = false;
}

for(int k = 0; k < btn_length; k++)
{
if (!IsBtnPressed[k] && (digitalRead(Btns[k]) == LOW) && (millis() - LastBtnChangeTime[k] > 50)) {
IsBtnPressed[k] = true;

Serial.println(btns_Print[k]);
}

}

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

  for (int k = 0; k < btn_length; k++)
  {
    pinMode(Btns[k], INPUT_PULLUP);
    IsBtnPressed[k] = false;
  }

Why is this code in loop() ?

Why are you setting the LastBtnChangeTime only once in the sketch ?

Hello @alexvain31 ,
Questions similar to this are common on here so if you read some of the topics you'll find help. There are also lots of tutorials on reading buttons, here's mine: Buttons and other electro-mechanical inputs (introduction) , there are plenty of others.

Yes, i just agreed sorry

בתאריך שבת, 4 ביוני 2022 ב-19:06 מאת UKHeliBob via Arduino Forum <arduino@discoursemail.com>:

Hello alexvain31
Try this small example. The sketch uses a structured arry to collect all data.

BUTTON2TXT button2txts[] {
  {ButtonPins[Blue], "blue", 0, 20, false},
};

You have to add the information as need for your project simply and may change the port addresses of the ButtonPins.

constexpr byte ButtonPins[] {A0, A1, A2, A3, A4};
enum Colors {Blue, Green, Yellow, Red, White};
struct BUTTON2TXT {
  byte pin;
  String text;
  unsigned long stamp;
  unsigned long duration;
  int   currentState;
};
BUTTON2TXT button2txts[] {
  {ButtonPins[Blue], "blue", 0, 20, false},
};

void setup() {
  for (auto ButtonPin: ButtonPins) pinMode(ButtonPin, INPUT_PULLUP);
  Serial.begin(9600); 
}
void loop() {
  unsigned long currentTime = millis();
  for (auto &button2txt : button2txts) {
    if (currentTime - button2txt.stamp >= button2txt.duration) {
      button2txt.stamp = currentTime;
      int newState = !digitalRead(button2txt.pin);
      if (button2txt.currentState != newState) {
        button2txt.currentState = newState;
        if (newState) Serial.println( button2txt.text);
      }
    }
  }
}

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

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