Arduino 闹钟仿真,可按键手动调时定时

Arduino 闹钟仿真,可手动调时定时请添加图片描述

调时定时
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

所用到的元器件有:arduino uno,4*4矩阵按键,lcd1602带iic,有源蜂鸣器,led灯,自锁开关,10K电阻


提示:以下是本篇文章正文内容,下面案例可供参考

一、仿真电路

请添加图片描述

二、仿真步骤

1.首先时间设置开关按下闭合,时间清零开关按下清零时间,时间设置是从小时设置开时,按下时分秒选择设置开关,然后跳到分设置,在按下时分秒选择设置开关,最后到秒设置,按下定时开关,定时时间显示在显示屏第2行,标准时间是第1行,然后时间设置开关按下打开,如果标准时间到定时时间,蜂鸣器响起,led灯亮起1分钟。
请添加图片描述
请添加图片描述

1.引入库

#include <Keypad.h>
#include <Wire.h>   
#include <LiquidCrystal_I2C.h>
lcd.setCursor("如有不懂的加作者Q:2188263281");   

2.程序

#include <Keypad.h>
#include <Wire.h>   
#include <LiquidCrystal_I2C.h>

//设置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

  • 6
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代坐电子工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值