在這篇文章中,我們將使用 Arduino、16 x 2顯示器和實(shí)時(shí)時(shí)鐘模塊構(gòu)建一個(gè)自動(dòng)校鈴/大學(xué)鈴聲系統(tǒng)。您可以對(duì)此項(xiàng)目進(jìn)行編程,使其每天在您喜歡的時(shí)間和分鐘敲鐘多達(dá) 16次。鈴聲的長(zhǎng)度可以在幾秒鐘內(nèi)編程。
概述
當(dāng)學(xué)校的牡丹敲響“錫錫丁”的鈴聲時(shí),學(xué)生們飛快地跑出學(xué)校門口的日子已經(jīng)一去不復(fù)返了。當(dāng)牡丹在幾分鐘前敲響最后一聲鐘聲時(shí),有些人可能會(huì)更高興。
這是15到20年前的情況,但現(xiàn)在所有的學(xué)校和學(xué)院都嚴(yán)格限制時(shí)間,鐘聲是自動(dòng)化的。
作者的快速童年/青少年時(shí)期記得:
在我上小學(xué)和中學(xué)時(shí),我佩戴的數(shù)字手表與學(xué)校的鈴鐺系統(tǒng)同步,精度為1秒。
上課鈴響起后,我會(huì)大喊“下課鈴要響5秒了”,所有學(xué)生都驚訝地盯著我,這種情況幾乎每天都在發(fā)生。有一天,我和我的好朋友開始倒數(shù)10,9,8,7...在最后的鐘聲之前。
顯示到 Arduino 連接
顯示到 Arduino 連接與我們通常接線的略有不同,此處使用的引腳 9、8、7、6、5 和 4。引腳 2 和 3 通過(guò)按鈕用作硬件中斷。
使用 10K 電位計(jì)調(diào)整顯示器的對(duì)比度。
有關(guān)鐘形和繼電器連接的詳細(xì)信息:
更新:A5 到 SCL 和 A4 到 SDA(不是 A4 到 SCK)
實(shí)時(shí)時(shí)鐘模塊
實(shí)時(shí)時(shí)鐘模塊即使在長(zhǎng)時(shí)間斷電后也能跟蹤時(shí)間。提供 9V 繼電器,用于打開和關(guān)閉鈴鐺。
請(qǐng)?jiān)诶^電器上連接一個(gè)反向偏置的 1N4007 二極管(原理圖中未顯示),這將吸收繼電器有害的高壓反電動(dòng)勢(shì)。
使用9V / 500mA墻上適配器為電路供電。
提供了三個(gè)按鈕,一個(gè)用于在某些情況下手動(dòng)操作鈴鐺。按下“退出”按鈕將在手動(dòng)按鈴后停止鈴鐺。
“鈴鐺禁用按鈕”將永遠(yuǎn)禁用鈴鐺。要重新啟用鈴鐺,請(qǐng)按“退出”按鈕。
上傳下面的程序,它將時(shí)間設(shè)置為 RTC
//----------------------------------------------------//
#include 《Wire.h》
#include 《TimeLib.h》
#include 《DS1307RTC.h》
int P=A3; //Assign power pins for RTC
int N=A2;
const char *monthName[12] = {
“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”,
“Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”
};
tmElements_t tm;
void setup() {
pinMode(P,OUTPUT);
pinMode(N,OUTPUT);
digitalWrite(P,HIGH);
digitalWrite(N,LOW);
bool parse=false;
bool config=false;
// get the date and time the compiler was run
if (getDate( DATE ) && getTime( TIME )) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.print(“DS1307 configured Time=”);
Serial.print( TIME );
Serial.print(“, Date=”);
Serial.println( DATE );
} else if (parse) {
Serial.println(“DS1307 Communication Error :-{”);
Serial.println(“Please check your circuitry”);
} else {
Serial.print(“Could not parse info from the compiler, Time=”“);
Serial.print( TIME );
Serial.print(”“, Date=”“);
Serial.print( DATE );
Serial.println(”“”);
}
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, “%d:%d:%d”, &Hour, &Min, &Sec) != 3) return
false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, “%s %d %d”, Month, &Day, &Year) != 3) return
false;
for (monthIndex = 0; monthIndex 《 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex 》= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
//----------------------------------------------------//
After uploading the code, open the serial monitor, it will say that the
time is set.
Once the above step is accomplished successfully move on to next.
Now upload the below code to Arduino.
Main program Code:
//------------Program developed by R.GIRISH------------//
#include《EEPROM.h》
#include 《Wire.h》
#include 《TimeLib.h》
#include 《DS1307RTC.h》
#include 《LiquidCrystal.h》
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
int i = 0;
int H = 0;
int M = 0;
int S = 0;
int setting_value;
const int bell = 10;
const int P = A3;
const int N = A2;
const int setting_address = 0;
const int over_ride_off = 11;
boolean bell_status = true;
boolean Over_ride = true;
//------------------- Set Bell Timings from hours 1 to 23 hrs
-------------------//
//---- 1st bell ------//
const int h1 = 0; //hours
const int m1 = 0; //Minutes
//---- 2nd bell ------//
const int h2 = 0;
const int m2 = 0;
//---- 3rd bell ------//
const int h3 = 0;
const int m3 = 0;
//---- 4th bell ------//
const int h4 = 0;
const int m4 = 0;
//---- 5th bell ------//
const int h5 = 0;
const int m5 = 0;
//---- 6th bell ------//
const int h6 = 0;
const int m6 = 0;
//---- 7th bell ------//
const int h7 = 0;
const int m7 = 0;
//---- 8th bell ------//
const int h8 = 0;
const int m8 = 0;
//---- 9th bell ------//
const int h9 = 0;
const int m9 = 0;
//---- 10th bell ------//
const int h10 = 0;
const int m10 = 0;
//---- 11th bell ------//
const int h11 = 0;
const int m11 = 0;
//---- 12th bell ------//
const int h12 = 0;
const int m12 = 0;
//---- 13th bell ------//
const int h13 = 0;
const int m13 = 0;
//---- 14th bell ------//
const int h14 = 0;
const int m14 = 0;
//---- 15th bell ------//
const int h15 = 0;
const int m15 = 0;
//---- 16th bell ------//
const int h16 = 0;
const int m16 = 0;
//--------------- bell ring lenght in seconds -------//
const int Lenght = 3; //in seconds
//-------------------------- -------------------------//
void setup()
{
lcd.begin(16, 2);
pinMode(P, OUTPUT);
pinMode(N, OUTPUT);
pinMode(bell, OUTPUT);
pinMode(over_ride_off, INPUT);
digitalWrite(P, HIGH);
digitalWrite(N, LOW);
digitalWrite(over_ride_off, HIGH);
attachInterrupt(0, over_ride, RISING);
attachInterrupt(1, bell_setting, RISING);
if (EEPROM.read(setting_address) != 1)
{
bell_setting();
}
}
void loop()
{
tmElements_t tm;
lcd.clear();
if (RTC.read(tm))
{
H = tm.Hour;
M = tm.Minute;
S = tm.Second;
lcd.setCursor(0, 0);
lcd.print(“TIME:”);
lcd.print(tm.Hour);
lcd.print(“:”);
lcd.print(tm.Minute);
lcd.print(“:”);
lcd.print(tm.Second);
lcd.setCursor(0, 1);
lcd.print(“DATE:”);
lcd.print(tm.Day);
lcd.print(“/”);
lcd.print(tm.Month);
lcd.print(“/”);
lcd.print(tmYearToCalendar(tm.Year));
} else {
if (RTC.chipPresent())
{
lcd.setCursor(0, 0);
lcd.print(“RTC stopped?。?!”);
lcd.setCursor(0, 1);
lcd.print(“Run SetTime code”);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Read error!”);
lcd.setCursor(0, 1);
lcd.print(“Check circuitry!”);
}
}
if (EEPROM.read(setting_address) == 1)
{
if (H == 0 && M == 0 && S == 0)
{
digitalWrite(bell, LOW);
}
if (H == h1 && M == m1 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h2 && M == m2 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h3 && M == m3 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h4 && M == m4 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h5 && M == m5 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h6 && M == m6 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h7 && M == m7 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h8 && M == m8 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h9 && M == m9 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h10 && M == m10 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h11 && M == m11 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h12 && M == m12 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h13 && M == m13 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h14 && M == m14 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h15 && M == m15 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
if (H == h16 && M == m16 && S == 0)
{
for (i = 0; i 《 Lenght; i++)
{
digitalWrite(bell, HIGH);
delay(1000);
}
digitalWrite(bell, LOW);
i = 0;
}
}
delay(1000);
}
void over_ride()
{
lcd.clear();
while (Over_ride)
{
digitalWrite(bell, HIGH);
lcd.setCursor(0, 0);
lcd.print(“Press Exit to”);
lcd.setCursor(0, 1);
lcd.print(“Stop the bell?。。 保?
if (digitalRead(over_ride_off) == LOW)
{
Over_ride = false;
digitalWrite(bell, LOW);
}
}
Over_ride = true;
}
void bell_setting()
{
setting_value = 0;
EEPROM.write(setting_address, setting_value);
lcd.clear();
while (bell_status)
{
lcd.setCursor(0, 0);
lcd.print(“Bell is Disabled”);
lcd.setCursor(0, 1);
lcd.print(“Press Exit.”);
if (digitalRead(over_ride_off) == LOW)
{
bell_status = false;
}
}
bell_status = true;
setting_value = 1;
EEPROM.write(setting_address, setting_value);
}
//------------Program developed by R.GIRISH------------//
上傳上述代碼后,您應(yīng)該在顯示屏上看到以小時(shí)為單位的時(shí)間。
如何使用這個(gè)自動(dòng)鈴鐺系統(tǒng):
在完成硬件設(shè)置后執(zhí)行此操作。
1.先上傳“時(shí)間設(shè)置”代碼,然后打開串行監(jiān)視器。
2.在主程序中設(shè)置需要觸發(fā)繼電器的時(shí)間 這里。
//---- 1st bell ------//
const int h1 = 0; //hours
const int m1 = 0; //Minutes
//---- 2nd bell ------//
const int h2 = 0;
const int m2 = 0;
//---- 3rd bell ------//
const int h3 = 0;
const int m3 = 0;
//---- 4th bell ------//
const int h4 = 0;
const int m4 = 0;
? 從 1 到 1 小時(shí)設(shè)置 h23(小時(shí)),在 1 到 0 之間設(shè)置 m59(以分鐘為單位)。
? 與 h1 至 h16 和 m1 至 m16 相同。
? 如果要禁用某些鈴鐺離開值 h = 0 和 m = 0,例如:h5 = 0 和 m5 = 0,則零將禁用該特定鈴鐺。
3.設(shè)置鈴鐺打開和關(guān)閉的時(shí)間長(zhǎng)度,在這里:
---------------鐘聲長(zhǎng)度(以秒為單位) -------//
const int Lenght = 3;以秒為單位
默認(rèn)情況下,該值設(shè)置為 3 秒。當(dāng)?shù)竭_(dá)設(shè)定的時(shí)間時(shí),繼電器將打開 3 秒并關(guān)閉。如果需要,請(qǐng)更改此設(shè)置。
- 將修改后的代碼上傳到 Arduino。
- 要禁用鈴鐺,請(qǐng)按“鈴鐺禁用按鈕”。要重新啟用,請(qǐng)按“退出”按鈕。
6.要手動(dòng)按鈴,請(qǐng)按“手動(dòng)鈴開關(guān)”,要停止鈴聲,請(qǐng)按“退出”。
-
控制系統(tǒng)
+關(guān)注
關(guān)注
41文章
6622瀏覽量
110614 -
Arduino
+關(guān)注
關(guān)注
188文章
6469瀏覽量
187106 -
實(shí)時(shí)時(shí)鐘模塊
+關(guān)注
關(guān)注
0文章
8瀏覽量
1469
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論