計(jì)時(shí)器是一種用于測量時(shí)間間隔的時(shí)鐘。有兩種類型的計(jì)時(shí)器,一種從零開始向上計(jì)數(shù),用于測量經(jīng)過的時(shí)間,稱為秒表。并且,第二個從用戶提供的指定持續(xù)時(shí)間開始倒計(jì)時(shí),通常稱為倒數(shù)計(jì)時(shí)器。
在這里,在本教程中,我們將向您展示如何使用Arduino制作倒數(shù)計(jì)時(shí)器。
所需材料
Arduino UNO
液晶屏 16*2
4*4矩陣鍵盤
蜂鳴器
按鈕
電位器 (10k)
電阻器(10k,100歐姆)
連接線
Arduino倒數(shù)計(jì)時(shí)器電路圖
Arduino Uno在這里用作主控制器。鍵盤用于饋送持續(xù)時(shí)間,16*2 LCD用于顯示倒計(jì)時(shí)。按鈕用于啟動時(shí)間。
Arduino倒數(shù)計(jì)時(shí)器代碼和解釋
完整的Arduino定時(shí)器代碼在本項(xiàng)目結(jié)束時(shí)給出。
在下面的代碼中,我們將初始化鍵盤和LCD的庫以及代碼中使用的變量。
#include
#include
long int set1;
long int set2;
long int set3;
long int set4;
long int j;
int t1, t2, t3, t4, t5, t6;
int r1, r2, r3;
char key;
String r[8];
String hours;
String minutes;
String seconds;
現(xiàn)在,在下面的代碼中,我們正在初始化 no。用于定義鍵盤矩陣的行和列。
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
要將 4*4 矩陣鍵盤與 Arduino 連接,我們必須定義行和列的引腳。因此,在下面的代碼中,我們定義了鍵盤和 16x2 LCD 的引腳。
byte rowPins[ROWS] = { 6, 7, 8, 9 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins
byte colPins[COLS] = { 10, 11, 12, 13 };// Connect keypad COL0, COL1 and COL2 to t
LiquidCrystal lcd(A0, A1, 5, 4, 3, 2); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
下面的代碼用于制作鍵盤,
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
在 void setFeedingTime() 函數(shù)代碼中,按下按鈕后我們可以輸入計(jì)時(shí)器的時(shí)間,然后在輸入計(jì)時(shí)器持續(xù)時(shí)間后,我們必須按 D 開始倒計(jì)時(shí)。
void setFeedingTime()
{
feed = true;
int i=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set feeding Time");
lcd.clear();
lcd.print("HH:MM:SS");
lcd.setCursor(0,1);
while(1){
key = kpd.getKey();
char j;
if(key!=NO_KEY){
lcd.setCursor(j,1);
lcd.print(key);
r[i] = key-48;
i++;
j++;
if (j==2 || j == 5)
{
lcd.print(":"); j++;
}
delay(500);
}
if (key == 'D')
{key=0; break; }
}
lcd.clear();
}
在void setup()函數(shù)中,我們初始化了LCD和串行通信,并在下面的代碼中將引腳定義為INPUT和OUTPUT。
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A3, INPUT);
pinMode(A4, OUTPUT);
}
這個Arduino倒數(shù)計(jì)時(shí)器的工作很簡單,但代碼有點(diǎn)復(fù)雜。代碼由代碼中的注釋解釋。
最初,它將在LCD顯示屏上打印“ Arduino Timer”,直到您按下按鈕。一旦您按下按鈕,它將通過調(diào)用“setFeedingTime”函數(shù)要求進(jìn)入倒計(jì)時(shí)持續(xù)時(shí)間。然后,您可以在鍵盤的幫助下輸入持續(xù)時(shí)間。然后,您需要按“ D”以節(jié)省時(shí)間并開始倒數(shù)計(jì)時(shí)器。在void loop()函數(shù)中,我們做了一些計(jì)算來逐秒遞減時(shí)間,并根據(jù)剩余時(shí)間顯示小時(shí),分鐘和秒(HH:MM:SS)的正確值。所有代碼都通過注釋進(jìn)行了很好的解釋。您可以查看下面的完整代碼和演示視頻。
當(dāng)計(jì)時(shí)器達(dá)到零時(shí),蜂鳴器開始發(fā)出嗶嗶聲并僅發(fā)出 100 次嗶嗶聲(根據(jù)代碼)。要停止蜂鳴器,請按住按鈕。您可以隨時(shí)使用按鈕在計(jì)數(shù)之間停止計(jì)時(shí)器。
// Arduino Countdown Timer Code
#include
#include
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
long int set1;
long int set2;
long int set3;
long int set4;
long int j;
String hours;
String minutes;
String seconds;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = { 6, 7, 8, 9 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins
byte colPins[COLS] = { 10, 11, 12, 13 };// Connect keypad COL0, COL1 and COL2 to t
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(A0, A1, 5, 4, 3, 2); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
int t1, t2, t3, t4, t5, t6;
int r1, r2, r3;
boolean feed = true; // condition for alarm
char key;
String r[8];
void setFeedingTime()
{
feed = true;
int i=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set feeding Time");
lcd.clear();
lcd.print("HH:MM:SS");
lcd.setCursor(0,1);
while(1){
key = kpd.getKey();
char j;
if(key!=NO_KEY){
lcd.setCursor(j,1);
lcd.print(key);
r[i] = key-48;
i++;
j++;
if (j==2 || j == 5)
{
lcd.print(":"); j++;
}
delay(500);
}
if (key == 'D')
{key=0; break; }
}
lcd.clear();
}
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A3, INPUT);
pinMode(A4, OUTPUT);
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Arduino Timer");
//Serial.println(A3);
if (digitalRead(A3)==1) //
{
lcd.clear();
setFeedingTime();
for(int i = 0; i < 6; i++) // this for loop is used to get the value of the feeding time and print it serially
{
Serial.print(r[i]);
Serial.println();
}
hours = String (r[0]) + String (r[1]) ; //combining two separate int values of r[0] and r[1] into one string and save it to "hours"
minutes = String (r[2]) + String (r[3]) ; //combining two separate int values of r[2] and r[3] into one string and save it to "minutes"
seconds = String (r[4]) + String (r[5]) ; //combining two separate int values of r[4] and r[5] into one string and save it to "seconds"
set1 = (hours.toInt()*3600); //converting hours into seconds
set2 = (minutes.toInt() * 60); //converting minutes into seconds
set3 = seconds.toInt();
set4 = (hours.toInt() * 3600)+ (minutes.toInt() * 60) + seconds.toInt(); //adding set1, set2 and set3 together in set4
Serial.print("set4");
Serial.print(set4);
Serial.println();
lcd.setCursor(0,0);
lcd.print("Countdown begins");
delay(1000);
lcd.clear();
for(long int j = set4; j >= 0; j--) // this for loopis used to decrease the total time in seconds
{
Serial.println(j);
lcd.setCursor(0,0);
lcd.print("HH:MM:SS");
long int HH = j / 3600; // converting the remaining time into remaining hours
lcd.setCursor(0,1);
Serial.println(HH);
if (HH < 10) { lcd.print('0'); }
lcd.print(HH);
lcd.print(":");
long int MM = (j - (HH*3600))/60 ; //converting the remaining time into remaining minutes
lcd.setCursor(3,1);
Serial.println(MM);
if (MM < 10) { lcd.print('0'); }
lcd.print(MM);
lcd.print(":");
long int SS = j - ((HH*3600)+(MM*60)); //converting the remaining time into remaining seconds
lcd.setCursor(6,1);
Serial.println(SS);
if (SS < 10) { lcd.print('0'); }?
lcd.print(SS);
delay(1000);
if (digitalRead(A3)==1){break;}
if (j == 0)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Timer Stop");
lcd.setCursor(2,1);
lcd.print("-Ring-Ring-");
for(int k =0; k<= 100; k++) //this for loop is used for the buzzer to beep 100 time as the timer reaches zero
{
digitalWrite(A4,HIGH);
delay(300);
digitalWrite(A4,LOW);
delay(300);
if (digitalRead(A3)==1){break;}
}
}
}
}
}
-
計(jì)時(shí)器
+關(guān)注
關(guān)注
1文章
416瀏覽量
32605 -
Arduino
+關(guān)注
關(guān)注
187文章
6458瀏覽量
186504
發(fā)布評論請先 登錄
相關(guān)推薦
評論