#include <IRremote.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <FastLED.h>
#include <dht.h>
// Pins der verschiedenen Komponenten.
#define PIN_IR 9
#define PIN_DHT22 8
#define PIN_LED 10
#define NUM_LEDS 20
IRrecv receiver(PIN_IR);
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 rtc;
dht DHT;
CRGB leds[NUM_LEDS];
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
typedef unsigned long time_t;
#endif
// Timestamps für die Verwendung der verschiedenen Intervalle.
time_t systime;
time_t timestamp_LCD;
time_t timestamp_DHT22_Serial;
time_t timestamp_DHT22_LCD;
time_t timestamp_LED;
// Intervalle für die verschiedenen Komponenten in Sekunden.
int intervall_LCD = 1;
int intervall_DHT22_Serial = 5;
int intervall_DHT22_LCD = 1;
int intervall_LED = 1;
int stat_total = 0;
float temp_min = 15;
float temp_max = 27;
int map_led;
// Grad Zeichen (°) für den LC-Display.
byte grad[8] = {
B00100,
B01010,
B00100,
B00000,
B00000,
B00000,
B00000,
B00000
};
void setup()
{
Serial.begin(57600);
lcd.init(); // initialize the lcd
lcd.backlight();
rtc.begin();
FastLED.addLeds<WS2812B, PIN_LED, GRB>(leds, NUM_LEDS);
receiver.enableIRIn(); // Start the receiver
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// 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
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
lcd.setCursor(0, 0);
lcd.print("Freez Wachter");
lcd.setCursor(0, 1);
lcd.print("--- Starting ---");
lcd.createChar(0, grad);
delay(1000);
lcd.clear();
}
void loop()
{
systime = millis();
// Checks received an IR signal
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
if (systime - timestamp_LCD >= (intervall_LCD * 1000)) {
timestamp_LCD = systime;
//print_time();
print_time_LCD();
}
if (systime - timestamp_DHT22_Serial >= (intervall_DHT22_Serial * 1000)) {
timestamp_DHT22_Serial = systime;
print_messung_DHT22();
}
if (systime - timestamp_DHT22_LCD >= (intervall_DHT22_LCD * 1000)) {
timestamp_DHT22_LCD = systime;
print_messung_DHT22_LCD();
}
if (systime - timestamp_LED >= (intervall_LED * 1000)) {
timestamp_LED = systime;
LED_Strip();
}
}
void LED_Strip() {
//fill_rainbow (leds, NUM_LEDS, 0, (NUM_LEDS));
fill_solid(leds, NUM_LEDS, CRGB::Red);
map_led = map(DHT.temperature, temp_min, temp_max, NUM_LEDS, 0);
for (int i = 0; i < (map_led - 1); i++) {
leds[i] = 0;
}
FastLED.show();
}
void print_messung_DHT22()
{
// READ DATA
int chk = DHT.read22(PIN_DHT22);
stat_total++;
// DISPLAY DATA
Serial.print(DHT.humidity, 2);
Serial.print("% Luftfeuchtigkeit und ");
Serial.print(DHT.temperature, 2);
Serial.print("°C.");
Serial.println();
if (stat_total % 20 == 0)
{
Serial.print("Insgesammt wurden ");
Serial.print(stat_total);
Serial.print(" Messungen erhoben.");
Serial.println();
}
}
void print_messung_DHT22_LCD()
{
int chk = DHT.read22(PIN_DHT22);
stat_total++;
lcd.setCursor(0, 1);
lcd.print("Temp ");
lcd.print(DHT.temperature, 1);
lcd.write(0);
lcd.print("C");
if (DHT.temperature <= -10) {
lcd.print("");
} else if (DHT.temperature < 0) {
lcd.print(" ");
} else if (DHT.temperature < 10) {
lcd.print(" ");
} else {
lcd.print(" ");
}
lcd.print("Hum ");
lcd.print(DHT.humidity, 0);
lcd.print("%");
}
void print_time()
{
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
void print_time_LCD()
{
DateTime now = rtc.now();
lcd.setCursor(0, 0);
if (now.day() < 10) {
lcd.print("0");
}
lcd.print(now.day());
lcd.print(".");
lcd.print(now.month());
lcd.print(".");
lcd.print(now.year());
lcd.print(" ");
if (now.hour() < 10) {
lcd.print("0");
}
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());
}
void lcdPrint(char* text)
{
lcd.setCursor(0, 2);
lcd.print("button pressed:");
lcd.setCursor(0, 3);
lcd.print(text);
lcd.print(" code: ");
lcd.print(receiver.decodedIRData.command);
}
void translateIR()
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 162:
lcdPrint("POWER");
break;
case 226:
lcdPrint("MENU");
break;
case 34:
lcdPrint("TEST");
break;
case 2:
lcdPrint("PLUS");
break;
case 194:
lcdPrint("BACK");
break;
case 224:
lcdPrint("PREV.");
break;
case 168:
lcdPrint("PLAY");
break;
case 144:
lcdPrint("NEXT");
break;
case 104:
lcdPrint("num: 0");
break;
case 152:
lcdPrint("MINUS");
break;
case 176:
lcdPrint("key: C");
break;
case 48:
lcdPrint("num: 1");
break;
case 24:
lcdPrint("num: 2");
break;
case 122:
lcdPrint("num: 3");
break;
case 16:
lcdPrint("num: 4");
break;
case 56:
lcdPrint("num: 5");
break;
case 90:
lcdPrint("num: 6");
break;
case 66:
lcdPrint("num: 7");
break;
case 74:
lcdPrint("num: 8");
break;
case 82:
lcdPrint("num: 9");
break;
default:
lcd.clear();
lcd.print(receiver.decodedIRData.command);
lcd.print(" other button");
}
}
FPS: 0
Power: 0.00W
Power: 0.00W