I was just wondering if there was a way to code a timer that activates when a sensor reads high. I think I should be using millis, but I'm not sure how to implement it into code.
Can I re-write your statement as follows for my understanding:
If a temperature sensor indicates that the room temperature is higher than 270C (for example), a Timer will be ON (activated) and it will remain ON for 600-sec (for example) during which a fan will be circulating cool air inside the room.
If not, please paraphrase your statement.
Sorry for the messy statement :0
I'm using a PIR sensor and I wanted to make it so that once the sensor senses something, a 60-second timer would start.
Something along these lines. I'd move functional blocks out of the loop() and into their own functions but you should get an idea from this:
unsigned long
elapsedTime,
timerSensor;
bool
bFlg;
//where is your sensor high/low coming in?
#define PIN_SENSOR 9
void setup()
{
// put your setup code here, to run once:
bFlg = false;
pinMode( PIN_SENSOR, INPUT_HIGH );
//set up whatever else needs doing
.
.
.
}//setup
void loop()
{
if( digitalRead(PIN_SENSOR) == HIGH )
{
//use boolean "bFlg" to know if we're timing already
if( !bFlg )
{
//start timing
timerSensor = millis();
//set flag true
bFlg = true;
}//if
}//if
.
.
.
//later when you want to see how much time has passed
//i.e. some condition indicates you want to do this...
if( conditions are right )
{
//make sure you were timing
if( bFlg )
{
//time of event is the time now minus the time you started
elapsedTime = millis() - timerSensor;
//use this information
.
.
.
//ready to time another event
bFlg = false;
}//if
}//if
}//loop
Thank you for the response :000
A few questions though, what does bFlg mean in the code?
4scires:
Thank you for the response :000
A few questions though, what does bFlg mean in the code?
bFlg is just a boolean (so true or false) indicator used to know whether or not timing of PIN_SENSOR being high is happening.
When the code gets to the top of the loop and saw the "if( digitalRead(PIN_SENSOR) == HIGH )" statement, without some sort of indication that a previous loop has already seen this condition and started timing the value of timerSensor would be updated every pass with millis().
bFlg ensures that once the condition is seen (PIN_SENSOR == HIGH) that the timing starts then and isn't fouled by subsequent loops.
Once you're finished with the timing, you set bFlg false again so another timing cycle can happen.
Thank you, can you show me the sample code. :0
4scires:
Thank you, can you show me the sample code. :0
What do you mean?
@OP
Look here in this link for sample codes on the application of PIR.