Skip to content

Instantly share code, notes, and snippets.

@andrewhaskin
Created December 16, 2011 16:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewhaskin/1486643 to your computer and use it in GitHub Desktop.
Save andrewhaskin/1486643 to your computer and use it in GitHub Desktop.
Arduino Code for a Rotary Switch and a Button used for Globalgram
// RotarySwitch.pde -- test out a rotary switch (not a rotary encoder)
// Tod E. Kurt, http://todbot.com/blog/
////4-position rotary switch
const int firstRotaryPin = 3;
const int lastRotaryPin = 7;
int button = 9; ////button input
int input1 = LOW;
void setup() {
for( int i=firstRotaryPin; i<= lastRotaryPin; 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!");
pinMode(button, INPUT); ////for button input
}
// returns the position of the rotary switch, 1-4
// or returns 0 if no rotary switch is hooked up
int getRotaryValue() {
for( int i=firstRotaryPin; i<= lastRotaryPin; i++) {
int val = digitalRead( i ); // look at a rotary switch input
if( val == LOW ) { // it's selected
return (i - firstRotaryPin + 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 {
Serial.println("uh oh, somethings wrong");
}
delay(100); // slow down the loop() so we don't spam the serial port
input1 = digitalRead(button);
if (input1 == HIGH){ //Checking for button push
Serial.println(101);
}
if (input1 == LOW){ //Checking for button push
Serial.println(rotaryPos);
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment