#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <RTClib.h>
#define TFT_DC 9
#define TFT_CS 10
#define DHT_TYPE DHT22
#define BUZ_PIN 7
#define DS1_PIN 2
#define DS2_PIN 3
#define DS3_PIN 4
#define DS4_PIN 5
#define POT_PIN A0
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
const int TONE = 2000;
const int TONE_DURATION = 300;
bool needAlarm = false;
RTC_DS1307 rtc;
OneWire wire1(DS1_PIN);
DallasTemperature engineDs(&wire1);
OneWire wire2(DS2_PIN);
DallasTemperature transmissionDs(&wire2);
OneWire wire3(DS3_PIN);
DallasTemperature cabinDs(&wire3);
OneWire wire4(DS4_PIN);
DallasTemperature outsideDs(&wire4);
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
class TemperatureSensor;
double map(double x, double in_min, double in_max, double out_min, double out_max);
class TemperatureSensor
{
public:
String title;
double temperature;
public:
TemperatureSensor(String title)
{
this->title = title;
}
void requestTemperature(DallasTemperature &ds)
{
ds.requestTemperatures();
Serial.println(ds.getTempCByIndex(0));
temperature = ds.getTempCByIndex(0);
}
};
class OledDisplay {
public:
OledDisplay() {
oled.setTextSize(1);
oled.setTextColor(WHITE);
}
void showTime(DateTime now) {
oled.setCursor(0, 0);
if (now.day() < 10) {
Serial.print('0');
}
oled.print(now.day());
oled.print('.');
if (now.month() < 10)
oled.print('0');
oled.print(now.month());
oled.print('.');
oled.println(now.year());
if (now.hour() < 10)
oled.print('0');
oled.print(now.hour());
oled.print(':');
if (now.minute() < 10)
oled.print('0');
oled.println(now.minute());
}
void showTemp(TemperatureSensor &sensor) {
oled.print(sensor.title);
oled.print(':');
oled.println(sensor.temperature);
}
void showVoltage(double v) {
oled.print("Voltage: ");
oled.println(v);
// if (v < 10) {
// oled.println("Warning: low voltage");
// }
}
void display() {
oled.display();
}
void resetCursor() {
oled.setCursor(0, 0);
}
};
class EngineTemperatureSensor : public TemperatureSensor
{
private:
bool isProblem = false;
public:
EngineTemperatureSensor(String title)
: TemperatureSensor(title)
{
if (temperature > 100) {
isProblem = true;
needAlarm = true;
tone(BUZ_PIN, TONE, TONE_DURATION);
}
}
};
OledDisplay display;
void setup() {
Serial.begin(115200);
Serial.println("Start");
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(500);
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
engineDs.begin();
transmissionDs.begin();
cabinDs.begin();
outsideDs.begin();
rtc.begin();
pinMode(POT_PIN, INPUT);
}
EngineTemperatureSensor engine("Engine");
TemperatureSensor transmission("Transmission");
TemperatureSensor cabin("Cabin");
TemperatureSensor outside("Outside");
int state = 0;
int lastState = 0;
int time = 5000;
void loop() {
oled.clearDisplay();
if (millis() - lastState >= time) {
lastState = millis();
state++;
}
double potValue = analogRead(POT_PIN);
double voltage = map(potValue, 0, 1023., 0, 15);
if (voltage < 10) {
tone(BUZ_PIN, TONE, TONE_DURATION);
}
if (state % 2 == 0) {
DateTime now = rtc.now();
display.showTime(now);
display.showVoltage(voltage);
}
else {
display.resetCursor();
oled.clearDisplay();
engine.requestTemperature(engineDs);
display.showTemp(engine);
transmission.requestTemperature(transmissionDs);
display.showTemp(transmission);
cabin.requestTemperature(cabinDs);
display.showTemp(cabin);
outside.requestTemperature(outsideDs);
display.showTemp(outside);
}
if (engine.temperature > 120) {
tone(BUZ_PIN, TONE, TONE_DURATION);
}
if (transmission.temperature > 100) {
tone(BUZ_PIN, TONE, TONE_DURATION);
}
display.display();
delay(1000);
}
double map(double x, double in_min, double in_max, double out_min, double out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}