Bilgiler > Arduino RF Kapı Kumandasini Bluetooth ve RTC ile kontrol Devresi Kodlari
Arduino RF Kapı Kumandasini Bluetooth ve RTC ile kontrol Devresi Kodlari
#include "SoftwareSerial.h"
#include "TimerOne.h"
#include "RCSwitch.h"
#include "Wire.h"
#define DS1307_ADDRESS 0x68
RCSwitch mySwitch = RCSwitch();
int data,dataold;
int led=13;
//saat degiskenleri
int second =0;
int minute =0;
int hour = 0; //24 hour time
int weekDay = 0; //0-6 -> sunday - Saturday
int monthDay =0;
int month =0;
int year = 0;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
//RTC AYAR
Wire.begin();
Serial.begin(9600);
mySerial.begin(9600);
pinMode(led,OUTPUT);
Serial.begin(19200);
digitalWrite(led,0);
Timer1.initialize(600000000); // on dakika
Timer1.attachInterrupt( timerIsr );
mySwitch.enableTransmit(2);
// Optional set pulse length.
mySwitch.setPulseLength(504);
// Optional set protocol (default is 1, will work for most outlets)
// mySwitch.setProtocol(2);
// Optional set number of transmission repetitions.
mySwitch.setRepeatTransmit(10);
}
void loop() {
if (mySerial.available())
{
data = mySerial.read();
if(data!=0)
Serial.println(data);
if(data==1) //istiklal marşı başlat/ durdur
mars();
//kapıyı aç
if(data==2)
ac();
//kapıyı kapat
if(data==3)
kapat();
//kapıyı durdur
if(data==4)
durdur();
}
}
//SAAT icin donusum
byte bcdToDec(byte val)
{
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
//saat okuma
void saatoku()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
monthDay = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
printDate();
}
void printDate()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
//print the date EG 3/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
void kapat()
{
durdur();
mySwitch.send(22146, 24);//kapat
Serial.println(22146);
delay(1);
digitalWrite(led,LOW);
}
void ac()
{
durdur();
mySwitch.send(22145, 24);//ac
Serial.println(22145);
delay(1);
digitalWrite(led,HIGH);
}
void durdur()
{
mySwitch.send(22148, 24);
Serial.println(22148);
delay(1);
}
void mars()
{
mySwitch.send(15977475, 24);
Serial.println(15977475);
delay(1);
}
//10 dakika da bir kapıyı kapat. Kapının açık kalmasını engelle
void timerIsr()
{
saatoku();
//saat ve gune gore islem yap
if(weekDay==0||weekDay==6)
{
kapat();
}
else
{
if(hour==8||hour==13||hour==16)
ac();
else
kapat();
}
}
***