/*
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
#define ONE_WIRE_BUS 6
#include <LiquidCrystal.h>
#include <RTClib.h>
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <GyverHTU21D.h>
GyverHTU21D htu;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
RTC_DS1307 rtc;
float tempSum = 0, temp;
byte tempCounter;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
uint32_t myTimer1, myTimer2, myTimer3;
boolean LEDflag = false;
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
sensors.begin();
htu.begin();
if (! rtc.begin())
{
lcd.print("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning())
{
lcd.print("RTC is NOT running!");
}
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));// to set the time manually
lcd.begin(16, 2); // set up the LCD's number of columns and rows
// термометр
sensors.setWaitForConversion(false); // асинхронное получение данных
sensors.requestTemperatures(); // запрос температуры
float tempC = sensors.getTempCByIndex(0); // получаем
}
void loop() {
if (htu.readTick()) {
}
// 2 раза в секунду
if (millis() - myTimer1 >= 100) {
myTimer1 = millis(); // сбросить таймер
toggleLED();
}
if (millis() - myTimer2 >= 200) {
myTimer2 = millis(); // сбросить таймер
getTemp();
}
if (millis() - myTimer3 >= 1000) {
myTimer3 = millis(); // сбросить таймер
redrawDisplay();
}
}
// print the number of seconds since reset:
// lcd.print(millis() / 1000);
void redrawDisplay () {
DateTime now = rtc.now();
lcd.setCursor(1, 0);
lcd.print("BPEMR");
lcd.print(" ");
lcd.print(now.hour());
lcd.print(':');
if (now.minute() < 10) lcd.print(0);
lcd.print(now.minute());
lcd.print(':');
if (now.second() < 10) lcd.print(0);
lcd.print(now.second());
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("T ");
lcd.print(htu.getTemperature());
lcd.setCursor(8, 1);
lcd.print("H ");
lcd.print(htu.getHumidity());
lcd.print("%");
// lcd.print("Moe no4teHue");
// delay(200);
// lcd.print("DATE");
// lcd.print(" ");
// lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
// lcd.print(" ");
// lcd.print(now.day());
// lcd.print('/');
// lcd.print(now.month());
// lcd.print('/');
// lcd.print(now.year());
// lcd.print(" ");
}
void toggleLED() {
digitalWrite(13, LEDflag); // вкл/выкл
LEDflag = !LEDflag; // инвертировать флаг
}
void getTemp() {
// суммируем температуру в общую переменную
tempSum += sensors.getTempCByIndex(0);
sensors.requestTemperatures();
// счётчик измерений
tempCounter++;
if (tempCounter >= 5) { // если больше 5
tempCounter = 0; // обнулить
temp = tempSum / 5; // среднее арифметическое
tempSum = 0; // обнулить
}
}