Setup with an array


I am trying to do a project with an ultrasonic sensor. For this I´m using some new programming techniques, like shortening the setup with an array and a loop. But somehow I dont get it to work. I´ve already tried different type of loops but it won´t working. Hopefully you have an idea what could be wrong.

Thank you


const byte calPin = 7;
const byte buzzPin = 6; const byte trigPin = 9; const byte ledPin = 13;
const byte echoPin = 10; const byte gasPin = 11;

const int ButtonPin[] {calPin};
const int OutputPin[] {buzzPin, trigPin, ledPin};
const int InputPin[] {echoPin, gasPin};


void setup() {
  byte i = 0;
  while (ButtonPin[i] != 0 && OutputPin[i] != 0 && InputPin[i] != 0) //It doesn´t work with a for loop as well
    pinMode(ButtonPin[i], INPUT_PULLUP);
    pinMode(OutputPin[i], OUTPUT);
    pinMode(InputPin[i], INPUT);

  /* With the normal pin variant it works
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);


int measureDistance() {
  digitalWrite(trigPin, LOW); condition
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  int distance = (duration * 0.034 / 2);
  return distance;


void loop() {
while (ButtonPin[i] != 0 && OutputPin[i] != 0 && InputPin[i] != 0)

ButtonPin has one element, OutputPin has three elements and InputPin has two.

That's not an easy combination to work with just using the same subscript variable.

Also, none of them has an element with a zero value, so that makes things even harder. @PieterP beat me to the edit.

When will ButtonPin[i] ever be 0? You're just going to read out of bounds of the arrays.
If the arrays have different lengths, you need different loops (or additional logic).

Try this:

for (auto pin : ButtonPin)
    pinMode(pin, INPUT_PULLUP);
for (auto pin : OutputPin)
    pinMode(pin, OUTPUT);
for (auto pin : InputPin)
    pinMode(pin, INPUT);

I have thought, that the array will be 0 if no Pin is declared in it. Or what´s the state the array has?

Each array will be as big as it needs to be.
The only arrays that are automatically zero-terminated are strings.

const int InputPin[] {echoPin, gasPin};

This array contains 2 numbers (10 and 11), nothing more, nothing less.

If you try to read the nonexistent third element, your program is invalid.

Could the last element be 0 if the code looks like this?

const int ButtonPin[3] {calPin};
const int OutputPin[3] {buzzPin, trigPin, ledPin};
const int InputPin[3] {echoPin, gasPin};

Not OutputPin.
You (and the compiler) know the sizes of the arrays. Why do you need 0 as a sentinel?

Because I tried to put everything into one loop. But as it looks with different array sizes it won´t be possible. Or do you have any idea?

Why? Just use 3 loops:


const byte ButtonPin[] {calPin};
const byte OutputPin[] {buzzPin, trigPin, ledPin};
const byte InputPin[] {echoPin, gasPin};

Thank you very much for your kind help. I will stick with your solutions.

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