Étape 5: Code
Code, le plus drôle partie:) Le code est assez simple, mais vous avez besoin de certaines connaissances de le comprendre (si c’est votre objectif, de comprendre le code), j’ai essayé de commenter la plupart du code pour le rendre plus facile pour vous de comprendre, mais je ne sais pas si j’ai fait un bon travail ou non. Vous devrez également télécharger la bibliothèque LCD_5110_Graph. Merci à Henning Karlsen pour créer ceci !
Voici le code, profitez de :
#include <LCD5110_Graph.h> int calibrationTime = 20; // PIR sensor has to be calibrated, do not make much movement during this time long unsigned int lowIn; long unsigned int pause = 5000; //The time the sensor has to be low before we assume there is no motion bool lockLow = true; //Variables for the fact that the sensor goes low sometimes and we correct for that bool takeLowTime;int motionPin = 8; //We're connected to pin 8 int ledPin = 13; //We're also going to show if motion is detected on pin 13 (HIGH = Motion, LOW = No motion)LCD5110 lcd(7, 4, 5, 3, 6); //Making lcd objectextern unsigned char SmallFont[]; // Including our smallfont (which is included with the library)void setup() { Serial.begin(9600); //Starting serial communication lcd.InitLCD(); //Initiating LCD lcd.setFont(SmallFont); //Setting our font to a small one //Giving the sensor some time to calibrate (As said, make minimal movement during this time) Serial.println("Calibrating sensor"); //Letting user now via Serial monitor that we are calibrating lcd.clrScr(); lcd.print("Calibrating", CENTER, 16); //Same deal lcd.update(); for (int i = 0; i < calibrationTime; i++) { Serial.print("."); delay(1000); } Serial.println("Calibration done"); Serial.println("Sensor is now active and code is running"); lcd.clrScr(); lcd.print("Done", CENTER, 16); lcd.update(); delay(100); //Small delay just to make sure everything is running smooth lcd.clrScr(); lcd.print("No motion", CENTER, 14); lcd.print("detected", CENTER, 24); lcd.update(); }void loop() { if (digitalRead(motionPin) == HIGH) { //Checking whether there is motion or not digitalWrite(ledPin, HIGH); //The led visualizes the sensors output if (lockLow) { //makes sure we wait for a transition to LOW beforte any further output is made: lockLow = false; Serial.println("---"); Serial.println("Motion detected"); lcd.clrScr(); lcd.print("Motion", CENTER, 14); lcd.print("detected", CENTER, 24); lcd.update(); delay(20); } takeLowTime = true; } if (digitalRead(motionPin) == LOW) { digitalWrite(ledPin, LOW); if (takeLowTime) { lowIn = millis(); //saving the time of the transition from HIGH to LOW takeLowTime = false; //making sure this is only done at the start of the LOW phase } //if the sensor is low for more than the given pause, //we assume that no more motion is going to happen if (!lockLow && millis() - lowIn > pause) { lockLow = true; lcd.clrScr(); lcd.print("No motion", CENTER, 14); lcd.print("detected", CENTER, 24); lcd.update(); digitalWrite(ledPin, LOW); Serial.println("motion ended"); delay(10); } }}