#include <Adafruit_GFX.h> // Include the Adafruit Graphics Library
#include <Adafruit_ILI9341.h> // Include the Adafruit ILI9341 Library
#include <SPI.h>
#include <Keypad.h>
#include<DHT.h>
#define TFT_CS 5 // Chip select line for TFT display
#define TFT_RST 22 // Reset line for TFT
#define TFT_DC 17 // Data/command line for TFT
#define TFT_MOSI 23 // SPI MOSI pin
#define TFT_CLK 18 // SPI Clock pin
#define TFT_MISO 21 // SPI MISO pin
#define USONIC_TRG 16 // Trigger pin
#define USONIC_ECHO 35 // Echo pin
#define DHTPIN 4 // Pin for the DHT sensor
#define DHTTYPE DHT22 // DHT 11 (AM2302), DHT11
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect to the row pinouts of the keypad
byte rowPins[ROWS] = {32, 33, 25, 26};
// Connect to the column pinouts of the keypad
byte colPins[COLS] = {14, 27, 13};
// Create an instance of the Keypad class
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Create an instance of the ILI9341 display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
int isPassengerPresent = 0;
int currentFloor = 0;
int targetFloor = -1;
boolean stopRequested = false;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize SPI communication
SPI.begin(TFT_CLK, TFT_MISO, TFT_MOSI, TFT_CS);
// Initialize the ILI9341 display
tft.begin();
// Set rotation of the display
tft.setRotation(1);
// Fill the screen with a color
tft.fillScreen(ILI9341_BLACK);
// Set text size and color
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
// Initialize the ultrasonic sensor
pinMode(USONIC_TRG, OUTPUT);
pinMode(USONIC_ECHO, INPUT);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
if (isPassengerPresent == 0 && isPassengerDetected()) {
resetScreen();
tft.setCursor(10, 10);
tft.println("Hello! Which floor?");
isPassengerPresent = 1;
callDHTSensor();
delay(2000);
} else if (isPassengerPresent == 1) {
char key = keypad.getKey();
if (key) {
targetFloor = key - '0'; // Convert char to int (assuming valid input)
while (currentFloor != targetFloor) {
if (currentFloor < targetFloor) {
currentFloor++;
} else {
currentFloor--;
}
resetScreen();
tft.setCursor(10, 10);
tft.print("Passing floor: ");
tft.println(currentFloor);
for(int i = 1 ; i <= 10; i++){
delay(500);
char key = keypad.getKey();
Serial.println(key);
if(key =='#'){
targetFloor = getNearestFloor(currentFloor);
break;
}
}
}
resetScreen();
tft.print("Reached floor: ");
tft.println(targetFloor);
}
} else {
isPassengerPresent = 0;
tft.setCursor(10, 10);
tft.println("Waiting for passenger...");
delay(2000);
}
delay(500);
}
boolean isPassengerDetected() {
// Reset the sensor
digitalWrite(USONIC_TRG, LOW);
delayMicroseconds(2);
// Send a pulse
digitalWrite(USONIC_TRG, HIGH);
delayMicroseconds(10);
// Stop the pulse
digitalWrite(USONIC_TRG, LOW);
// Read the echo time
float duration = pulseIn(USONIC_ECHO, HIGH);
// Calculate distance in cm
float distance = duration * 0.034 / 2;
// Return true if distance is less than 100 cm
return distance < 100;
}
void resetScreen() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
}
void displayTemperatureHumidity(float temperature, float humidity) {
tft.fillRect(0, 200, 320, 40, ILI9341_BLACK); // Clear the bottom part of the screen
tft.setCursor(10, 200);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print("Temp: ");
tft.print(temperature);
tft.print(" C");
tft.setCursor(155, 200);
tft.print("Humidity: ");
tft.print(humidity);
tft.print(" %");
}
void callDHTSensor(){
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
// Print temperature and humidity data to the display
displayTemperatureHumidity(temperature, humidity);
}
}
int getNearestFloor(int currentFloor) {
int nearestFloor = 0;
int minDistance = abs(currentFloor - 0);
for (int i = 1; i <= 9; i++) {
int distance = abs(currentFloor - i);
if (distance < minDistance) {
nearestFloor = i;
minDistance = distance;
}
}
return nearestFloor;
}