Help understanding this code for. checking buttons

From this post

I saw this little snippet

byte button[] = {30, 26, 28, 32};
byte numberOfButtons = 4;
byte buttonState[numberOfButtons];  
byte activeButton[numberOfButtons];
boolean hit;

void setup() 
{
  for (byte i = 0; i < numberOfButtons; i++) 
  {
    pinMode(button[i], INPUT_PULLUP);  //Check for LOW for a button press
  }   
  Serial.begin(115200); 
}

void loop() 
{
  for (byte i = 0; i < numberOfButtons; i++) //read all buttons at beginning of loop()
  {  
    buttonState[i] = !digitalRead(button[i]);
  }
  
  for (byte i = 0; i < numberOfButtons; i++)
  {
    if (buttonState[i] && (!otherButtonPressed(i) || activeButton[i]))  //if no other buttons are active or this one is already active
    {
      activeButton[i] = true;
      Serial.print(i+1);
    }
    else
    {
      activeButton[i] = false;
    }
  }
}

boolean otherButtonPressed(byte currentButton)
{
  hit = 0;
  for (byte i = 0; i < numberOfButtons; i++)
  {
    if (i == currentButton)
    {
      continue; // skip test for button passed to function
    }
    if (activeButton[i] == HIGH)  // if any other button is currently the active one
    {
      hit = true;
    }
  }
  return hit;  //Were any other buttons already pressed?
}

Why does he use byte arrays? The first array ,button[] , is a set of integers so shoudnt it be an int array?
The he declares the number of buttons as a byte. Shouldnt it be an integer?
Then buttonState is a byte array but he sets button state to the output of digitalRead. I thought digitalRead returns high or low. Is that a boolean value?
Then the activeButton array is also declared as a byte array . But later in the code he sets it to true or false. Isnt that a boolean?

I am quite confused by the use of byte in the declaration of variables.

And in his main loop he has this statement:

if (buttonState[i] && (!otherButtonPressed(i) || activeButton[i])) //if no other buttons are active or this one is already active

How can he use the value of activeButton[i] before it is ever initialized or set?

Finally, dont I have to worry about 'debouncing' and add delays?

As you might imagine, I am trying to write code to efficiently monitor momentary button presses and happened across this code in my research.

byte is an 8 bit integer
int is a 16 bit integer with sign

uses hardcoded numbers that fit into an 8-bit variable. 8 bit = values between 0 and 255 (2^8 - 1)

digitalread results the number 0 or the number 1
code a
serial.println(digitalRead(2) );

"LOW" and "HIGH" are constants representing 0 and 1

"LOW" and "HIGH" are not an extra data-type

any expression inside an if-condition that results in a nonzero-value are seen as "true"

if(2) is the same as if(true)

if(100) is the same as if(true)

if(0) is the same as if(false)

unsigned long a = 11;
byte b = 10;

if( a - b)     // results in true  because the mathematical result is 1

global variables are initialised to zero automatically

best regards Stefan

Hello skypickle
Generaly spoken:
Sometimes it is helpful and useful to read, study and understand the Arduino instruction set. Aspecially to have a view to return values given.
eg
A digitalRead() returns an either a HIGH or LOW using the datatype INT.

In this way you can check the complete sketch by yourself.

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

p.s.
None published sketch is errorless.

please tell me what is the error inside this sketch ?

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Stefan, thank you. Clearly you see I am a beginner and I appreciate the time you took to help. Reminds me of an earlier internet when people were kind

certainly a confusing piece of code. could be clearer if avoiding multiple button presses

consider

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

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

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

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