0

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?

  • " I cannot put the code for checking for motion within the thread as I could risk the chance of a reading being missed" Wouldn't you miss a reading when the function is a disabled? – wondra Mar 14 '18 at 12:36
  • 1
    You probably don't want threads for this. Instead, check elapsed time in your main loop, and call the less frequent functions only if enough time has elapsed since you last ran them. – Chris Stratton Mar 14 '18 at 15:18
  • @ChrisStratton That's basically what TimedAction.h does. It does the same thing as SimpleTimer.h - it just encapsulates a check of millis() and runs functions when the required delay passes. Nothing you can't do with a simple check of millis() in your main loop. Ideal if you don't have a clue about simple programming... – Majenko Mar 14 '18 at 18:24
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.