Rotary Switch Colour Selector help needed

Hello all first off sorry if this is in the wrong Section I am new and thought this might be the best place if not Sorry admins and could you please move it to the Right Section thank you, now on to what I need help with

I have a 12 way 1 pole Rotary Switch which I am hoping to use as my selection changer for my blinkM I2C RGB LED I am using a Uno Board so you known what layout I have

now I will not need all 12 points on it but the colours I am setting to are in Hex value so I known them well thanks to my web site building days Below is the list of colours I am using.

I am not sure how to get the Switch to interact with the Arduino I am hoping I can use 1 or 2 pins for the input but as I am beginner at this I am not sure so any help you can offer would be most greatful

Colour List

  • Red FF0000
  • Green 00FF00
  • Blue 0000FF
  • Purple 8000FF
  • Orange FF8000
  • White FFFFFF
  • Random

Links to Items
BlinkMI2C LED
Rotary Switch Code FF73Q

You will need more than one or two pins.

One of the switch's pins is the center pole (or common terminal or something like that, I'm not sure what the correct English term is). When you rotate the switch, it connects the center pole with one of the 12 other terminals. Connect the center pole to the Arduino's ground and the other terminals (as many of them as you need) to different digital input pins, for example pins 2 to 8. In you sketch, put the pins to the input-mode and enable the internal pull-up resistors. Associate the colors with different pins, for example 2=red, 3=green, ... 8=random. Read the pin values. The pin that is LOW is the one that is selected.

It you use an analog input you can wire resistor between each switch position to make a multi-way voltage divider - the wiper terminal will then have a voltage that depends on the switch setting and can be read by analogRead () - the same way you would read a potentiometer (but with a fixed set of voltages, not continually varying).

MarkT:
It you use an analog input you can wire resistor between each switch position to make a multi-way voltage divider - the wiper terminal will then have a voltage that depends on the switch setting and can be read by analogRead () - the same way you would read a potentiometer (but with a fixed set of voltages, not continually varying).

your way is very confusing to me I am new to electronics so I think it would be best to do it with the first suggestion thank you though I am sure it makes sence just not to a noobie like me

Kobra299:
Hello all first off sorry if this is in the wrong Section I am new and thought this might be the best place if not Sorry admins and could you please move it to the Right Section thank you, now on to what I need help with

I have a 12 way 1 pole Rotary Switch which I am hoping to use as my selection changer for my blinkM I2C RGB LED I am using a Uno Board so you known what layout I have

A switch like that needs an awful lot of Arduino pins to connect it (12 of them).

If pins are important you'd be much better off with a "rotary encoder". This will do the same thing but only needs two pins for the shaft rotation. You also get a built-in push button (push the shaft down) which you can connect to a third pin.

fungus:
A switch like that needs an awful lot of Arduino pins to connect it (12 of them).

If pins are important you'd be much better off with a "rotary encoder". This will do the same thing but only needs two pins for the shaft rotation. You also get a built-in push button (push the shaft down) which you can connect to a third pin.

thank you that is what I need I was not sure now I known just got to work out how to do the data from that to it now

fungus:
A switch like that needs an awful lot of Arduino pins to connect it (12 of them).

He only needs to connect 7 pins. He can leave the rest of the pins disconnected.

fungus:
If pins are important you'd be much better off with a "rotary encoder". This will do the same thing but only needs two pins for the shaft rotation. You also get a built-in push button (push the shaft down) which you can connect to a third pin.

I believe it is a bit more complicated to read gray code/ rotary encoder. But perhaps there are Arduino libraries for that, though.

pekkaa:
I believe it is a bit more complicated to read gray code/ rotary encoder. But perhaps there are Arduino libraries for that, though.

Yes, there is a whole section of the Playground with different examples of how to use rotary encoders with Arduinos.

Far-seeker:

pekkaa:
I believe it is a bit more complicated to read gray code/ rotary encoder. But perhaps there are Arduino libraries for that, though.

Yes, there is a whole section of the Playground with different examples of how to use rotary encoders with Arduinos.

Yes, but it would still be more complicated than using a rotary switch with each pin connected a different input pin. If this is Kobra299's fist Arduino project, I suggest going with the simpler alternative.

pekkaa:
Yes, but it would still be more complicated than using a rotary switch with each pin connected a different input pin. If this is Kobra299's fist Arduino project, I suggest going with the simpler alternative.

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.

fungus:

pekkaa:
Yes, but it would still be more complicated than using a rotary switch with each pin connected a different input pin. If this is Kobra299's fist Arduino project, I suggest going with the simpler alternative.

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.

Why in the world would a library be needed to check a fixed number of input pins (he listed seven choices) to see which is active? He needs to wire up seven input pins from his single pole switch and do a series of if tests, that hardly justifies a library solution does it?

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

I would dispute that unless the OP has requirements above the simple one he defined.
Lefty

A rotary switch is going to be like a bunch of non-matrixed buttons/switches, so in this case seven input pins will be required. These pins will also need either be polled or have pin-change interrupts setup. The code might be fairly simple, but fungus is right that there will be significantly more of it to deal with. On the other hand, there is code applicable avialable for using a rotary encoder, it would only need a couple of pins, and introduces the concept of using I/O pins to represent binary integers (AKA bit banging) which can be useful later on.

retrolefty:
I would dispute that unless the OP has requirements above the simple one he defined.

Ah, he only wants seven fixed choices...I guess that's not difficult to code... :smiley:

(Not very future-proof though)

Far-seeker:
A rotary switch is going to be like a bunch of non-matrixed buttons/switches, so in this case seven input pins will be required. These pins will also need either be polled or have pin-change interrupts setup. The code might be fairly simple, but fungus is right that there will be significantly more of it to deal with. On the other hand, there is code applicable avialable for using a rotary encoder, it would only need a couple of pins, and introduces the concept of using I/O pins to represent binary integers (AKA bit banging) which can be useful later on.

Not saying a rotary encoder wouldn't work and might very well even be a more elegant and resource saving solution, but to say a simple 7 input pin polling sketch is other then a very simple task is assuming more requirements then the OP stated. Recall also that the OP stated that he already has the switch in hand. I would think it's a very good method for a beginner first learning programming and hardware electronics, rather then relying on a library based solution.
Lefty

retrolefty:
Not saying a rotary encoder wouldn't work and might very well even be a more elegant and resource saving solution, but to say a simple 7 input pin polling sketch is other then a very simple task is assuming more requirements then the OP stated. Recall that the OP stated that he already has the switch in hand. I would think it's a very good method for a beginner first learning programming and hardware electronics, rather then relying on a library based solution.

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. :wink: Furthermore it's perhaps the most compact ways to express the logic without getting into rather arcane tricks.

Yet, you are correct that using the available hardware is probably the best approach for this particular device since it's definately a workable solution. Still it doesn't hurt to bring up the alternatives, even if they aren't used. There's always the next project... :slight_smile:

fungus:
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 :wink: 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)

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

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

Far-seeker:
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. :wink: Furthermore it's perhaps the most compact ways to express the logic without getting into rather arcane tricks.

Ever heard of the 'switch' statement?

pekkaa:
I don't think a rotary switch is as complicated as you think :wink: 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.

fungus:
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. :stuck_out_tongue: