Hi all!
I have been working on a program in which I want to include an off-delay timer.
The off-delay timer has an output that buffers the input, that's kind of basic.
Also I want it to write the remaining time and the endtime as an output variable.
therefore I wrote this little sequence:
nowMillis= millis();
if (PIROut==HIGH){
PIRBufferTimeRemaining=PIRBufferSettime;
PIRBufferEndtime=nowMillis+PIRBufferSettime;
}
if (PIRBufferEndtime > nowMillis){
PIRBufferTimeRemaining= PIRBufferEndtime-nowMillis;
PIROutBuffered= HIGH;
}
else{
PIRBufferTimeRemaining=0UL;
PIROutBuffered=LOW;
}
This buffers the output of a PIR sensor. It resets the timer every time the output from the PIR is HIGH. When the input is LOW, it compares the current millis() with the endtime() that was written on the last time the input was HIGH. Tested it, and works as it should.
Now since I want to use this function multiple times, so I want it to be callable (don't know if that's even a word) in a way like this:
PIROutBuffered = offDelay(PIROut, PIRBufferSettime, PIRBufferTimeRemaining, PIRBufferEndtime);
with that in mind, I wrote the following code as a function:
bool offDelay(bool input, unsigned long timeDelayMillis, unsigned long dataRemainingTime, unsigned long dataEndTime){
bool output=LOW;
nowMillis= millis();
if (input==HIGH){
dataRemainingTime=timeDelayMillis;
dataEndTime=nowMillis+timeDelayMillis;
}
if (dataEndTime > nowMillis){
dataRemainingTime= dataEndTime-nowMillis;
output= HIGH;
}
else{
dataRemainingTime=0UL;
output=LOW;
}
return output;
}
So having this implemented inside the loop()-function, with the attributes filled in, it seems that my function doesn't accept global/public variables, or can't use or write them.
They are initiated with the following code (above the setup()-function):
//Timers
//All timervalues are in milliseconds and are unsigned long-datatype. For this reason there is "UL" after every value.
unsigned long PIRBufferSettime=60000UL;
unsigned long PIRBufferTimeRemaining=0UL;
unsigned long PIRBufferEndtime=0UL;
unsigned long nowMillis=0UL;
But when I use this, the output is just a copy of the input, no buffering is present and I don't even see why my output is set to HIGH in the first place.
I'm probably making some horrible mistakes in here, but it would be nice if someone could point out what my mistake is.
I have some very basic knowledge of class-programmed java-things, but my knowledge of coding isn't that great, so please take it easy on me Also English is not my primary language, so sorry for grammar mistakes etc.
Thanks!