#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Inštancia pre servo motor
Servo servo;
// Inštancia pre LCD displej s I2C adresou 0x27 (upravte, ak máte iné nastavenie) a veľkosťou 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Konštanty pre uhly serva
const int angle_45 = 45;
const int angle_90 = 90;
// Konštanty pre trvanie serva v milisekundách
const long duration = 2000; // 2 sekundy
// Premenná pre sledovanie posledného otočenia serva
long last_rotation_time = 0;
// Vlajky na spracovanie v hlavnom cykle
volatile bool int0_flag = false;
volatile bool int1_flag = false;
// Funkcia prerušenia INT0 (na pinu 2)
void int0Handler() {
int0_flag = true; // Nastaví vlajku, že INT0 bolo spustené
}
// Funkcia prerušenia INT1 (na pinu 3)
void int1Handler() {
int1_flag = true; // Nastaví vlajku, že INT1 bolo spustené
}
void setup() {
// Inicializácia servo motora
servo.attach(9);
servo.write(0); // Servo motor je nastavený na nulovú polohu na začiatku
// Inicializácia LCD displeja s I2C rozhraním
lcd.init();
lcd.backlight(); // Zapnutie podsvietenia LCD displeja
// Nastavenie prerušení pre INT0 a INT1
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
// Konfigurácia prerušení
attachInterrupt(digitalPinToInterrupt(2), int0Handler, FALLING);
attachInterrupt(digitalPinToInterrupt(3), int1Handler, FALLING);
// Povolenie globálnych prerušení
sei(); // Enable interrupts
}
void loop() {
// Spracovanie vlajky prerušenia INT0
if (int0_flag) {
// Reset vlajky
int0_flag = false;
// Natočenie serva na 45 stupňov
servo.write(angle_45);
last_rotation_time = millis();
// Vyčistenie displeja a zobrazenie uhlu
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Uhol: 45");
}
// Spracovanie vlajky prerušenia INT1
if (int1_flag) {
// Reset vlajky
int1_flag = false;
// Natočenie serva na 90 stupňov
servo.write(angle_90);
last_rotation_time = millis();
// Vyčistenie displeja a zobrazenie uhlu
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Uhol: 90");
}
// Kontrola času od posledného otočenia serva
if (millis() - last_rotation_time >= duration) {
// Vrátenie serva do nulovej polohy
servo.write(0);
// Vyčistenie displeja a zobrazenie správnej polohy
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Servo v polohe 0");
}
}