2

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.

jose can u c
6,9742 gold badges16 silver badges27 bronze badges
asked Mar 13, 2018 at 12:22
4
  • 1
    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. Commented 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. Commented Mar 13, 2018 at 16:36
  • you could use an analog pin and 4 voltages instead of many pins. Commented 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 Commented Mar 14, 2018 at 1:30

1 Answer 1

2

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
 if (digitalRead(PIN_SWITCH_1) == HIGH)
 {
 timer_value = 30000;
 }
 else if (digitalRead(PIN_SWITCH_2) == HIGH)
 {
 timer_value = 60000;
 }
 else if (digitalRead(PIN_SWITCH_3) == HIGH)
 {
 timer_value = 90000;
 }
 else if (digitalRead(PIN_SWITCH_4) == HIGH)
 {
 timer_value = 120000;
 }
 else
 {
 // Keep default value
 }
 }
 void loop()
 {
 // Here you can use timer_value to check for the end of the game.
 }
answered Mar 13, 2018 at 12:31
3
  • 1
    Thank you everyone for your input. I will be moving ahead with this project. Commented Mar 13, 2018 at 16:58
  • 2
    pin 1 is a trap for the beginner? Commented Mar 13, 2018 at 18:13
  • 1
    @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. Commented Mar 13, 2018 at 23:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.