#include <Wire.h>
#include <RTClib.h>
#include <Servo.h>
RTC_DS1307 rtc; // DS1307 RTC modülü için nesne oluştur
Servo servo; // Servo motor nesnesi oluştur
// ----------------------
// pins
const int servoPin = 9; // Servo motorun bağlı olduğu pin
const int buttonPin = 2; // Buton pin numarası
const int ledPin = 4; // Buton pin numarası
// settings
int openAngle = 90; // Servo motorun açılacağı açı - yatay acık
int closeAngle = 0; // Servo motorun kapanacağı açı - dikey kapalı
int openServoDate_Hour = 15; // servonun acılacağı saat
int openServoDate_Minute = 55; // servonun acılacağı dakika
int closeServoDate_Hour = 15; // servonun kapancağı saat
int closeServoDate_Minute = 55; // servonun kapancağı dakika
// status
int servoPosition = closeAngle; // Servo motor konumu (false: 0 derece, true: 90 derece)
int buttonState = 0; // Buton durumu için değişken
int lastButtonState = 0; // Önceki buton durumu
// ----------------------
// start
void setup() {
Serial.begin(9600);
// start RTC
if (!rtc.begin()) {
Serial.println("RTC bağlantısı başarısız!");
while (1);
}
if (!rtc.isrunning()) {
// If RTC is not working, adjust the clock
Serial.println("RTC çalışmıyor, saat ayarlanıyor!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Arduino'nun derleme zamanına göre saati ayarlar
}
// attach pins
servo.attach(servoPin); // Connect servo motor to pin
pinMode(buttonPin, INPUT); // Buton pin giriş olarak ayarlanır
pinMode(ledPin, OUTPUT); // led pin
// first test
servo.write(servoPosition); //Initial servo position
// delay(1000); // 1sn wait
// openServo(); // open
// delay(1000); // 1sn wait
// closeServo(); // close
}
void loop() {
buttonState = digitalRead(buttonPin); // Buton durumu okunur
// Butona basıldığında ve önceki durumda basılmamışsa
if (buttonState == 1 && lastButtonState == 0) {
// led gösterge
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
// Servo konumunu değiştir
if (servoPosition == closeAngle) {
openServo();
} else {
closeServo();
}
// Butonun bırakılmasını beklemek için kısa bir gecikme eklenir
delay(50);
}
lastButtonState = buttonState; // Buton durumunu güncelle
currentDate(); // debug
// checkTimeServo();
delay(1000);
}
void openServo() {
// servo is open
if (servoPosition == closeAngle){
servo.write(openAngle);
// Reverse servo position
servoPosition = openAngle;
}
}
void closeServo() {
// servo is close
if (servoPosition == openAngle){
servo.write(closeAngle);
// Reverse servo position
servoPosition = closeAngle;
}
}
void checkTimeServo(){
DateTime now = rtc.now(); // get RTC data
// Servo on/off at specific time interval
if (now.hour() == openServoDate_Hour && now.minute() == openServoDate_Minute) {
openServo();
}else if (now.hour() == closeServoDate_Hour && now.minute() == closeServoDate_Minute){
closeServo();
}
}
void currentDate(){
DateTime now = rtc.now(); // get RTC data
Serial.print("Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
}