电子说
厨房定时器用户界面流程
打开电源后,设备将显示“Arduino Kitchen Timer”消息3秒钟。
然后计时器将提示您设置时间。您可以通过按左右键将光标移动到分钟和小时。
您可以使用向上和向下箭头键调整分钟和小时设置。
设置了所需的时间后,按下选择按钮,计时器将启动。
如果您想再次设置时间,请再次按下选择按钮。
一旦时间结束,蜂鸣器将发出蜂鸣声。
要停止蜂鸣器,请按键盘护罩上的重置按钮。
厨房定时器所需的组件
Arduino
LCD键盘护盾
蜂鸣器
厨房定时器的威廉希尔官方网站 图
首先,对齐并放置L CD Keypad直接屏蔽Arduino。然后将蜂鸣器的正极连接到Arduino上的引脚12,将蜂鸣器的负极连接到地面。
Arduino厨房定时器项目代码
将以下代码复制并上传到Arduino IDE中。代码的每个部分都有一个附带的解释,以帮助您了解它的功能。
#include
// select the pins used for the LCD keypad
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some variables
int lcd_key = 0;
int adc_key_in = 0;
int hrs = 0;
int mins = 0;
int set_mins = 0;
int set_hrs = 1;
int secs = 60;
int cursor_pos = 1;
int buzzer_pin = 12;
bool startTimer = false;
bool setTimer = true;
bool get_time = false;
unsigned long interval=1000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
// Defining button used by the lcd keypad
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // reading the button value from the lcd keypad
// checking which button is pressed
if (adc_key_in 》 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in 《 50) return btnRIGHT;
if (adc_key_in 《 250) return btnUP;
if (adc_key_in 《 450) return btnDOWN;
if (adc_key_in 《 650) return btnLEFT;
if (adc_key_in 《 850) return btnSELECT;
return btnNONE; // when all others fail, return this.。.
}
void setup()
{
Serial.begin(115200);
pinMode(buzzer_pin, OUTPUT);
lcd.begin(16, 2); // start communication with LCD keypad shield
lcd.setCursor(0,0);
lcd.print(“Arduino Kitchen”);
lcd.setCursor(0, 1);
lcd.print(“ Timer”);
delay(3000);
}
void loop(){
// Checking which condition is true based on the button pressed
if(startTimer == true){
start_timer();
}
else if (setTimer == true){
set_timer();
}
}
// This function will count the time and will beep the buzzer when the time will be up.
void start_timer(){
// Checking whether time is up or not
if(hrs == 0 && mins == 0 && secs == 0){
lcd.setCursor(0, 0);
lcd.print(“ Time is UP”);
lcd.setCursor(0, 1);
lcd.print(“ Beep Beep”);
digitalWrite(buzzer_pin, HIGH);
delay(500);
digitalWrite(buzzer_pin, LOW);
delay(500);
}
else if(secs 《 0){
secs = 59;
mins = mins - 1;
}
else if(mins 《 0){
mins = 59;
hrs = hrs - 1;
}
else
{
get_time = true;
counter();
lcd.setCursor(0, 0);
lcd.print(“Timer is ON”);
lcd.setCursor(0, 1);
lcd.print(hrs);
lcd.print(“:”);
lcd.setCursor(4, 1);
lcd.print(mins);
lcd.print(“:”);
lcd.setCursor(8, 1);
lcd.print(secs);
}
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
// if select button is pressed, move back to set time
case btnSELECT:
{
startTimer = false;
setTimer = true;
delay(300);
lcd.clear();
break;
}
case btnNONE:
{
break;
}
}
}
// This function will set the time
void set_timer(){
counter();
lcd.setCursor(0, 0);
lcd.print(“Set Time”);
lcd.setCursor(0, 1);
lcd.print(“Hrs:”);
lcd.print(hrs);
lcd.setCursor(8, 1);
lcd.print(“Mins:”);
lcd.print(mins);
lcd.setCursor(0,1);
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
// if right button is pressed, then move the cursor to minutes
case btnRIGHT:
{
cursor_pos = set_mins;
break;
}
// if left button is pressed, then move the cursor to hours
case btnLEFT:
{
cursor_pos = set_hrs;
break;
}
// if up button is pressed, add 1 to the minutes or hours
case btnUP:
{
delay(300);
if(cursor_pos == set_mins){
mins++;
if(mins 》 59){
mins = 0;
}
}
else if(cursor_pos == set_hrs){
hrs++;
if(hrs 》 24){
hrs = 0;
}
}
break;
}
// if down button is pressed, minus 1 from the minutes or hours
case btnDOWN:
{
delay(300);
if(cursor_pos == set_mins){
mins--;
if(mins 《 0){
mins = 60;
}
}
else if(cursor_pos == set_hrs){
hrs--;
if(hrs 《 0){
hrs = 24;
}
}
break;
}
// if select button is pressed, start the timer
case btnSELECT:
{
startTimer = true;
setTimer = false;
mins = mins - 1;
delay(300);
break;
}
case btnNONE:
{
break;
}
}
}
void counter() {
unsigned long currentMillis = millis(); // grab current time
// check if “interval” time has passed (1000 milliseconds)
if ((unsigned long)(currentMillis - previousMillis) 》= interval) {
lcd.clear();
if(get_time == true){
secs--;
get_time = false;
}
previousMillis = millis();
}
}
创建厨房计时器只是一个开始!
您已经创建了自己的厨房计时器!该项目的最佳部分是能够调整该模块以构建其他项目,这些项目需要LCD和按钮或蜂鸣器之间的简单接口。您还可以为模块设计自定义3D打印的外壳,并使其成为您自己的。
全部0条评论
快来发表一下你的评论吧 !