Go Down

### Topic: Rotary Switch Colour Selector help needed (Read 2105 times)previous topic - next topic

#### pekkaa

#15
##### Oct 23, 2012, 09:15 pmLast Edit: Oct 23, 2012, 09:35 pm by pekkaa Reason: 1

I don't think a rotary switch is as easy as you think... it would need quite a lot of code and I bet there's no library for it.

A rotary encoder library with examples is probably easier for a novice to get working.

I don't think a rotary switch is as complicated as you think   I already provided guidelines for the sketch in reply #1. If the OP has any programming experience at all, he will probably write the code in the time it would take to find a suitable library for a rotary encoder.
(edit: typo)

#### pekkaa

#16
##### Oct 23, 2012, 09:32 pm

Well from a programming stand point, I think a seven level deep block of nested if-then-else statements evaluating if each pin is TRUE would be fairly simple.  However, it would take up quite a few lines by itself and be somewhat cumbersome if you ever had to debug it.   Furthermore it's perhaps the most compact ways to express the logic without getting into rather arcane tricks.

Why on earth would it need to be anything that complicated? A simple loop through the pins is enough, assuming he uses subsequent (is this the right word?) pins.

Code: [Select]
`byte colorNum = 0;for (byte pin=2; pin <= 8; pin++) {    if (digitalRead(pin) == LOW) {      colorNum = pin;      break;   }}if (colorNum) color = colorMap[colorNum];`

#### fungus

#17
##### Oct 23, 2012, 09:43 pm

Well from a programming stand point, I think a seven level deep block of nested if-then-else statements evaluating if each pin is TRUE would be fairly simple.  However, it would take up quite a few lines by itself and be somewhat cumbersome if you ever had to debug it.   Furthermore it's perhaps the most compact ways to express the logic without getting into rather arcane tricks.

Ever heard of the 'switch' statement?
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#### fungus

#18
##### Oct 23, 2012, 09:45 pm

I don't think a rotary switch is as complicated as you think   I already provided guidelines for the sketch in reply #1. If the OP has any programming experience at all, he will probably write the code in the time it would take to find a suitable library for a rotary encoder.
(edit: typo)

I agree.

Plus ... you won't have five clicks on the dial where nothing happens.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#### Far-seeker

#19
##### Oct 23, 2012, 10:27 pm

Ever heard of the 'switch' statement?

Yes, but a case-switch block involving seven cases, or even just six if one color is used as a default case, is going to be about the same size as a nested if-then-else block.

Go Up

Please enter a valid email to subscribe