//////////////////////////////////////////////
////////////// My Libraries //////////////
//////////////////////////////////////////////
/////////////////SERVO LIBRARY////////////////
#include <Servo.h>
Servo GATE;
Servo myservo;
///////// DHT LIBRARY &CONSTRACTION///////////
//#include <DHT.h>
#include <DHT_U.h>
#include "DHT.h"
#define DHTPIN 32
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//////////////////LIQUID CRYSTAL DISPLAY LCD//////////////
#include <LiquidCrystal.h>
const int rs= 2,en=3,d4=4, d5=5 , d6=6, d7=7;
LiquidCrystal lcd_1(rs, en, d4, d5, d6, d7);
/*#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); */ // set the LCD address to 0x27 for a 16 chars and 2 line display
///////////////////////////////////////////////////////////
#define REMOTEXY_MODE__HARDSERIAL
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial2
#define REMOTEXY_SERIAL_SPEED 115200
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] = // 400 bytes
{ 255,4,0,47,0,137,1,16,172,5,70,18,35,75,26,15,1,26,37,0,
70,18,35,59,26,15,1,26,37,0,70,18,35,43,26,15,1,26,37,0,
70,16,22,20,19,19,1,26,37,0,2,1,37,45,22,11,1,50,27,31,
31,79,78,0,79,70,70,0,2,1,37,61,22,11,1,50,27,31,31,79,
78,0,79,70,70,0,2,1,37,77,22,11,1,50,27,31,31,79,78,0,
79,70,70,0,1,0,24,22,15,15,1,50,31,71,97,116,101,0,129,0,
3,48,31,5,1,31,76,105,118,105,110,103,32,82,111,111,109,0,129,0,
3,80,25,5,1,31,66,101,100,32,82,111,111,109,0,129,0,3,64,18,
5,1,31,75,105,116,99,104,101,110,0,66,129,4,37,55,5,2,35,26,
129,0,4,31,42,5,2,31,84,101,109,112,101,114,97,116,117,114,101,32,
40,194,176,67,41,58,0,129,0,4,48,32,5,2,31,72,117,109,105,100,
105,116,121,32,40,37,41,58,0,66,129,4,54,55,5,2,177,26,67,5,
48,30,11,6,2,35,26,4,67,5,48,47,11,6,2,177,26,4,131,1,
4,1,24,7,1,161,31,67,111,110,116,114,111,108,0,131,0,32,1,30,
7,2,34,31,78,111,116,105,102,105,99,97,116,105,111,110,0,129,0,4,
66,20,5,2,31,71,97,122,32,40,37,41,58,0,66,129,4,72,55,5,
2,94,26,67,5,48,65,11,6,2,94,26,4,69,0,7,85,10,10,2,
1,69,0,26,85,10,10,2,177,69,0,45,84,10,10,2,94,70,16,48,
14,12,12,2,26,37,0,67,5,4,17,42,6,2,31,26,21,129,0,4,
13,27,4,2,31,77,111,116,105,111,110,32,83,116,97,116,117,115,58,0 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t living; // =1 if switch ON and =0 if OFF
uint8_t kitchen; // =1 if switch ON and =0 if OFF
uint8_t bed; // =1 if switch ON and =0 if OFF
uint8_t gate; // =1 if button pressed, else =0
// output variables
uint8_t led_bed; // led state 0 .. 1
uint8_t led_ktchn; // led state 0 .. 1
uint8_t led_lvng; // led state 0 .. 1
uint8_t led_gate; // led state 0 .. 1
int8_t level_temp; // =0..100 level position
int8_t level_humidity; // =0..100 level position
char text_temp[4]; // string UTF8 end zero
char text_humidity[4]; // string UTF8 end zero
int8_t level_gaz; // =0..100 level position
char text_gaz[4]; // string UTF8 end zero
int16_t sound_temp; // =0 no sound, else ID of sound, =1001 for example, look sound list in app
int16_t sound_hmdt; // =0 no sound, else ID of sound, =1001 for example, look sound list in app
int16_t sound_gaz; // =0 no sound, else ID of sound, =1001 for example, look sound list in app
uint8_t led_motion; // led state 0 .. 1
char text_motion[21]; // string UTF8 end zero
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
#define PIN_LIVING 36
#define PIN_KITCHEN 38
#define PIN_BED 40
#define PIN_GATE 44
const int trigPin = 9;
const int echoPin = 10;
const int motor = 11;
const int buzzer2 = 12; // Pin for the buzzer (adjust as needed)
const int ledPin1 = 19; // Pin for LED 1 (adjust as needed)
const int ledPin2 = 20; // Pin for LED 2 (adjust as needed)
const int ledPin3 = 13;
unsigned long startTime_servo = 0;
unsigned long interval_servo = 50;
int servo_angle = 0;
float distance;
int traveltime;
unsigned long previousMillis = 0;
const long interval = 200;
bool increasing = true; // To determine whether to increase or decrease servo_angle
///////////////////MY VARIABLES//////////////////
byte pir_pin =26;
byte pir_output =42;
byte buzzer= 22;
////////////////////////////////////////////
void setup()
{
myservo.attach(motor);
RemoteXY_Init ();
pinMode (PIN_LIVING, OUTPUT);
pinMode (PIN_KITCHEN, OUTPUT);
pinMode (PIN_BED, OUTPUT);
pinMode (PIN_GATE, OUTPUT);
pinMode(ledPin3 , OUTPUT);
// TODO you setup code
//////////////////LCD related command//////////////////
lcd_1.begin(16, 2);
// lcd.init();
//////////////////////////////////////////////////////
//////////////PIR Sensor related command//////////////
pinMode (pir_pin,INPUT_PULLUP);
pinMode (pir_output,OUTPUT);
//////////////PIR Sensor related command//////////////
//////////////DHT related command//////////////
dht.begin();
//////////////DHT related commandS END//////////////
/////////////////////////////////////////////////////
//Serial.begin(9600);
//////// GATE SERVO related commandS////////////////
GATE.attach(45);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer2, OUTPUT);
pinMode(ledPin1, OUTPUT); // Set ledPin1 as an output
pinMode(ledPin2, OUTPUT);
}
void loop()
{
RemoteXY_Handler ();
digitalWrite(PIN_LIVING, (RemoteXY.living==0)?LOW:HIGH);
digitalWrite(PIN_KITCHEN, (RemoteXY.kitchen==0)?LOW:HIGH);
digitalWrite(PIN_BED, (RemoteXY.bed==0)?LOW:HIGH);
digitalWrite(PIN_GATE, (RemoteXY.gate==0)?LOW:HIGH);
// TODO you loop code
// use the RemoteXY structure for data transfer
// do not call delay()
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the current time
// Read from ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
traveltime = pulseIn(echoPin, HIGH);
distance = traveltime * 0.034 / 2;
// Display the distance and servo angle on LCD
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("Distance: ");
lcd_1.print(distance);
lcd_1.print(" cm");
}
// Check distance conditions and control buzzer and LEDs
if (distance <= 370 && distance > 240) {
// Blink LED1 and tone buzzer at 2000 Hz
lcd_1.setCursor(0, 1);
lcd_1.print("Obstacle Found");
digitalWrite(ledPin1, HIGH); // Blink every 500 milliseconds
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
tone(buzzer2, 2000); // Adjust the frequency as needed
} else if (distance <= 240) {
// Turn off LED1, blink LED2, and tone buzzer at 2000 Hz
lcd_1.setCursor(0, 1);
lcd_1.print("Obstacle Found");
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH); // Blink every 500 milliseconds
digitalWrite(ledPin3, LOW);
tone(buzzer2, 2000); // Adjust the frequency as needed
} else {
// Turn off LED1, LED2, and stop the buzzer
lcd_1.setCursor(0, 1);
lcd_1.print("No Obstacle");
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
noTone(buzzer2);
}
// Your other non-blocking code can go here
unsigned currentTime = millis();
if (currentTime - startTime_servo >= interval_servo) {
startTime_servo = currentTime;
if (distance > 370) {
myservo.write(servo_angle);
if (increasing) {
servo_angle++;
if (servo_angle == 180) {
increasing = false;
}
} else {
servo_angle--;
if (servo_angle == 0) {
increasing = true;
}
}
} else {
// Stop the servo when the obstacle is detected
myservo.write(servo_angle);
}
}
//////////////////DHT Related commands//////////////
int h = dht.readHumidity();
int t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f))
{
// Serial.println(F("Failed to read from DHT sensor!"));
// return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
//////////////////////////////////////////////////////////
///////////////////////PIR Sensor////////////////////////
bool pir_status =digitalRead(pir_pin);
////////////////////////////////////////////////////////
////////////////////////indications////////////////////
RemoteXY.level_temp=t;
RemoteXY.level_humidity=h;
itoa(t, RemoteXY.text_temp, 10);
itoa (h, RemoteXY.text_humidity, 10);
int gaz =analogRead(A14);
gaz = map(gaz ,0,1023,0,100);
RemoteXY.level_gaz=gaz;
itoa (gaz, RemoteXY.text_gaz, 10);
if (h>70){RemoteXY.sound_hmdt=2002;} else {RemoteXY.sound_hmdt=0;}
if (t>30){RemoteXY.sound_temp=2004;} else {RemoteXY.sound_temp=0;}
if (gaz>30){RemoteXY.sound_gaz=2011; tone(buzzer,900);} else {RemoteXY.sound_gaz=0; noTone(buzzer);}
if ((pir_status)==0)
{
strcpy(RemoteXY.text_motion,"Thing Detected");
digitalWrite(pir_output,LOW);
RemoteXY.led_motion = HIGH;
}
else
{
strcpy(RemoteXY.text_motion," Thing is Away");
digitalWrite(pir_output,HIGH);
RemoteXY.led_motion = LOW;
}
if (digitalRead(PIN_KITCHEN)==0){RemoteXY.led_ktchn = HIGH;} else {RemoteXY.led_ktchn = LOW;}
if (digitalRead(PIN_BED)==0) {RemoteXY.led_bed = HIGH;} else {RemoteXY.led_bed = LOW;}
if (digitalRead(PIN_LIVING)==0) {RemoteXY.led_lvng = HIGH;} else {RemoteXY.led_lvng = LOW;}
//if (digitalRead(pir_pin)==0) {RemoteXY.led_motion = LOW;} else {RemoteXY.led_motion = HIGH;}
///////////////////////////LCD related command/////////////////////
//lcd.backlight();
/////////////////////////////////////////////////////
//Serial.println(gaz);
///////////////// GATE SERVO///////////////////////
if (digitalRead(PIN_GATE)==1)
{
RemoteXY.led_gate = HIGH;
GATE.write(90);
}
else
{
RemoteXY.led_gate = LOW;
GATE.write(2);
}
}
//////////////////////////////////////////////////