#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Wire.h>
#include <UniversalTelegramBot.h>
#define ECHO_PIN 14
#define TRIG_PIN 15
#define SERVO_PIN 16
#define LCD_ADDRESS 0x27
#define LCD_COLS 16
#define LCD_ROWS 2
#define BOT_TOKEN "6375860078:AAEFNmCu68IxsN2Rb0bKX0rUlBoqnWJ-sCY"
#define CHAT_ID "5558733865"
Servo servo;
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLS, LCD_ROWS);
UniversalTelegramBot bot(BOT_TOKEN);
bool isGarageOpen = false;
void setup() {
Serial.begin(115200);
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.print("Garage Door");
lcd.setCursor(0, 1);
lcd.print("Simulator");
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
servo.attach(SERVO_PIN);
servo.write(90); // Set the initial position to 90 degrees (closed).
delay(2000); // Allow time to read the LCD
lcd.clear();
}
void loop() {
float distance = readDistanceCM();
lcd.clear();
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
checkTelegramCommands(); // Check for Telegram commands
// Send status and distance info to Telegram
sendTelegramStatus(distance);
delay(500); // Adjust the delay based on your needs
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void openGarage() {
lcd.setCursor(0, 1);
lcd.print("Opening Garage");
servo.write(0); // Adjust the angle based on your servo's range
delay(2000); // Adjust the delay based on your needs
isGarageOpen = true;
}
void closeGarage() {
lcd.setCursor(0, 1);
lcd.print("Closing Garage");
servo.write(90); // Set to the closed position
delay(2000); // Adjust the delay based on your needs
isGarageOpen = false;
}
void checkTelegramCommands() {
String command = bot.getCommand();
if (command.equals("/open")) {
openGarage();
notifyTelegram("Garage door is opened!");
} else if (command.equals("/close")) {
closeGarage();
notifyTelegram("Garage door is closed!");
}
}
void sendTelegramStatus(float distance) {
String statusMessage = "Garage is " + (isGarageOpen ? "open" : "closed") +
"\nDistance to object: " + String(distance) + " cm";
bot.sendMessage(CHAT_ID, statusMessage);
}
void notifyTelegram(const char* message) {
bot.sendMessage(CHAT_ID, message);
}