About pins declaration need help please

Hi Everyone.
the example here: https://www.the-diy-life.com/multiple-push-buttons-on-one-arduino-input/
assign buttons as:

int input5Pin = 7;      
int input4Pin = 6;
int input3Pin = 5;
int input2Pin = 4;
int input1Pin = 3;

and used them as:

for(int i=3 ; i<=7 ; i++)
    checkPush (i);

my question is if my buttons wiring on discontinue numbers, such as: 2, 7, 9, 10...
how to use:

for(int i=3 ; i<=7 ; i++)
    checkPush (i);

kind of function?
Thanks
Adam

//Michael Klements
//The DIY Life
//17 May 2019 

int ledPin = 13; 	 // choose the pin for the LED
int input5Pin = 7;       // define push button input pins
int input4Pin = 6;
int input3Pin = 5;
int input2Pin = 4;
int input1Pin = 3;

void setup()
{
  pinMode(ledPin, OUTPUT);   // declare LED as output
  pinMode(input5Pin, INPUT); // declare push button inputs
  pinMode(input4Pin, INPUT);
  pinMode(input3Pin, INPUT);
  pinMode(input2Pin, INPUT);
  pinMode(input1Pin, INPUT);
}

void loop()
{
  for(int i=3 ; i<=7 ; i++)
    checkPush (i);
}

void checkPush(int pinNumber)
{
  int pushed = digitalRead(pinNumber);  // read input value
  if (pushed == HIGH) // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  else
    digitalWrite(ledPin, HIGH);  // turn LED ON
}

Put the pin numbers in an array and refer to them by the array index.

1 Like

A quick example of what I meant in post #2.

const byte pins[] = {2, 4, 5, 7, 10, 12};
bool states[sizeof(pins)];

void setup()
{
   Serial.begin(115200);
   for (byte n = 0; n < sizeof(pins); n++)
   {
      pinMode(pins[n], INPUT_PULLUP);
   }
}

void loop()
{
  static unsigned long timer = 0;
  unsigned long interval = 500;
  if(millis() - timer >= interval)
  {
   timer = millis();
   for(byte n = 0; n < sizeof(pins); n++)
   {
      states[n] = digitalRead(pins[n]);
      Serial.print("pin ");
      Serial.print(pins[n]);
      Serial.print(" = ");
      Serial.println(states[n]);
   }
   Serial.println();
  }
}

Output:

pin 2 = 0
pin 4 = 1
pin 5 = 1
pin 7 = 0
pin 10 = 1
pin 12 = 1

1 Like

Great!
Thank you groundFungus.

BTW. if I have:

void subA () {}
void subB () {}
void subC () {}
void subD () {}

may I definition:
void SUBs [] = {subA () , subB () . subC () . subD () };
and use SUNs [n] to pick a sub to use?

void (*functions[])(void) {subA, subB, subC, subD};
1 Like

Thank you TheMemberFormerlyKnownAsAWOL

One more question please.
I tested your code, got the expected output.

I added few lines for getting the acted pin number as below, didn't get the output expected, why?

const byte pins[] = {4, 5, 6, 7, 12};
bool states[sizeof(pins)];

int n = 0;
int M = 0;

void setup()
{
  Serial.begin(115200);
  for (byte n = 0; n < sizeof(pins); n++)
  {
    pinMode(pins[n], INPUT_PULLUP);
  }

  for (byte n = 0; n < sizeof(pins); n++)
  {
    digitalWrite(pins[n], LOW);
  }

}

void loop()
{
  static unsigned long timer = 0;
  unsigned long interval = 2000;
  if (millis() - timer >= interval)
  {
    timer = millis();
    for (byte n = 0; n < sizeof(pins); n++)
    {
      states[n] = digitalRead(pins[n]);
      Serial.print("pin ");
      Serial.print(pins[n]);
      Serial.print(" = ");
      Serial.println(states[n]);

       if (states[n] == 1)
      
        n = M;
    }
    Serial.println();

      Serial.print(" n= ");
      Serial.println(n);

      Serial.print("M = ");
      Serial.println(M);
  }
}

Serial Monitor:

23:50:05.113 -> pin 5 = 0
23:50:05.113 -> pin 6 = 0
23:50:05.113 -> pin 7 = 1
23:50:05.113 -> pin 5 = 0
23:50:05.113 -> pin 6 = 0
23:50:05.113 -> pin 7 = 1
23:50:05.113 -> pin 5 = 0
23:50:05.113 -> pin 6 = 0
23:50:05.113 -> pin 7 = 1
23:50:05.113 -> pin 5 = 0
23:50:05.113 -> pin 6 = 0
23:50:05.113 -> pin 7 = 0
23:50:05.113 -> pin 12 = 0
23:50:05.113 -> 
23:50:05.113 ->  n= 0
23:50:05.113 -> M = 0
23:50:06.604 -> pin 4 = 0
23:50:06.604 -> pin 5 = 0
23:50:06.604 -> pin 6 = 0
23:50:06.604 -> pin 7 = 0
23:50:06.604 -> pin 12 = 0
23:50:06.604 -> 
23:50:06.604 ->  n= 0
23:50:06.604 -> M = 0
23:50:08.608 -> pin 4 = 0
23:50:08.608 -> pin 5 = 0
23:50:08.608 -> pin 6 = 0
23:50:08.608 -> pin 7 = 0
23:50:08.608 -> pin 12 = 0
23:50:08.608 -> 

What did you expect?

The first for-loop in setup() enables the internal pull-ups; the second one disables them again. Why would you do that?

The variable n at the end of the loop is not the same variable n that you have declared as a global variable.

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