MsTimer2::set(100, process); // Set the timer interrupt time 1000ms
MsTimer2::start();//Timer interrupt start
}
void loop()
{
// Serial.println(count); // Printing times of 1000ms suspension
delay(1);
if (statef == HIGH ||stateb == HIGH) //When moving objects are detected later, 2s shut down automatically after the ledout light is convenient.
{
delay(100); //2000
if(statef == HIGH){
statef = LOW;
digitalWrite(ledfOut, statef); //Turn off led
}
if(stateb == HIGH){
stateb = LOW;
digitalWrite(ledbOut, stateb); //Turn off led
}
}
}
void statefChange() //Interrupt function
{
countf++;
}
void statebChange() //Interrupt function
{
countb++;
}
void process() //Timer handler
{
if (countf > 10) //1000ms interrupt number greater than 1 is considered detected a moving object (this value can be adjusted according to the actual situation, equivalent to adjust the detection threshold of the speed of a moving object)
{
statef = HIGH;
digitalWrite(ledfOut, statef); //Lighting led
countf = 0; //Count zero
}
if (countb > 10) //1000ms interrupt number greater than 1 is considered detected a moving object (this value can be adjusted according to the actual situation, equivalent to adjust the detection threshold of the speed of a moving object)
{
stateb = HIGH;
digitalWrite(ledbOut, stateb); //Lighting led
countb = 0; //Count zero
}
// else
// count = 0; //In 1000ms, interrupts does not reach set threshold value is considered not detect moving objects, interrupt the count number is cleared to zero.