2017-08-16 6 views
0

Mon projet est: Lire la température et l'envoyer par SMS avec 10 min span, et dans cette durée s'il y a mouvement puis envoyer un SMS "MOUVEMENT DÉTECTÉ".Détecteur PIR détectant continuellement le compteur de boucle ("i") peut ne pas être incrémenté et boucle rester dans un état inactif

donc, juste pour le test je l'ai utilisé deux LED un pour tempéré (il restera pendant 10 min) SMS et un autre pour le mouvement (il clignote une fois) et commenté instruction d'appel de fonction c.-à-sendMessage, sendMsg

tout fonctionne bien mais le problème est "quand je garde et secoue continuellement pendant une longue période ma main devant le capteur PIR, il détecte continuellement le mouvement et peut être le compteur n'augmente pas donc dled ie le premier pour temp rester sur plus de 10 min ..., mais si je garde ma main devant le capteur PIR pendant quelques secondes juste pour détecter le mouvement (autant de fois que je veux), alors ça marche bien ... mais pour la détection continue, quelque chose se passe pour ce qui reste plus de 10 minutes, par exemple pendant 30 secondes de détection continue dled rester sur 10 min 30 sec ... pendant 1 min séjour DLED sur 11mins ... »

c'est le problème espère que je vous fais les gars rendre compréhensible ... donc s'il vous plaît, aidez-moi .. merci

ici est le code ci-dessous ...

#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); 
} 
*/ 

Répondre

0

le problème est que votre code est écrit d'une manière de blocage en utilisant delay(500); et ainsi de suite. Vous exécutez le PIR en détectant 10 * 60 fois et delay(1000); chacun, ainsi vous attendez 10 minutes de retard pour le basculement de LED. Cependant, le delay(200); interne va allonger le temps passé à l'intérieur de la boucle PIR, comme vous l'avez remarqué.

Vous pouvez résoudre ce problème de plusieurs façons, mais le plus simple serait de faire des événements LED de manière asynchrone à l'aide d'un temporisateur global. Vous définissez le temporisateur de délai d'attente à 10 minutes, allumez le voyant, puis au bout de 10 minutes, votre interruption de temporisation appelle la routine de service de temporisation correspondante qui éteint le voyant. Idem pour la deuxième LED.

Votre projet pourrait en général bénéficier d'une sorte de RTOS au lieu d'écrire du code Arduino.