intpbIn =2;// 定義輸入信號引腳intledOut = A0;// 定義輸出指示燈引腳intstate = LOW;// 定義默認(rèn)輸入狀態(tài)voidsetup(){// 設(shè)置輸入信號引腳為輸入狀態(tài)膳灶、輸出引腳為輸出狀態(tài)pinMode(pbIn, INPUT);
pinMode(ledOut, OUTPUT);
}voidloop(){
state = digitalRead(pbIn);//讀取微動開關(guān)狀態(tài)digitalWrite(ledOut, state);//把讀取的狀態(tài)賦予LED指示燈//模擬一個長的流程或者復(fù)雜的任務(wù)for(inti =0; i <100; i++)
{//延時(shí)10毫秒delay(10);
}
}
intpbIn =0;// 定義中斷引腳為0炒刁,也就是D2引腳intledOut = A0;// 定義輸出指示燈引腳volatileintstate = LOW;// 定義默認(rèn)輸入狀態(tài)voidsetup(){// 置ledOut引腳為輸出狀態(tài)pinMode(ledOut, OUTPUT);// 監(jiān)視中斷輸入引腳的變化attachInterrupt(pbIn, stateChange, CHANGE);
}voidloop(){// 模擬長時(shí)間運(yùn)行的進(jìn)程或復(fù)雜的任務(wù)医男。for(inti =0; i <100; i++)
{// 什么都不做嗽仪,等待10毫秒delay(10);
}
}voidstateChange(){
state = !state;
digitalWrite(ledOut, state);
}
#include ? ? ? ? ? //Timer interrupt function
int pbIn = 0;? ? ? ? ? ? ? ? ? // Define the interrupt PIN is 0, that is, digital pins 2
int ledOut = 13;
int count=0;
volatile int state = LOW;? ? ? //Define ledOut, default is off
void setup()
{
Serial.begin(9600);
pinMode(ledOut, OUTPUT);
attachInterrupt(pbIn, stateChange, FALLING); // Sets the interrupt function, falling edge triggered interrupts.
MsTimer2::set(1000,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(state == HIGH)? //When moving objects are detected later, 2s shut down automatically after the ledout light is convenient.
{
delay(2000);
state = LOW;
digitalWrite(ledOut, state);? ? //Turn off led
}
}
void stateChange()? //Interrupt function
{
count++;
}
void process()? //Timer handler
{
if(count>1)? //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)
{
state = HIGH;
digitalWrite(ledOut, state);? ? //Lighting led
count=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.
}