Loading...
Pages: 1 [2]   Go Down
Author Topic: Rotary Switch Colour Selector help needed  (Read 644 times)
0 Members and 1 Guest are viewing this topic.
Finland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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  smiley-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)
« Last Edit: October 23, 2012, 02:35:41 pm by pekkaa » Logged

Finland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Valencia, Spain
Offline Offline
Edison Member
*
Karma: 65
Posts: 2227
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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. smiley-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?
Logged

Valencia, Spain
Offline Offline
Edison Member
*
Karma: 65
Posts: 2227
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

USA
Offline Offline
God Member
*****
Karma: 14
Posts: 644
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-razz 
Logged

Pages: 1 [2]   Go Up
Print
 
Jump to: