【Arduino综合项目】小闹钟

您所在的位置:网站首页 小闹钟怎么设定时间 【Arduino综合项目】小闹钟

【Arduino综合项目】小闹钟

2024-07-16 20:53| 来源: 网络整理| 查看: 265

小闹钟项目

在额济纳支教这一年,给孩子们上了很多Arduino开发的课,也带他们完成了几个Arduino综合项目,下面陆续把这些小项目放上来,欢迎围观~~~非科班出身,C语言学的不好,望各路大神批评指点。

功能介绍

本项目主要是实现基本的闹钟功能:

LCD屏幕实时显示当前时间(年、月、日、星期、时、分、秒)以及闹钟的开关; 通过4*4键盘可以设置初始时间及闹钟时间; 通过键盘,还可以查看当前设置的闹钟时间; 当闹钟时间到了,蜂鸣器便会响起,同时LED亮起,可以通过键盘关闭闹钟。 准备材料 Arduino UNO *1 LCD 1602A *1 LCD1602液晶屏转接板(PCF8574AT) *1 4*4 矩阵键盘 *1 RTC I2C时钟模块(DS1307) 蜂鸣器 *1 LED灯 *1 面包板 *1 跳线若干 电烙铁、松香、焊锡等 就不给出购买链接了,这些材料在某宝上随便一搜就有~ 模块接线 键盘与Arduino

R1-C4 -> 2-9

键盘与Arduino接线示意图 LCD 1602与转接板

如下图把转接板与LCD1602焊接在一起就行了,或者也可以插在面包板上。

LCD 1602与转接板焊接图 转接板与Arduino PCF8574T Arduino GND -> GND VCC -> 5V SDA -> A4 SCL -> A5 DS1307时钟模块与Arduino DS1307 Arduino GND -> GND VCC -> 5V SDA -> AREF上一个口 SCL -> AREF上两个口 DS1307时钟模块与Arduino接线示意图 蜂鸣器、LED灯与Arduino 蜂鸣器、LED灯 Arduino 负极 -> GND 正极 -> 10 第三方库 #include #include "RTClib.h" #include #include

下载地址:

SCoop.h->https://github.com/fabriceo/SCoop RTClib.h->https://github.com/adafruit/RTClib Keypad.h-> http://playground.arduino.cc/uploads/Code/keypad.zip LiquidCrystal_I2C.h->https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads 源程序 /*-----( Import needed libraries )-----*/ #include #include #include #include "RTClib.h" #include #include void formatPrint( int hour, int minute, int second); String show_s(int a,int b,int c,int d); void menu(); char set_time(); char input_show(int x); char set_time_year(); char set_time_month(); char set_time_day(); char set_time_hour(); char set_time_minute(); char set_time_second(); char set_clock(); char set_clock_hour(); char set_clock_minute(); char set_clock_second(); LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack char y[4]; //存储year的数值 int i=0;//循环计数 /**初始时间**/ int py; //year int pmo; //month int pd; //day int ph; //hour int pmi; //minute int ps; //second /**闹钟时间**/ //int cy = 2015; //year //int cmo = 5; //month //int cd = 21; //day int ch = 00; //hour int cmi = 00; //minute int cs = 00; //second char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; int menu_flag = 0; int show_flag = 1; //1是显示,-1是不显示 int switch_flag = 1; //1是开,-1是关 const byte ROWS = 4; //four rows const byte COLS = 4; //three columns char keys[ROWS][COLS] = { {'0','1','2','3'}, {'4','5','6','7'}, {'8','9','A','B'}, {'C','D','E','F'} }; byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad //initializes an instance of the Keypad class Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); /*-----( Declare objects )-----*/ RTC_DS1307 rtc; // Create a RealTimeClock object defineTask(Task1) void Task1::setup() { Serial.begin(9600); pinMode(10,OUTPUT); } void Task1::loop() { char key = keypad.getKey(); if (key == 'F'){ show_flag = -show_flag; menu(); show_flag = 1; }else if(key == 'C'){ switch_flag = -switch_flag; noTone(10); }else if(key == 'D'){ show_flag = -show_flag; lcd.clear(); lcd.noBlink(); lcd.setCursor(3,0); lcd.print("Alarm Clock"); formatPrint(ch, cmi, cs); if(keypad.waitForKey()) show_flag = -show_flag; } DateTime nowClock = rtc.now(); // Read data from the RTC Chip if(nowClock.hour() == ch && nowClock.minute() == cmi && nowClock.second() == cs && switch_flag == 1) { tone(10, 31); key = keypad.getKey(); if(key == 'C'){ switch_flag = -switch_flag; noTone(10); } } } void setup() /****** SETUP: RUNS ONCE ******/ { Serial.begin(9600); // Set up for Serial Monitor to be able to see this work /*----( These lines allow code to work also on Arduino DUE )----- */ // Put these "//" in front of the line you do NOT want to use // following line sets the RTC to the date & time this sketch was compiled // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // May 21, 2015 at 6pm you would call: (use 24 hour time) #ifdef AVR Wire.begin(); #else Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due #endif rtc.begin(); // Start the RTC library code rtc.adjust(DateTime(2015, 5, 21, 18, 0, 0)); lcd.begin (16,2); // for 16 x 2 LCD module lcd.setBacklightPin(3,POSITIVE); lcd.setBacklight(HIGH); mySCoop.start(); } void loop() /****** LOOP: RUNS CONSTANTLY ******/ { switch(show_flag){ case 1:{ DateTime now = rtc.now(); // Read data from the RTC Chip //lcd.home (); // set cursor to 0,0 lcd.noBlink(); lcd.setCursor(1,0); lcd.print(show_s(now.year(),now.month(),now.day(),1)+' ' +daysOfTheWeek[now.dayOfTheWeek()]); lcd.setCursor (4,1); // go to start of 2nd line if(switch_flag == 1) lcd.print(String(show_s(now.hour(),now.minute(),now.second(),0)+" o")); else lcd.print(String(show_s(now.hour(),now.minute(),now.second(),0)+" x")); break; } case -1:{ break; } } yield(); //本函数必须放在主线的loop循环体中 }//--(end main loop )--- //显示日期和时间的字符拼合及补零函数 String show_s(int a,int b,int c,int d){ String s1,s2,s3; if(a6){ lcd.setCursor(x-1,1); lcd.print(" "); lcd.setCursor(x-1,1); return '*'; }else{ return '#'; } }else if( k >= '0' && k


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3