I recently started using the TimeAction library (which allows for proto-threading) and I noticed it has a really useful function for enabling/disabling threads. I want to use the same idea but apply it to a general function, though I haven't been able to find a possible solution to doing such a task on the internet. Here is a potential way for how I would like to implement it:
#include <TimedAction.h>
boolean disableCheck = false;
boolean checking = false;
boolean state = false;
const int motionPin = 5;
void checkMotion();
void motion();
void DisableCheckMotion();
TimedAction checkable = TimedAction(6000, DisableCheckMotion);
void setup() {
pinMode(motionPin, INPUT);
Serial.begin(9600);
}
void loop() {
checkMotion(); //this will be the function I wish to enable and disable
if(checking == true)
{
motion();
}
if(disableCheck == true)
{
checkable.check();
}
}
void checkMotion()
{
checking = true;
}
void motion()
{
if(digitalRead(motionPin) == HIGH)
{
Serial.println("motion");
checking = false;
checkable.check();
}
}
void DisableCheckMotion()
{
state ? state = true : state = false;
if(state == false)
{
checkMotion.disable(); //this is where I would like to disable the function
disableCheck = true;
}
if(state == true)
{
checkMotion.enable(); //this is where I would like to enable the function
disableCheck = false;
}
}
The purpose of this task is to output one value for whenever a PIR scanner (set at pin 5) picks up motion. Without any form of delaying it will flood the output with values (which is undesirable). A thread is used to offer this delay, I cannot put the code for checking for motion within the thread as I could risk the chance of a reading being missed.
The ideal setup would be for the Arduino board to receive the input and then delay the input from being received again for a period of time while not delaying the entire program. What would be the best way to implement this?
1 Answer 1
If you don't want a function to run you can just return at the start of that function:
void myFunction() {
if (!myFunctionShouldRun) return;
... rest of function ...
}
If myFunctionShouldRun
is false the if
will be true (due to the !
) and the function will return. If it's true
the rest of the function will execute.
millis()
and runs functions when the required delay passes. Nothing you can't do with a simple check ofmillis()
in your main loop. Ideal if you don't have a clue about simple programming...