What is the recommended approach for using a hardware switch to alter circuit behaviour please? I want to wire in a miniature switch (or button) to change the value of a constant during testing. Up until now I’ve been swapping over the 328 chips, respectively loaded with sketches that differ by merely that one line, but a switch would obviously be more convenient and satisfying.
I’m a novice Arduino user but an experienced electronics hobbyist, (old school) and this is my first proper project. The constant determines the duration of pressing a camera button with a servo, and while testing I’m using 5s instead of 30s.
Using a toggle/slide switch (or a jumper), read the switch position in setup() then set the variable that you are thinking of as a constant to the appropriate value. If you don't mess with that value in the rest of the program it will remain constant.
Since the switch is only checked once in setup() that means your 'constant' value is set only on power up or reset.
Many thanks Steve, that looks exactly what I’m seeking and I’ll try it eagerly tomorrow.
Presumably I would, for example, connect the common terminal of my two-way switch to 0V, and switch it between two otherwise unused pins, both using PULL-UP. Calling those pins say Switch5 and Switch30, I’d then use an IF statement in Setup() to check for a low condition, and hence set the respective delay with its Const statement?
UKHeliBob:
You cannot change the value of a constant whilst the program is running. What exactly do you want to do and when ?
When the program is compiled ?
When the Arduino is powered up or reset ?
Whilst the program is running ?
Something else ?
Thanks for replying, but I thought that was all clear from my opening sentence? Obviously I want the constant to be defined at one of the two places I understood constants can be defined, namely at the very top or in the setup() section. Steve’s reply makes it clear that it must be the latter. That then sets it on power up, as wanted.
Terrypin:
Thanks for replying, but I thought that was all clear from my opening sentence? Obviously I want the constant to be defined at one of the two places I understood constants can be defined, namely at the very top or in the setup() section. Steve’s reply makes it clear that it must be the latter. That then sets it on power up, as wanted.
This is all a bit mixed up.
If you want a value to be changed when the program runs it must be a variable - you cannot change the value of a constant. The values of constants are set when the program is compiled.
Secondly, the place where a variable is defined is not necessarily the place where its value is set. If you define a variable in setup() it is a local variable and it can only be used inside setup().
What you need to do is to define the variable before the setup() function and then set its value inside the setup() function. Variables that are defined before the setup() function are global variables and can be used in any part of your program.
Terrypin:
Many thanks Steve, that looks exactly what I’m seeking and I’ll try it eagerly tomorrow.
Presumably I would, for example, connect the common terminal of my two-way switch to 0V, and switch it between two otherwise unused pins, both using PULL-UP. Calling those pins say Switch5 and Switch30, I’d then use an IF statement in Setup() to check for a low condition, and hence set the respective delay with its Const statement?
You will only need one of the two to be connected to the Arduino; no need for both.
Read the one pin, if it's HIGH do one thing, if it's LOW do the other thing.
I thought that was all clear from my opening sentence? Obviously I want the constant to be defined at one of the two places I understood constants can be defined, namely at the very top or in the setup() section. Steve's reply makes it clear that it must be the latter. That then sets it on power up, as wanted.
You can declare a constant anywhere in the sketch but where it is declared sets its scope. If a constant (or any other variable) is declared in setup() then it can only be referenced in setup() or passed as a parameter to another function to be used there.
I am still not clear when you want to be able to change the value of the timing variable. I can't help feeling that there is confusion in your requirements between a constant, whose value cannot be changed once the program is compiled and uploaded and a variable whose value can be changed whilst the program is running.
Do you want to be able to change the timing whilst the program is running or should the value be set at power up or reset ? As your question asks about using a switch I assume that the value will not be set when the program is compiled.
Thanks for the follow-ups. The switch will be set manually before power is applied. I agree that I didn’t summarise accurately; it is, of course, a variable that the switch will change.
I’ll report back when I’ve actually tried Steve’s solution.
What is the recommended approach for using a hardware switch to alter circuit behaviour please? I want to wire in a miniature switch (or button) to change the value of a constant during testing.
The switch will be set manually before power is applied. I agree that I didn't summarise accurately; it is, of course, a variable that the switch will change.
Its not clear whether if you need to operate the switch while the software is running. However, changing the value of a variable during testing implies that this is the case.
When checking the switch status in your "void loop()" code, you'll find that the value will fluctuate or bounce while the switch is in operation. This can be easily debounced in code using a millis timer.
If using a SPDT switch, sure, a millis() timer would work, but an old school SR NAND Latch implemented in code is ultra fast, simple and 100% effective.
Terrypin:
The switch will be set manually before power is applied.
Then just read the switch pin(s) in setup() and save the state to a variable for use elsewhere in the code.
I use that technique to allow unique identification of some custom Arduino boards. On mine there is a jumper that can be placed in different positions - but the result should be the same
dlloyd:
Its not clear whether if you need to operate the switch while the software is running. However, changing the value of a variable during testing implies that this is the case.
Its not clear whether if you need to operate the switch while the software is running.
Thanks, but as mentioned the switch will be set before power-up. It follows that power will be removed before I change the setting.
When checking the switch status in your "void loop()" code, you'll find that the value will fluctuate or bounce while the switch is in operation. This can be easily debounced in code using a millis timer.
If using a SPDT switch, sure, a millis() timer would work, but an old school SR NAND Latch implemented in code is ultra fast, simple and 100% effective.
I’ll investigate that further, although I’m unclear about your reference to millis(), as timing seems irrelevant.