#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
//oled stats
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//pins
#define DHTPIN 2 // pin connected to DHT11 sensor
#define fanPin 6 //signal pin for fan power
#define fanSig 5 //fan signal from menu system
//changed to 22 for sim.. ~q
#define DHTTYPE DHT22
//variables
int fanPwr = 0;
int overTemp = 0;
float tempF;
float tempC;
float humi;
//screen/oled init
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
DHT dht(DHTPIN, DHTTYPE);
String displayString;
void setup() {
Serial.begin(9600);
pinMode(fanSig, INPUT);
pinMode(fanPin, OUTPUT);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
dht.begin(); // initialize DHT11 the temperature and humidity sensor
displayString.reserve(10); // to avoid fragmenting memory when using String
}
void loop() {
delay(350);
float humi = dht.readHumidity(); // read humidity
float tempC = dht.readTemperature(); // read temperature
float tempF = (tempC *= 9) / 5 + 32; //convert temp
//temp high/low
if (tempF > 77) {
overTemp = 1;
}
if (tempF < 77) {
overTemp = 0;
}
//fan signal on (read signal pin for fan)
fanPwr = digitalRead(fanSig);
//fan on (signal)
if (fanPwr == 1) { // works like normal
digitalWrite(fanPin, HIGH);
}
//fan on (temp) ***// not working it says over temp is 1 but it isnt activating the fan
if (overTemp == 1) {
digitalWrite(fanPin, HIGH);
}
//temp low AND fan signal off (fan off) ***// same here but it doesnt turn off
if (overTemp == 0 && fanPwr == 0) {
digitalWrite(fanPin, LOW);
Serial.println("lol");
}
Serial.println(tempF);
Serial.println(overTemp);
// check if any reads failed
if (isnan(humi) || isnan(tempC)) {
displayString = "Failed";
} else {
displayString = String(tempF, 1); // one decimal places
displayString += "F ";
displayString += String(humi, 1); // one decimal places
displayString += "%";
}
//Serial.println(displayString); // print the temperature in F to Serial Monitor
oledDisplayCenter(displayString); // display temperature and humidity on OLED
}
void oledDisplayCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// display on horizontal and vertical center
oled.clearDisplay(); // clear display
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
oled.println(text); // text to display
oled.display();
}