#include <LiquidCrystal_I2C.h> | |
#include <FlexiTimer2.h> // Contains FlexiTimer2 Library | |
// initialize the library with the numbers of the interface pins | |
LiquidCrystal_I2C lcd(0x27, 16, 2); | |
int tempPin = 0; // define the pin of temperature sensor | |
float tempVal; // define a variable to store temperature value | |
int hour, minute, second; // define variables stored record time | |
void setup() { | |
lcd.init(); // initialize the lcd | |
lcd.backlight(); // Turn on backlight | |
startingAnimation(); // display a dynamic start screen | |
FlexiTimer2::set(1000, timerInt); // configure the timer and interrupt function | |
FlexiTimer2::start(); // start timer | |
Serial.begin(115200); // initialize serial port with baud rate 9600 | |
Serial.println("Input hour,minute,second to set time."); | |
} | |
void loop() { | |
// Get temperature | |
tempVal = getTemp(); | |
if (second >= 60) { // when seconds is equal to 60, minutes plus 1 | |
second = 0; | |
minute++; | |
if (minute >= 60) { // when minutes is equal to 60, hours plus 1 | |
minute = 0; | |
hour++; | |
if (hour >= 24) { // when hours is equal to 24, hours turn to zero | |
hour = 0; | |
} | |
} | |
} | |
lcdDisplay(); // display temperature and time information on LCD | |
delay(200); | |
} | |
void startingAnimation() { | |
for (int i = 0; i < 16; i++) { | |
lcd.scrollDisplayRight(); | |
} | |
lcd.print("starting..."); | |
for (int i = 0; i < 16; i++) { | |
lcd.scrollDisplayLeft(); | |
delay(300); | |
} | |
lcd.clear(); | |
} | |
// the timer interrupt function of FlexiTimer2 is executed every 1s | |
void timerInt() { | |
second++; // second plus 1 | |
} | |
// serial port interrupt function | |
void serialEvent() { | |
int inInt[3]; // define an array to save the received serial data | |
while (Serial.available()) { | |
for (int i = 0; i < 3; i++) { | |
inInt = Serial.parseInt(); // receive 3 integer data |
[TR]
[TD][/TD]
[TD] }[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] // print the received data for confirmation[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print("Your input is: ");[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(inInt[0]);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(", ");[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(inInt[1]);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(", ");[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.println(inInt[2]);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] // use received data to adjust time[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] hour = inInt[0];[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] minute = inInt[1];[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] second = inInt[2];[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] // print the modified time[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print("Time now is: ");[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(hour / 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(hour % 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(':');[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(minute / 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(minute % 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(':');[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.print(second / 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] Serial.println(second % 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] }[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]}[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]// function used by LCD1602 to display time and temperature[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]void lcdDisplay() {[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.setCursor(0, 0); // set the cursor to (0,0) (first column,first row).[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print("TEMP: "); // display temperature information[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(tempVal);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print("C");[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.setCursor(0, 1); // set the cursor to (0,1) (first column,second row)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print("TIME: "); // display time information[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(hour / 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(hour % 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(':');[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(minute / 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(minute % 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(':');[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(second / 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] lcd.print(second % 10);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]}[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]// function used to get temperature[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]float getTemp() {[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] // Convert analog value of tempPin into digital value[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] int adcVal = analogRead(tempPin);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] // Calculate voltage[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] float v = adcVal * 5.0 / 1024;[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] // Calculate resistance value of thermistor[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] float Rt = 10 * v / (5 - v);[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] // Calculate temperature (Kelvin)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] float tempK = 1 / (log(Rt / 10) / 3950 + 1 / (273.15 + 25));[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] // Calculate temperature (Celsius)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD] return tempK - 273.15;[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]}[/TD]
[/TR]