Neater way to code these choices?

basic C, no C++

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

3 Likes

I can toss another idea onto the fire.

That is to use diode encoding. Eight positions need three input pins, up to sixteen with just four.

You may need a bit of help with the dets, but this shows the concept:

encoding methods for switches

Skip or read carefully down to

 Diode Matrix Encoders


HTH or at least amuses you.

a7

2 Likes

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.

Thanks, that’s clearer. Will still need some work, so it’s on my study list.

Thanks, interesting - but a lot of work in practice I think!

Got it, thank you.

break is another command I’d not used before, but now see this just stops further looping.

Good learning exercise, thanks.

Think of it as fun. :expressionless:

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:

resistor analog switch decoder

Your party, your rules!

a7

@alto777 wish i understood that reluctance, but we all have our...favorites, I guess.

No, I said “… I can afford the 12 pins”.

1 Like

My slip, my thanks were intended for post 20 by qubits-us.

Thanks, mis-read that eatlier. The most intuitive of all.

Why does it not need the break as in the almost identical code from qubits-us in post 6?

break is used to immediately exit a loop.

@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. :expressionless:

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.

@Delta_G also dropped a brace along the way

for (int i = 3; i <= 10; i++) {
  if(digitalRead(i) == LOW) {
    file = i - 1;
  }
}

a7

1 Like

Thanks, understood. And I also missed that AWOL brace; broke my usual habit of running the code before replying :wink:

I've also learnt another bit of syntax that's always rather puzzled me:

if (XYZ)
is the same as
if (XYZ == HIGH)

FYI, not sure if you're watching it, but the other thread I linked to has resolved to a single-liner. Very succinct.

technically no.
as others will mention, this works in this case because HIGH is defined as a non-zero value.

it's more relavant that if (XYZ) of if (! XYZ) are testing for non-zero or zero

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. :wink:

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.

Sorry, @alto777, that was for @Terrypin

almost always
those pins are the TX/RX pins used to program the processor as well as the serial monitor

you might consider @alto777 suggestion to reduce the # of pins used by the rotary switch

1 Like

Post 54 has a more correct version.

@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.

a7