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!
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