I'm setting a variable using a 1P x 12W rotary switch. Some of the switch settings have an obvious relationship with the pins used. So is there a neater/smarter/more compact way to code the following please?
if (digitalRead(3) == LOW)
{
file = 2;
}
if (digitalRead(4) == LOW)
{
file = 3;
}
if (digitalRead(5) == LOW)
{
file = 4;
}
if (digitalRead(6) == LOW)
{
file = 5;
}
if (digitalRead(7) == LOW)
{
file = 6;
}
if (digitalRead(8) == LOW)
{
file = 7;
}
if (digitalRead(9) == LOW)
{
file = 8;
}
if (digitalRead(10) == LOW)
{
file = 9;
}
Is the resistor divider technique not of interest, or is the hardware already built? Your selections appear mutually exclusive, so using a single analog input would seem more 'frugal' than many digitals.
Will consider the several interesting alternative approaches for any future projects needing a single variable to be set at power-up. But for this one just want improve my code.
The project plays an MP3 file chosen before power-up, such as sea surf sound, babbling brook, etc, and fades it to silence over a user-settable duration. To set duration I'm using an analog approach: a 4K7 pot voltage to A5. But for 12 choices of sound file I thought it would be harder work getting the 12 ranges coded, and I can afford the 12 pins.
Also depends, of course, on which Arduino you're using. Some have sufficient analog in, some are scanty that way. The resistor solution is frugal only if you have analogs uncommitted.