小闹钟项目
在额济纳支教这一年,给孩子们上了很多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
下载地址:
源程序
/*-----( 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 =