I'm pretty new to arduino, and also programming. The projects I have made in the past I had to rely heavily on others projects and examples, with small tweaks here and there to fit my needs. I come here seeking such an example, or directions to the resources and terminology I would require to figure it out. I have a sensor node sending me analog values 3 times a day. When the value received is below a set threshold, the arduino begins counting the number of successive values also below this threshold until a specified number is reached, at which time the arduino triggers an event. If the specified number of successive low values is not met (readings again rise above set lower threshold) then the counter stops and resets until lower values are again recognized, also, if an event is triggered due to reaching the maximum number of below threshold values, than the counter stops and resets until low values are again see.
To be honest, I don't even know what to google to shed some light on my naive understanding of programming. I am determined though!
Keep it simple. Keep counting low values, but only trigger the event when the count EQUALS your target value.
Reset the count to 0 when a value above the threshold arrives.
Sounds like a recent question on the forum, Is it a school project?
The reason it sounds familiar is because it is - same OP
Familiar indeed... Not a school project, I'm leaving for a while and need my garden watered, thought I would have some fun trying to autonomously take care of things while I'm away. Found examples of arduino irrigation systems, but they just water as soon as things dry out. My plants can live in dry times for a little while so I thought it would be nice to save water and only give the bare minimum to them to keep them alive, not thrive..
You want to take note when X failures in a row happen, but you're back to normal as soon as 1 success happens?
If so, then the code to add is just a counter - every time you get a new reading to send, check it against your limit value. If it's bad, increment a counter, but if good, set that counter to zero. When the counter reaches high enough, then you have the problem to report, and can do something about it.
Oops - forgot to add code:
void testLimit(int levelToCheck)
{
static int failureCount=0;
if (levelToCheck<200) // or whatever you wish for threshold
{
++failureCount;
if (failureCount>5) // how many failures are tolerated
{
failureCount=0;
// sound alarm - however you wish
}
}
else // success - reset
{
failureCount=0;
}
}
Call a function like this in your loop, and change the values inside to suit your needs - it should keep track of your levels for you.
thanks so much David, I may just get to see my project realized before i leave!
if (failureCount>5) // how many failures are tolerated
{
failureCount=0;
Do you want this reset or do you want multiple alarms if the value stays above the threshold e.g. 6,7,8,etc times?
wiredone:
...if an event is triggered due to reaching the maximum number of below threshold values, than the counter stops and resets
...so reset on failure too
if an event is triggered due to reaching the maximum number of below threshold values, than the counter stops and resets until low values are again see.
@david
Your code will trigger an alarm multiple times if the levelToCheck stays low. e.g. If there are > 12 low's in a row failureCount will go 1,2,3,4,5,6,0,1,2,3,4,5,6,0, etc
The code need a flag to meet the requirement "until low values are again see."
(based upon your code)
void testLimit(int levelToCheck)
{
static int failureCount=0;
static boolean mayCount= true;
if (levelToCheck<200) // or whatever you wish for threshold
{
if (mayCount) ++failureCount;
if (failureCount>5) // how many failures are tolerated
{
mayCount == false;
// sound alarm - however you wish
}
}
else // success - reset
{
failureCount = 0;
mayCount= true;
}
}
robtillaart:
If there are > 12 low's in a row failureCount will go 1,2,3,4,5,6,0,1,2,3,4,5,6,0, etc
Isn't that the expected behavior (except starting from 6, not 12)? From the spec I see "the counter stops and resets until low values are again see" - so repeating errors will set repeating alarms.
In any case, he now has two ways to go with the code now.