無標題文章

intpbIn =2;// 定義輸入信號引腳intledOut = A0;// 定義輸出指示燈引腳intstate = LOW;// 定義默認輸入狀態(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指示燈//模擬一個長的流程或者復雜的任務(wù)for(inti =0; i <100; i++)

{//延時10毫秒delay(10);

}

}


intpbIn =0;// 定義中斷引腳為0,也就是D2引腳intledOut = A0;// 定義輸出指示燈引腳volatileintstate = LOW;// 定義默認輸入狀態(tài)voidsetup(){// 置ledOut引腳為輸出狀態(tài)pinMode(ledOut, OUTPUT);// 監(jiān)視中斷輸入引腳的變化attachInterrupt(pbIn, stateChange, CHANGE);

}voidloop(){// 模擬長時間運行的進程或復雜的任務(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.

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容