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.
1 Answer 1
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.
}
-
1Thank you everyone for your input. I will be moving ahead with this project.Bender– Bender2018年03月13日 16:58:42 +00:00Commented Mar 13, 2018 at 16:58
-
2pin 1 is a trap for the beginner?2018年03月13日 18:13:08 +00:00Commented 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.Michel Keijzers– Michel Keijzers2018年03月13日 23:12:43 +00:00Commented Mar 13, 2018 at 23:12
INPUT_PULLUP
mode for the pins. If you don't you then need to add a pulldown resistor to all 4 inputs.