# Using a rotary switch

I don’t have a sketch for this project, just doing some thinking before I pullout the keyboard (this is my phone). I was interested in writing a program that uses a predetermined time limit, for instance having a definition like...

``````#define RUNTIME 30000
``````

So the game runs for 30 seconds. By doing this it would be a simple matter of changing the 30000 to 60000 and uploading to the Arduino.

After thinking about that a while, I would prefer to be able to adjust the time using a selector switch.
So my question is.... if I use a 1p4t (on-on-on-on) rotary selector switch, assign each position to a pin and run 5v to the common. Each pin would defined as RUNTIME and a different time limit, 30000, 60000, 90000, 120000, three pins would be LOW and one HIGH depending on position. Then using IF and ELSE statements in the loop setup would I be able to switch the times? I know once the Arduino is powered up the selector switch would need to be at the correct location and if you choose to change the “RUNTIME” a reset would need to be pressed.

Thank you for any assistance or ideas.

• Better to use GND not +5V and use the `INPUT_PULLUP` mode for the pins. If you don't you then need to add a pulldown resistor to all 4 inputs.
– Majenko
Mar 13, 2018 at 13:35
• You would not HAVE to reset when changing the switch position. you could periodically check the switch input and update the value while running. Mar 13, 2018 at 16:36
• you could use an analog pin and 4 voltages instead of many pins. Mar 13, 2018 at 20:01
• for a simpler keyswitch circuit look at the keypad schematic at the bottom of this page ... in the "More" section .... dfrobot.com/wiki/index.php/… ......... use the code on the page for reference Mar 14, 2018 at 1:30

If I understand you well, it's very easy.

Assuming the 4 outputs of the switch will be connected to 4 GPIO's you just can check for the state of each GPIO and act on it

``````// Pin numbers where switch 1 to 4 are attached to.
#define PIN_SWITCH_1  8
#define PIN_SWITCH_2  9
#define PIN_SWITCH_3 10
#define PIN_SWITCH_4 11

unsigned long timer_value = 1000; // No switch selected

void setup()
{
// Set switch pins to input mode.
pinMode(PIN_SWITCH_1, INPUT);
pinMode(PIN_SWITCH_2, INPUT);
pinMode(PIN_SWITCH_3, INPUT);
pinMode(PIN_SWITCH_4, INPUT);

// Check switches
{
timer_value = 30000;
}
{
timer_value = 60000;
}
{
timer_value = 90000;
}
{
timer_value = 120000;
}
else
{
// Keep default value
}
}

void loop()
{
// Here you can use timer_value to check for the end of the game.
}
``````
• Thank you everyone for your input. I will be moving ahead with this project. Mar 13, 2018 at 16:58
• pin 1 is a trap for the beginner?
– Juraj
Mar 13, 2018 at 18:13
• @Juraj ... yes it's mean of me to use those pins ... I was not near an Arduino so I should check which are really free and obvious to use. Mar 13, 2018 at 23:12