vmasters:
OK thank you.
Here is another piece of code a battled for a long time
It is based on the "switch" of "David A. Mellis" but I wanted to use an pull-up pin for the switch to toggle code. I am still thinking on using an interrupt to stop the functions abruptly but it is not a necessity.
What I am trying to achieve here is to execute a loop and then wait for the button toggle (tactile switch) to call other functions (again calling other functions) and when the button is pressed go back to standby mode by calling the standby function and stopping and resetting everything.
This is only a test snippet to run with a simulator to sort out what to do and then copying it in the main program
//button variables
const byte inPin = 2; // the number of the input pin
const byte outPin = 13; // the number of the output pin
const byte ledPin = 12;
// second led pin to test if the call of other functions within the loop work
byte state = HIGH; // the current state of the output pin
byte reading; // the current reading from the input pin
byte previous = HIGH; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT_PULLUP);
pinMode(outPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and debounce time
if (reading == LOW && previous == HIGH && millis() - time > debounce) {
if (state == LOW)
state = HIGH;
else
state = LOW;
time = millis();
}
digitalWrite(outPin, !state); //State Inverted to sync the led with change function
previous = reading;
if(state == LOW){
change(); //execute void (change)
}
else
digitalWrite(ledPin, LOW); //Need this routine to reset changes made in change() back to start conditions with button toggle
//or call a function to reset everything to the initial state
}
void change() //just a function to test the call in the main loop
{
digitalWrite(ledPin, HIGH);
}
What I actually want to do is omit the "Long" variables for Time and Debounce but I am not certain exactly how.
Study the use and actual functioning of an interrupt. It does NOT stop a function, merely "interrupts" the flow of instruction execution, executes the interrupt code, and returns to the next instruction in the function and continues merrily on it's way.
If you set a switch, flag, boolean,etc. in the interrupt code to signal that an interrupt has occurred, then your function can periodically test for the switch, flag, etc. and see if it is set, and if so, exit the function. For actual manual switches, you could just as efficiently test for the manual switch in your function code.
For manual switches, either way works, but wastes an interrupt that might be needed by some other device.
Paul