2017-08-16 59 views
0

我的項目是:讀取溫度並通過SMS以10分鐘的時間間隔發送,並且在此範圍內運動,然後發送短信「MOTION DETECTED」。PIR傳感器連續檢測,因此循環計數器(「i」)可能不會遞增並且循環保持空閒狀態

如此,只是爲了測試我用了兩個LED一個溫帶(它將停留在10分鐘),SMS和其他的運動(它將閃爍一次)和評論功能的調用語句即的sendMessage,SENDMSG

一切工作正常,但問題是「當我長時間持續晃動我的手在PIR傳感器前面時,它持續檢測運動並且可能是計數器不會增加,因此不會增加,即第一次溫度超過10分鐘...,但如果我將我的手放在PIR傳感器前幾秒鐘以檢測Motion(儘可能多的時間),那麼它的工作正常......但對於持續檢測出現的問題,超過10分鐘。例如,持續30秒的連續檢測在10分鐘內停留30秒...... 1分鐘DLED停留在11mins ......」

這是問題希望我讓你們使它可以理解的...... 所以請幫助我..謝謝

這裏下面的代碼...

#include <DHT.h>     //DHT sensor 
#include <SoftwareSerial.h>  //this is a constructor for gsm 
#include<LiquidCrystal.h>  //this is for LCD display 
#include "DHT.h"   
#define DHTPIN 8     //blue 8 define pins for DHT 
#define DHTTYPE DHT11 // DHT11 
#define pir 6     //yellow 6 
#define pled 13     //for PIR led 
#define dled 7     //for DHT led 

DHT dht(DHTPIN,DHTTYPE); 
int pirState=LOW; 

int prival=0; 

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 
SoftwareSerial mySerial(9, 10); //9-->Rx; 10-->Tx 



void setup() { 
    // put your setup code here, to run once: 
    mySerial.begin(9600); 
    Serial.begin(9600); 
    delay(100); 
    Serial.println("SMS ALERT"); 
    pinMode(pir,INPUT);   //set pir pin as input 
    dht.begin(); 
    lcd.begin(16,2); 
    pinMode(pled,OUTPUT);  //LED for pir sensor... "set pled pin as output" 
    pinMode(dled,OUTPUT);  //LED for DHT sensor... "set dled pin as output" 


} 
void loop() { 
    // put your main code here, to run repeatedly: 


// block for sending temp information... 
float t=dht.readTemperature(); 
Serial.print("Temperature: "); 
Serial.println(t); 
Serial.println("SMS INITIATING..."); 
// SendMessage(t); to sent temp... 

/*stay HIGH for 10 min..when loop will end pin will be low i.e. LED will be 
off then again for new loop it will be on ..and it will continue... */ 
digitalWrite(dled,HIGH);     

Serial.println("DONE..."); 
lcd.print("Temp is: "); 
lcd.print(t); 
lcd.print((char)223); 
lcd.print("C "); 
delay(500); 

//checking pir sensor.. whether motion is detected or not.. 
//when motion is detected sent msg "MOTION DETECTED" 
//and loop will continue for 10 min span 

int i=0; 
    while(i!=600){ 
     prival=digitalRead(pir);  //read pir pin = 6 
     if(prival==HIGH) 
     { 
     if(pirState==LOW) 
     { 
      Serial.println("Motion detected"); 
      Serial.print("Temp is: "); 
      Serial.print(t); 
      Serial.print((char)223); 
      Serial.println("C"); 
      lcd.setCursor(0,0); 
      lcd.print("temp is : "); 
      lcd.print(t); 
      lcd.print(char(223)); 
      lcd.print("C"); 
      lcd.setCursor(0,1); 
      lcd.print("Motion Detected"); 

     /*when motion is detected only that time LED will be on.. and stay  
     high for 200mSec then it will be off i.e. it will blink for once */  
      digitalWrite(pled,HIGH); 
      delay(200); 
      digitalWrite(pled,LOW); 
      Serial.println("SMS INITIATING FOR MOTION..."); 
      //sendMsg(); to send "motion detected msg" 

      Serial.println("DONE..."); 

      pirState=HIGH; 

     } 
    } 
    else 
    { 
     if(pirState==HIGH) 
     { 
      Serial.println("Motion stopped  "); 
      Serial.print("Temp is: "); 
      Serial.print(t); 
      Serial.print((char)223); 
      Serial.println("C"); 
      lcd.setCursor(0,0); 
      lcd.print("temp is : "); 
      lcd.print(t); 
      lcd.print(char(223)); 
      lcd.print("C"); 
      lcd.setCursor(0,1); 
      lcd.print("Motion stopped  "); 
      pirState=LOW; 

     } 
    } 

    i++; 
    delay(1000); 
    } 
digitalWrite(dled,LOW); 
delay(500); 
} 
/* 
void SendMessage(float t) 
{ 
    mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode 
    delay(1000); // Delay of 1000 milli seconds or 1 second 
    mySerial.println("AT+CMGS=\"XXXXXXXXXX\"\r"); // Replace x with mobile  number 
    delay(1000); 
    mySerial.println("TEMP IS : ");// The SMS text you want to send 
    mySerial.println(t); 
    delay(100); 
    mySerial.println((char)26);// ASCII code of CTRL+Z 
    delay(1000); 
} 
void sendMsg(){ 
    mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode 
    delay(1000); // Delay of 1000 milli seconds or 1 second 
    mySerial.println("AT+CMGS=\"XXXXXXXXXX\"\r"); // Replace x with mobile number 
    delay(1000); 
    mySerial.println("MOTION DETECTED...");// The SMS text you want to send 
    delay(100); 
    mySerial.println((char)26);// ASCII code of CTRL+Z 
    delay(1000); 
} 
*/ 

回答

0

的問題是,你的代碼是寫在使用delay(500);等的阻隔時尚。您每次執行PIR傳感10 * 60次和delay(1000);,因此您預計LED開關延遲10分鐘。但是,正如您注意到的那樣,delay(200);的內部會延長在PIR環路中花費的時間。

您可以通過多種方式解決此問題,但最簡單的方法是使用全局超時計時器異步執行LED事件。將超時定時器設置爲10分鐘,打開LED,然後等待10分鐘後,定時器中斷會調用相關的超時服務程序,以關閉LED。第二個LED也一樣。

您的項目通常可能會使用某種RTOS而不是編寫普通的Arduino代碼。