Untitled

 avatar
unknown
plain_text
10 months ago
5.4 kB
45
Indexable
#include <DFRobot_DHT11.h>
#include <AvrI2c_Greiman.h>
#include <LiquidCrystal_I2C_AvrI2C.h>
LiquidCrystal_I2C_AvrI2C lcd(0x27, 16, 2);
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11);
//YAğız
// Pin definitions
const int currentSensorPin = A2;      // Current sensor analog input
const int relayPin = 5;               // Relay digital output
const int ldrPin = A0;                // LDR analog input
const float currentThreshold = 10.0;  // Current threshold in Amps
const int lightThreshold = 500;       // Light threshold (analog value)
const int tempThreshold = 28;
// Variables
float currentReading;
int ldrReading;
float voltage;
float current;
//------------------
//tuğrul
const int pirPin = 2;   // PIR sensor pin
const int reedPin = 3;  // Reed switch pin
int pirState = LOW;
int reedState = LOW;
//-----------
//onur
DFRobot_DHT11 DHT;
//#define DHT11_PIN 10
int tempPin = 7;
int temperature;
//--------
//İlker
//CURRENT FLOAT????
//int datas[] = {current,ldrReading,pirState,reedState,temperature};

void yagi();
void tugrul();
void onur();
void ilker();
void enes(String message1, String message2, int sensorData);


void setup() {
  
  //yağız
  pinMode(currentSensorPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(ldrPin, INPUT);
  // Initially turn off the relay
  digitalWrite(relayPin, LOW);
  //-----------------
  //tuğrul
  pinMode(pirPin, INPUT);
  pinMode(reedPin, INPUT_PULLUP);  // Using internal pull-up resistor
  //---------------
  //onur
  pinMode(tempPin, INPUT);
  //-------
  //ilker
  lcd.begin();
  lcd.backlight();
  lcd.clear();
  //----------
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() {
  yagi();
  tugrul();
  onur();
  ilker();
  delay(5000);

}

void yagi() {
  currentReading = analogRead(currentSensorPin);
  // Assuming ACS712 20A sensor, 100mV/A, Vcc/2 at no current
  voltage = (currentReading / 1024.0) * 5.0;
  current = (voltage - 2.5) / 0.1;

  ldrReading = analogRead(ldrPin);

  // Control the relay based on current threshold
  if (current > currentThreshold) {
    digitalWrite(relayPin, LOW);  // Turn off relay
  } else {
    digitalWrite(relayPin, HIGH);  // Turn on relay
  }
}

void tugrul() {

  // Read the state of the PIR sensor
  pirState = digitalRead(pirPin);
  Serial.println(pirState);

  // Read the state of the reed switch
  reedState = digitalRead(reedPin);
}

void onur() {
  DHT.read(tempPin);
  temperature = DHT.temperature;
}

void ilker() {
  //int datas[] = {current,ldrReading,pirState,reedState,temperature};
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Sensors data");
  lcd.setCursor(0, 1);
  lcd.print("will display");
  enes("Sensors data will be displayed", "\n", -1);
  delay(3500);
  lcd.clear();

  //Current display
  if (current > currentThreshold) {
    enes("Current is ", " higher than desired current. It will be shutting down.\n", current);
  } else {
    enes("Current is ", " in the desired range. Flow will go on.\n", current);
  }
  lcd.setCursor(0, 0);
  lcd.print("Instant current");
  lcd.setCursor(0, 1);
  lcd.print(current);
  lcd.setCursor(4, 1);
  lcd.print("Amp");
  delay(3500);

  //ldrstatus
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Light status");
  lcd.setCursor(0, 1);
  // Control the LED based on light threshold
  if (ldrReading < lightThreshold) {
    lcd.print("House is bright");
    enes("House is bright", "\n", -1);
  } else {
    lcd.print("House is dark");
    enes("House is dark", "\n", -1);
  }
  delay(2500);


  //pirState & reedState
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Motion status");
  lcd.setCursor(0, 1);
  if (pirState == HIGH) {
    lcd.print("Motion detected");
    enes("Motion detected in house", "\n", -1);
    delay(500);
  } else {
    lcd.print("No motion");
    enes("Motion didn't detect in house", "\n", -1);
  }
  delay(3800);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Window status");
  lcd.setCursor(0, 1);
  // Check if the door/window is open (assuming LOW means open due to pull-up)
  if (reedState == LOW) {
    lcd.print("Window opened!");
    enes("Window opened in house!", "\n", -1);
  } else {
    enes("Window is closed", "\n", -1);
    lcd.print("Window closed!");
  }
  delay(3800);
  //Temperature

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Heat status");
  lcd.setCursor(0, 1);
  lcd.print(String(temperature) + " C");
  if (temperature > tempThreshold) {
    delay(500);
    enes("House is ", " degree.\nPlease cool down the temperature for your own safety", temperature);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("House is hotter");
    lcd.setCursor(0,1);
    lcd.print("Than desired");
  } else {
    enes("House is ", " degree.\nIf you want you can warm up.", temperature);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("House is warm");
    lcd.setCursor(1, 0);
    lcd.print("As desired");
  }
}

void enes(String message1, String message2, int sensorData) {
  String messageToSend;

  if (sensorData != -1) {
    messageToSend = message1 + String(sensorData) + message2;
  } else {
    messageToSend = message1 + message2;
  }

  mySerial.println(messageToSend);
}
Editor is loading...
Leave a Comment