Getting a function not to happen again until input is below threshold.

Hi All,

I'm trying to figure how to write this code but I'm getting a bit lost about which is the best way to start.

  • There is a function to execute.
  • An analog signal is an input.
  • There is an upper and lower threshold.

Here is the description of the program:

  • If the analog signal is above upper threshold.
  • Do function.
  • Don't do function (again) until analog signal is below lower threshold.
  • Loop

I'm trying to find a way to do this without 'pausing' the program until the lower threshold is met.

I'd like to find a way to keep checking if the value is below the lower threshold until allowing the function to be done again if the upper threshold is met again.

Whats the best approach here?

Thanks,
Ben

boolean didFunction; // declared out here.

setup() {

didFunction = false; // init to false.
}

loop() {

int signal = analogRead(..);
if (! didFunction && signal> threshold) {
doFunction();
didFunction = true;
} else (! didFunction && signal<threshold) {
doFunction();
didFunction = false;
}
}

Something like that?

-jim lee

Look at the state change detection example. While it specifically deals with switches changing from not pressed to pressed or from pressed to not pressed, it is easy to adapt to analog values going from below to above a threshold or from above to below a threshold.

The example increments a variable when one change happens. It could just as easily call a function.

jimlee2:
boolean didFunction; // declared out here.

setup() {

didFunction = false; // init to false.
}

loop() {

int signal = analogRead(..);
if (! didFunction && signal> threshold) {
doFunction();
didFunction = true;
} else (! didFunction && signal<threshold) {
doFunction();
didFunction = false;
}
}

Something like that?

-jim lee

Thanks for this. I had never thought of using a didFunction check together with operators.

Your examples wasn't exactly what I was looking for but this is because I didn't explain it clearly in my first post but I've managed to write the code to what i wanted based on what you suggested.

Basically I didn't want the function to execute again until the signal had gone below the lower threshold and then back above the upper threshold, so basically it would only execute once for every time is went over.

Here is my code which solved it:

int upperThreshold = 600; // up-shift threshold
int lowerThreshold = 300; // release threshold
boolean didFuelCut; // check if fuelCut was done

void setup ()
{
pinMode(2,OUTPUT);
digitalWrite(2,LOW);
didFuelCut = false; // initial setting to False
}

void fuelCut() //fuel cut function
{
digitalWrite(2,HIGH);
delay(500);
digitalWrite(2,LOW);
}

void loop()
{

float loadcell = analogRead(0);

if (!didFuelCut && (loadcell > upperThreshold))
{
fuelCut();
didFuelCut = true;
}

if (didFuelCut && (loadcell < lowerThreshold))
{
didFuelCut = false;
}
}

Thanks again

Naw, you said it right. I couldn't edit my error 'cause there's a 5 minute post limit for newbies. Frustrating seeing the error and not being able to do anything about it.

Glad it helped!

-jim lee