调时定时
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:这里可以添加本文要记录的大概内容:
所用到的元器件有:arduino uno,4*4矩阵按键,lcd1602带iic,有源蜂鸣器,led灯,自锁开关,10K电阻
提示:以下是本篇文章正文内容,下面案例可供参考
1.首先时间设置开关按下闭合,时间清零开关按下清零时间,时间设置是从小时设置开时,按下时分秒选择设置开关,然后跳到分设置,在按下时分秒选择设置开关,最后到秒设置,按下定时开关,定时时间显示在显示屏第2行,标准时间是第1行,然后时间设置开关按下打开,如果标准时间到定时时间,蜂鸣器响起,led灯亮起1分钟。
#include
#include
#include
lcd.setCursor("如有不懂的加作者Q:2188263281");
#include
#include
#include
//设置LCD1602设备地址,这里的地址是0x27,一般是0x20,或者0x3F,具体看模块手册
LiquidCrystal_I2C lcd(0x27,20,4);
const byte ROWS = 4; //四行
const byte COLS = 4; //四列
//定义键盘上的按键标识
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //连接到行扫描的输入输出端口
byte colPins[COLS] = {6, 7, 8, 9}; //连接到列扫描的输入输出端口
//定义Keypad类的实例
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const int ledPin = 11; //led引脚
const int buttonPin = 10; //按键引脚
int buttonState = 0; //按键状态
//时分秒变量
int a;
int h = 0; //编辑时间
int f = 0;
int s = 0;
int h1 = 0; //标准时间
int f1 = 0;
int s1 = 0;
int h2 = 0; //定时时间
int f2= 0;
int s2 = 0;
void setup(){
Serial.begin(9600); //波特率9600
lcd.init(); // 初始化LCD
lcd.backlight();//设置LCD背景等亮
pinMode(buttonPin, INPUT);//按键输入
pinMode(ledPin, OUTPUT);//led输出
}
void loop(){
char customKey = customKeypad.getKey(); //变量customKey等于矩阵按键标识
buttonState = digitalRead(buttonPin);//读取编辑按键电平值并赋值给buttonState
if (buttonState == HIGH) //编辑按键按下时
{
if (customKey) //如果矩阵按键按下
{
if(customKey == 'C') //如果矩阵按键等于‘C',a自加,只能自加到2
{
a+=1;
if(a > 2)
{
a = 2;
}
}else if(customKey == 'B') //如果矩阵按键等于‘B',a自减,只能自减到0
{
a-=1;
if(a < 0)
{
a = 0;
}
}
if(a == 0)
{
if(customKey == '1')
{
h = h *10+1;
}else if(customKey == '2')
{
h = h *10+2;
}else if(customKey == '3')
{
h = h *10+3;
}else if(customKey == '4')
{
h = h *10+4;
}else if(customKey == '5')
{
h = h *10+5;
}else if(customKey == '6')
{
h = h *10+6;
}else if(customKey == '7')
{
h = h *10+7;
}else if(customKey == '8')
{
h = h *10+8;
}else if(customKey == '9')
{
h = h *10+9;
}else if(customKey == '0')
{
h = h *10+0;
}
lcd.setCursor(1,0); //显示小时时间
lcd.print(0);
lcd.setCursor(4,0);
lcd.print(0);
lcd.setCursor(1,0);
lcd.print( h);
}else if(a == 1)
{
if(customKey == '1')
{
f = f *10+1;
}else if(customKey == '2')
{
f = f *10+2;
}else if(customKey == '3')
{
f = f *10+3;
}else if(customKey == '4')
{
f = f *10+4;
}else if(customKey == '5')
{
f = f *10+5;
}else if(customKey == '6')
{
f = f *10+6;
}else if(customKey == '7')
{
f = f *10+7;
}else if(customKey == '8')
{
f = f *10+8;
}else if(customKey == '9')
{
f = f *10+9;
}else if(customKey == '0')
{
f = f *10+0;
}
s = 0;
lcd.setCursor(4,0);//显示分钟时间
lcd.print(0);
lcd.setCursor(7,0);
lcd.print(0);
lcd.setCursor(4,0);
lcd.print(f);
}else if(a == 2)
{
if(customKey == '1')
{
s = s *10+1;
}else if(customKey == '2')
{
s = s *10+2;
}else if(customKey == '3')
{
s = s *10+3;
}else if(customKey == '4')
{
s = s *10+4;
}else if(customKey == '5')
{
s = s *10+5;
}else if(customKey == '6')
{
s = s *10+6;
}else if(customKey == '7')
{
s = s *10+7;
}else if(customKey == '8')
{
s = s *10+8;
}else if(customKey == '9')
{
s = s *10+9;
}else if(customKey == '0')
{
s = s *10+0;
}
lcd.setCursor(7,0);//显示秒时间
lcd.print(s);
}
if(customKey == '*') //时间复位
{
h = 0;
f = 0;
s = 0;
a = 0;
lcd.setCursor(1,0);
lcd.print(" ");
lcd.setCursor(1,0);
lcd.print(h);
lcd.setCursor(3,0);
lcd.print(":");
lcd.setCursor(4,0);
lcd.print(f);
lcd.setCursor(6,0);
lcd.print(":");
lcd.setCursor(7,0);
lcd.print(s);
}
if(customKey == 'A') //确认时间
{
h1 = h;
f1 = f;
s1 = s;
}else if(customKey == 'D') //定时
{
h2 = h;
f2 = f;
s2 = s;
lcd.setCursor(1,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print(h2);
lcd.setCursor(3,1);
lcd.print(":");
lcd.setCursor(4,1);
lcd.print(f2);
lcd.setCursor(6,1);
lcd.print(":");
lcd.setCursor(7,1);
lcd.print(s2);
}
}
}
else
{
s1+=1;
if(h1 == h2 && f1 == f2 )
{
digitalWrite(ledPin, HIGH);
}else
{
digitalWrite(ledPin, LOW);
}
if(s1 > 60)
{
s1 = 0;
f1+=1;
}else if(f1 > 60)
{
f1 = 0;
h1+=1;
}else if(h1 > 24)
{
h1 = 0;
}
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(1,0);
lcd.print(h1);
lcd.setCursor(3,0);
lcd.print(":");
lcd.setCursor(4,0);
lcd.print(f1);
lcd.setCursor(6,0);
lcd.print(":");
lcd.setCursor(7,0);
lcd.print(s1);
delay(1000);
}
}
提示:这里对文章进行总结:
仿真下载链接:https://download.csdn.net/download/qq_38234828/85287998