#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int sensePin = A0; //This is the Arduino Pin that will read the sensor output
int sensorInput; //The variable we will use to store the sensor input
double temp; //The variable we will use to store temperature in degrees.
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long interval_temp = 10000;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
lcd.init();
lcd.backlight();
lcd.begin (20, 4);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(2000);
display.clearDisplay();
}
void temperature_calc()
{
sensorInput = analogRead(A0); //read the analog sensor and store it
temp = (double)sensorInput / 1024; //find percentage of input reading
temp = temp * 5; //multiply by 5V to get voltage
temp = temp - 0.5; //Subtract the offset
temp = temp * 100; //Convert to degrees
float temperatureF = (temp * 9.0 / 5.0) + 32.0;
lcd.setCursor(0, 0);
lcd.print("TempC : ");
lcd.setCursor(7, 0);
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.print("TempF : ");
lcd.setCursor(7, 1);
lcd.print(temperatureF);
Serial.print("TempC : ");
Serial.print(temp);
Serial.print(" ");
Serial.print("Tempf : ");
Serial.print(temperatureF);
Serial.println("");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println("TempC : ");
display.setCursor(50, 10);
display.println(temp);
display.setCursor(0, 20);
display.println("Tempf : ");
display.setCursor(50, 20);
display.println(temperatureF);
display.display();
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis >= interval_temp)
{
previousMillis = currentMillis;
temperature_calc();
}
val = digitalRead(inputPin); // read input value from the PIR sensor
if (val == HIGH)
{
if (pirState == LOW)
{
// motion detected
digitalWrite(ledPin, HIGH); // turn on LED
pirState = HIGH;
Serial.println("Motion Detected");
interval_temp = 15000;
lcd.setCursor(3, 2);
lcd.print("Motion Detected");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 40);
display.println("Motion Detected");
display.display();
}
}
else
{
if (pirState == HIGH)
{
// motion ended
lcd.clear();
lcd.setCursor(3, 2);
lcd.print("Motion Ended");
Serial.println("Motion Ended");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 40);
display.println("Motion ENDED");
display.setCursor(20, 50);
display.println("Connected");
display.display();
delay(1000);
digitalWrite(ledPin, LOW); // turn off LED
pirState = LOW;
interval_temp = 10000;
}
}
}