Hello!
I'm new at arduino and I'm really stuck, been searching online for hours to figure it out. Can someone please help?
I am trying to make multiple rotary switches (2 poles 6 positions) work with an arduino uno, and not being able to. At the moment I'm only wiring up 2 rotary switches, but only one works. I already made sure they're not broken, so they do work individually, but I cant get readings from both at the same time. So the code runs well, except that one of the rotary switches' readings is always "not working". Is there maybe something about wiring multiple rotary switches to an arduino that I'm missing?
I do not want to use potentiometers (even though ideal because less pins to wire up....) because i need the knobs of my art installation to have that "click" feel when turning, rather than being a smooth rotation.
Here's my code, please ignore the terrible naming and yes, I know I should use variable arrays instead.
Thank you in advance!!
const int RotaryPinone = 2;
const int RotaryPintwo = 3;
const int RotaryPinthree = 4;
const int RotaryPinfour = 5;
const int RotaryPinfive = 6;
const int RotaryPinsix = 7;
const int RotaryOne = 8;
const int RotaryTwo = 9;
const int Rotarythree = 10;
const int RotaryFour = 11;
const int RotaryFive = 12;
const int RotarySix = 13;
void setup() {
for( int i=RotaryPinone; i<= RotaryPinsix; i++) {
pinMode( i, INPUT);
digitalWrite( i, HIGH); // turn on internal pullup resistor
}
Serial.begin(9600); // let's talk to the world
Serial.println("RotarySwitch ready!");
for( int x=RotaryOne; x<= RotarySix; x++) {
pinMode( x, INPUT);
digitalWrite( x, HIGH); // turn on internal pullup resistor
}
Serial.println("Rotary2nd ready!");
}
// returns the position of the rotary switch, 1-4
// or returns 0 if no rotary switch is hooked up
int getRotaryValue() {
for( int i=RotaryPinone; i<= RotaryPinsix; i++) {
int val = digitalRead( i ); // look at a rotary switch input
if( val == LOW ) { // it's selected
return (i - RotaryPinone + 1); // return a value that ranges 1 - 5
}
}
return 0; // error case
}
int getRotaryValue2() {
for( int x=RotaryOne; x<= RotarySix; x++) {
int val = digitalRead( x ); // look at a rotary switch input
if( val == LOW ) { // it's selected
return (x - RotaryOne + 1); // return a value that ranges 1 - 5
}
}
return 0; // error case
}
void loop() {
int rotaryPos = getRotaryValue();
if( rotaryPos == 1 ) {
Serial.println("1");
}
else if( rotaryPos == 2 ) {
Serial.println("2");
}
else if( rotaryPos == 3 ) {
Serial.println("3");
}
else if( rotaryPos == 4 ) {
Serial.println("4");
}
else if( rotaryPos == 5 ) {
Serial.println("5");
}
else if( rotaryPos == 6 ) {
Serial.println("6");
}
else if( rotaryPos == 7 ) {
Serial.println("7");
}
else {
Serial.println("uh oh, somethings wrong");
}
delay(100); // slow down the loop() so we don't spam the serial port
int rotaryPos2 = getRotaryValue2();
if( rotaryPos2 ==8 ) {
Serial.println("8");
}
else if( rotaryPos2 == 9 ) {
Serial.println("9");
}
else if( rotaryPos2 == 10 ) {
Serial.println("10");
}
else if( rotaryPos2 == 11 ) {
Serial.println("11");
}
else if( rotaryPos2 == 12 ) {
Serial.println("12");
}
else if( rotaryPos2 == 13 ) {
Serial.println("13");
}
else {
Serial.println("NOT WORKING");
}
delay(100);
}