Paso 5: código
Código, el más divertido parte :) El código es bastante simple, pero necesita algunos conocimientos para entenderlo (si es tu objetivo, entender el código), he intentado comentar la mayoría del código para hacerlo más fácil para que usted pueda entender, pero no sé si hice bien o no. Usted también necesitará descargar la biblioteca de LCD_5110_Graph. Muchas gracias a Henning Karlsen para crear esto!
Aquí está el código, disfrutar 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); } }}