pins are defined in an array. the sizeof() macro used to determine the # of pins
loop() uses a for-loop to check each pin in the array. it uses an index, n, into the array to retrieve the pin value and uses digitalRead() to read the pin state.
if the pin state is LOW (presumably pulled low by your switch), it recognizes that that position is selected and setsfile to the index value, n. the value is printed
break terminates the for loop, no need to continue
I will eventually add more sounds, so all 12 wil be used.
I vaguely recalled recommendations against using pins 0 and 1, so started with 2; but I’d already written that as a simple if() to choose file 1. And pins 10, 11 and 12 already used.
You've said you are low on input pins, so something must give.
Either you do the 12 pole switch differently than one input per pole, or you move the hole thing to an Arduino with so many i/o pins you'd never run out no matter what you needed to do.
Looks like three possibilities
use more i/o pins
use one pin and a bunch of resistors
use four pins and a bunch of diodes
If I had four pins, I'd use the diode encoder. I've just never liked the analog one pin hack, but I must say having been participate on the other thread @camsysca linked, it looks like sometimes it could work well:
@qubits-us chose to use it. The loop is exited as soon as a pin is found to be LOW. The result is the number associated with first pin that was LOW.
@Delta_G chose to not, or just didn't, use it. That code will look at every pin and the result will be the number associated with last pin that was LOW.
Which, given the constraints of the wiring, are gonna be the same pin.
I wouldn't have thought of it.
Ppl use break for many things. Here it makes the difference I point out, and using break will be faster on average. Since we can exit the loop when we have an answer.
The speed issue is virtually negligible in this case. But break and the way it was used here is, or should be, a familiar pattern and technique, so the only objections - confusion or unreadability or whatever - are moot.
I couldn't find that one-liner in the 53 posts of the thread? Can you paste it please?
That's for curiosity/education only, as my simple code for the (already installed) 1P12W rotary switch and its soldered wiring is working fine.
And I still have pin A4 on the UNO left for further use.
P.S: And pins 0 & 1? When is it inadvisable to use those two? I'm using the libraries "SoftwareSerial.h" and "DFRobotDFPlayerMini.h" with pins 10 and 11 for Tx/Rx, so am I right that there's no problem with now using 0 and 1 too?
Post 53. He wrapped it in a function call, but that map() statement is the whole enchilada. The preceding 5-10 posts must be absorbed in order to comprehend how tidy the solution is.
@Terrypin I apologize, I assumed you had made a typographical error and did mean you were low on pins.
But now you are! Haha.
So… when/if the time comes and you need to, keep in mind the solutions that use but 4 or even only 1 pin.
Switching (see what I did there?) shouldn't be that big a deal, although you are correct either alternate method involves parts and soldering and stuff.