Check if one of X Pins in an Array is activated

First of all: Hello everybody. Recently started doing stuff with Arduino again and really enjoying it so far.

Recently, I started a project where I use multiple magnetic sensors as input. For convenience, I have stored them in an Array like this:

int Magnet[5] = {A0,A1,A2,A3,A4};

Whenever one of those magnets is activated, I want to play a certain sound (I'll skip over how this is done because that works flawlessly):

if (digitalRead(A0) != LOW) {
//doSomething
}

Now I want to make a little riddle out of this. Imagine Simon Says, but with Sound Clues and a magnet you hold close to the corresponding sensor. To give the player a clue, I want there to be sort of an "experimentation phase", where holding the magnet close to any of the sensors will produce the sound.

This is where I am stuck: What would be the most effective way of checking which sensor is currently being addressed, if any? I thought about a Switch Case like this:

switch (Magnet[i] == LOW) {
 case 1: 
//etc
}

This doesn't work, sadly. Plus, I sorta feel that there ought to be a better way.
Any feedback is greatly appreciated!

Welcome to the forum

A for loop sounds like the obvious way to do this. Iterate through the pins in the array and if you find one that is active break out of the for loop and use the value of the for loop index to take action based on which one has been found to be active

Declare the for loop variable outside of the for loop so that its value is available to use after the break

Hi!

take a look of your formula:

your array stores the pin number, so you need to digitalRead the pin to get a value.
when you do:

you actually call the pin number (so Magnet[0] = A0) and not the value from the sensor.
You may use:

EDIT: thanks to @PaulRB digitalRead, no digitalWrite
digitalRead(Magnet[i]) == LOW;

instead

That's right - a for loop works perfectly here. Thank you very much!

I would suggest use of digitalRead() there, not digitalWrite().

ahaha! right, my mistake was obvious. I edit the initial post, thanks! :+1:

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);
    }
}