Last active
May 18, 2020 12:50
-
-
Save iamdual/100d40eede868818babc6af0c252230a to your computer and use it in GitHub Desktop.
Date, clock and temperature example for Arduino Nano
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Required Libraries: | |
* https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library | |
* https://github.com/sparkfun/BMP180_Breakout | |
* https://github.com/msparks/arduino-ds1302 | |
*/ | |
#include <Wire.h> | |
#include <SFE_BMP180.h> | |
#include <DS1302.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <stdio.h> | |
namespace { | |
// Create a DS1302 object. | |
DS1302 rtc(8, 7, 6); // RST -> D8, DAT -> D7, CLK -> D6 | |
// Create a LiquidCrystal object. | |
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address, width, height | |
// Create a BMP180 object. | |
SFE_BMP180 pressure; | |
String dayAsString(const Time::Day day) { | |
switch (day) { | |
case Time::kSunday: return "Paz"; | |
case Time::kMonday: return "Pts"; | |
case Time::kTuesday: return "Sal"; | |
case Time::kWednesday: return "Car"; | |
case Time::kThursday: return "Per"; | |
case Time::kFriday: return "Cum"; | |
case Time::kSaturday: return "Cts"; | |
} | |
return "??"; | |
} | |
String monthAsString(const uint8_t month) { | |
switch (month) { | |
case 1: return "Oca"; | |
case 2: return "Sub"; | |
case 3: return "Mar"; | |
case 4: return "Nis"; | |
case 5: return "May"; | |
case 6: return "Haz"; | |
case 7: return "Tem"; | |
case 8: return "Agu"; | |
case 9: return "Eyl"; | |
case 10: return "Eki"; | |
case 11: return "Kas"; | |
case 12: return "Ara"; | |
} | |
return "???"; | |
} | |
String formatDateTime() { | |
// Get the current time and date from the chip. | |
Time t = rtc.time(); | |
const String day = dayAsString(t.day); | |
const String month = monthAsString(t.mon); | |
char buf[17]; | |
snprintf(buf, sizeof buf, "%02d %s %s %02d:%02d", t.date, month.c_str(), day.c_str(), t.hr, t.min); | |
return buf; | |
} | |
String formatTemp(char type) { | |
char status; | |
double T, lastT; | |
String temp; | |
status = pressure.startTemperature(); | |
if (status != 0) | |
{ | |
delay(status); | |
status = pressure.getTemperature(T); | |
if (status != 0) { | |
lastT = T; | |
} | |
} | |
if (type == 'F') { | |
temp = String((9.0/5.0)*T+32.0, 2); | |
} else { | |
temp = String(T, 2); | |
} | |
// https://arduino.stackexchange.com/questions/46828/how-to-show-the-%C2%BA-character-in-a-lcd | |
return temp + (char)223 + type; | |
} | |
void updateLCD() { | |
lcd.clear(); | |
lcd.backlight(); | |
lcd.print(formatDateTime()); | |
lcd.setCursor(0, 1); | |
lcd.print(formatTemp('C')); | |
lcd.setCursor(9, 1); | |
lcd.print(formatTemp('F')); | |
} | |
} // namespace | |
void setup() { | |
Serial.begin(9600); | |
lcd.begin(); | |
if (! pressure.begin()) { | |
lcd.print("BMP180 init fail"); | |
while(1); | |
} | |
// Setup for once the time for DS1302 module, then comment the code. Time is taken from http://time.is | |
//rtc.writeProtect(false); | |
//rtc.halt(false); | |
//Time t(2020, 5, 17, 14, 39, 20, Time::kSunday); //Sunday, May 17, 2020 at 14:39:20 | |
//rtc.time(t); | |
} | |
void loop() { | |
updateLCD(); | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment