First, I am a beginner for this. I just ordered my first book on coding Arduino and I'm working through some simple applications using a DS3231 (that has a temperature reading), LCD, MicroSD card module, etc. My board is a Uno R3. Currently, I need a sketch to run part of a program which contains a loop to collect data and have this run when a toggle switch is turned on. So, toggle goes on and part of program runs, toggle goes off and this part exits. The reason I need a toggle with constant on/off is the problem with G forces and vibration. I momentary push button input will not be reliable. Ultimately, all the components will be in a amateur high-power rocket to run a roll control program. If you can help, I greatly appreciate your assistance.
2 Answers
There are different ways to accomplish the same thing.
The condition is a flag, or a read port.
if(digitalRead(inPin))
{
// Your first code here.
}
else
{
// second code
}
Switch contacts will bounce when opening or closing. You may want to consider that. Usually it is stable within 10 milliseconds. Depending on what you are doing it may or may not be relevant.
You need a few things. At the top of your sketch, before setup()
byte togglePin = x; // from 2 to 19 with an Uno. Wire to connect pin to Gnd when closed.
In setup()
pinMode (togglePin, INPUT_PULLUP);
In loop()
if (digitalRead(togglePin) == LOW){
// switch closed, do something - or stop doing something
// maybe set a flag too, like
toggleState = 0;
}
else {
// switch, stop doing something - or start doing something
toggleState = 1;
}
The flag can be used elsewhere in your code
if (someOtherCondition && toggleState == 0){
// run a timer or motor or something
}
if (someOtherCondition && toggleState == 1){
// turn off timer or motor or something
}
-
Thank you! I'm going to try this. RE previous comments, sorry if there was any misunderstanding. Just wanted to assure that people understood that I wasn't using a momentary switch like turning an LED on and off. I need to turn on the system, start the program and then wait for me to turn on the screw switch to start the sensor and control program. This will only be done once per flight.– REDCommented Dec 28, 2018 at 18:03
-
You could use a while (digitalRead()==Low) in setup() instead. It will sit there reading the pin until the condition becomes false. Commented Dec 29, 2018 at 19:39
-
The if-else statement works great; thanks again for the help. To gain some experience, I also used the toggleState variable too. I just had to add to define as an int in the beginning of the program. Do you think the (digitalRead()==Low) would be faster or behave any differently? Also, I'm curious, do comments (i.e. //) slow program execution, or does compiling disregard these comments?– REDCommented Jan 1, 2019 at 20:57
-
Comments do nothing, the compiler strips them out. "Do you think the (digitalRead()==Low) would be faster or behave any differently?" Than what? Commented Jan 1, 2019 at 23:47
-
I was thinking that although there might be several ways to accomplish the same behavior, some routines may be faster. And, I mean perceived faster, not just machine time. Although I'm sure that slower routines would add time if they are repeated many times. Anyway, I just received my first book on Arduino sketching and started reading it. Hopefully, I'll know more in about a week. But, I'll still have questions no doubt. This is an excellent resource!– REDCommented Jan 3, 2019 at 1:03
debounce
library in your sketch