I can not get Array to work like I want it to

I have made a program to comunicate to my computer using Serial, I use the same syntax as the cvzone library (example: 0#1#5#5#), and I use multiple if to check my button states (I'm sure it can be more efficient, but that's not the point), and here's the problem, the first two are working, the last one is as well, but the third and fourth one are not working, they are not changing in the Serial data (all of them are wired in Pull Down).

// Librairies
// none

// Pin Assignement
// Keys
int CTRL  = 1;
int SHIFT = 2;
int ALT   = 3;
int SPACE = 4;
int _A    = 5;
int _B    = 6;
int _C    = 7;
int _D    = 8;
int _E    = 9;
int _F    = 10;
int _G    = 11;
int _H    = 12;
int _I    = 13;
int _J    = 14;
int _K    = 15;
int _L    = 16;
int _M    = 17;
int _N    = 18;
int _O    = 19;
int _P    = 20;
int _Q    = 21;
int _R    = 22;
int _S    = 23;
int _T    = 24;
int _U    = 25;
int _V    = 26;
int _W    = 27;
int _X    = 28;
int _Y    = 29;
int _Z    = 30;
// Buttons
#define BT0_p 49
#define BT1_p 50
#define BT2_p 51
#define BT3_p 52
#define BT4_p 53

// Variables
// Python Communication
int sendVals[5];
// Button States
bool BT0 = false;
bool BT1 = false;
bool BT2 = false;
bool BT3 = false;
bool BT4 = false;
// Macros
int Macro1 = _A;
int Macro2 = _B;
int Macro3 = _C;
int Macro4 = _D;
int Macro5 = _E;

void setup() {
  Serial.begin(9600);
  pinMode(BT0_p, INPUT);
  pinMode(BT1_p, INPUT);
  pinMode(BT2_p, INPUT);
  pinMode(BT3_p, INPUT);
  pinMode(BT4_p, INPUT);
}

void loop() {
  sendVals[0] = 0;
  sendVals[1] = 0;
  sendVals[2] = 0;
  sendVals[3] = 0;
  sendVals[4] = 0;
  BT0 = digitalRead(BT0_p);
  BT1 = digitalRead(BT1_p);
  BT2 = digitalRead(BT2_p);
  BT3 = digitalRead(BT3_p);
  BT4 = digitalRead(BT4_p);

  if (BT0) { sendVals[0] = Macro1; }
  if (BT1) { sendVals[1] = Macro2; }
  if (BT2) { sendVals[2] = Macro3; }
  if (BT3) { sendVals[3] = Macro4; }
  if (BT4) { sendVals[4] = Macro5; }
  
  Serial.print(sendVals[0]); Serial.print("#");
  Serial.print(sendVals[1]); Serial.print("#");
  Serial.print(sendVals[2]); Serial.print("#");
  Serial.print(sendVals[3]); Serial.print("#");
  Serial.print(sendVals[4]); Serial.println("#");
}

(Underscores before letter are to not mix them with internal arduino things in case there is)
To be more precise, sendVals[2] and sendVals[3] are not working.

digitalRead() function returns int-type value; so, above initialization should be as follows:

int BT0 = 0, BT1 = 0, BT2 = 0, BT3 = 0, BT4 = 0;

Your report seems odd.

Can you print the values of BT0..BT4 right after you do the digitalRead on the buttons?

And say all what means your switches are wired pull down. It looks like 3 are working and 2 are not…

a7

How are the required pull-up or pull-down resistors wired? If you don't have them, your input pins are floating and will produce random results.

OP says that he has pull-down resistors with the buttons.

It seems the program is OK.
Are the buttons OK?
Could you swap the working ones with the malfunctioning ones?
Or maybe measure with volt meter?

Rather an overkill to save 5 yes/no high/low in 5 ints...
Could be saved in 1 byte....
But a simple cast would do to put it in 5 bools.
Without the cast (like it is now) it will work too, but according to some is undefined...

They are wired on the breadboard.

I did try that, still the same results.

Can you show us a picture of your breadboard?

You have a wiring issue or other problem.

Your code works, no surprise:

When you say

I did try that, still the same results.

Do you mean that the same buttons didn't work, or the same inputs? What exactly did you try?

a7

Quote from Arduino Reference Manual:

digitalRead

I know this by now from another thread. Hence I added the cast as an option to make you happy...
As an example:

bool isTouched = (bool)digitalRead(buttonpin);

But maybe it should be this:

bool isPressed = if(digitalRead(buttonPin==HIGH) ? true,  false);

If you think the cast itself is undefined...

1 Like

Same input didn't work, and the Burton worked on another input.

please show a schematic of your build
please show real pictures of your build - each component and each wire must be visible.

otherwise it's just guessing.


Resistors(330 Ohm) are wired Behring the pin (left of button) and to the gnd rail.
All red going to 5V.
Pins 49-53, buttons right to left.
And I'm using the 5V and GND pins from the side (where the pins 30-50 are)

Wiring looks OK, but some spots are difficult to see.

Another thing I previously overlooked.
What board do you use???
Most boards do not have pins with number 50 or higher....
If this is the problem, then it is surprising that some of the buttons do work...

I use a unofficial Arduino MEGA2560 Rev3 (I think it's Elegoo).

The above ternary/conditional operator based statement should stand as:

bool isPressed = (digitalRead(buttonPin)) ? true : false;
1 Like