Show all your code, not just a snippet.
#include <MemoryFree.h>
#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define DHT_PIN 3
#define DHT_TYPE DHT11
#define DISPLAY_POWER_PIN 5
#define MOTOR_RELAIS_PIN 6
#define ANA_POT_PIN 0
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define DISPLAY_TIMEOUT_MS 1000
#define POT_SAMPLE_RATE 200
// Size 32x32
const unsigned char temperature_icon_bitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0x04, 0x20, 0x00,
0x00, 0x04, 0x20, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x04, 0x20, 0x00,
0x00, 0x04, 0x20, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x04, 0x20, 0x00,
0x00, 0x04, 0x20, 0x00, 0x00, 0x05, 0xa0, 0x00, 0x00, 0x05, 0xa0, 0x00, 0x00, 0x05, 0xa0, 0x00,
0x00, 0x05, 0xa0, 0x00, 0x00, 0x05, 0xa0, 0x00, 0x00, 0x05, 0xa0, 0x00, 0x00, 0x0d, 0xb0, 0x00,
0x00, 0x19, 0x98, 0x00, 0x00, 0x33, 0xcc, 0x00, 0x00, 0x26, 0x64, 0x00, 0x00, 0x6c, 0x36, 0x00,
0x00, 0x68, 0x16, 0x00, 0x00, 0x68, 0x16, 0x00, 0x00, 0x6c, 0x36, 0x00, 0x00, 0x27, 0xe4, 0x00,
0x00, 0x33, 0xcc, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Size 28x28
const unsigned char drop_icon_bitmap [] PROGMEM = {
0x00, 0x06, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x19, 0x80, 0x00,
0x00, 0x39, 0xc0, 0x00, 0x00, 0x30, 0xc0, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0xe0, 0x70, 0x00,
0x00, 0xc0, 0x30, 0x00, 0x01, 0xc0, 0x38, 0x00, 0x01, 0x80, 0x18, 0x00, 0x03, 0x00, 0x0c, 0x00,
0x03, 0x00, 0x0c, 0x00, 0x06, 0x00, 0x06, 0x00, 0x06, 0x00, 0x06, 0x00, 0x0c, 0x00, 0x03, 0x00,
0x0c, 0x00, 0x03, 0x00, 0x0c, 0x00, 0x13, 0x00, 0x0c, 0x00, 0x1b, 0x00, 0x0c, 0x00, 0x1b, 0x00,
0x0c, 0x00, 0x33, 0x00, 0x06, 0x00, 0x76, 0x00, 0x06, 0x00, 0xe6, 0x00, 0x03, 0x00, 0xcc, 0x00,
0x03, 0x80, 0x1c, 0x00, 0x01, 0xe0, 0x78, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x00, 0x1f, 0x80, 0x00
};
// 'fan(1)', 32x32px
const unsigned char fan_icon_bitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x00,
0x00, 0x06, 0x18, 0x00, 0x00, 0x04, 0x06, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x08, 0x01, 0x00,
0x03, 0x88, 0x0f, 0x00, 0x06, 0x8c, 0x18, 0x00, 0x04, 0x84, 0x30, 0x00, 0x08, 0xc4, 0x20, 0x00,
0x08, 0x64, 0x43, 0x80, 0x10, 0x33, 0xde, 0x70, 0x10, 0x0e, 0x60, 0x10, 0x10, 0x04, 0x20, 0x08,
0x10, 0x04, 0x20, 0x08, 0x08, 0x06, 0x70, 0x08, 0x0e, 0x7b, 0xcc, 0x08, 0x01, 0xc2, 0x26, 0x10,
0x00, 0x04, 0x23, 0x10, 0x00, 0x0c, 0x21, 0x20, 0x00, 0x18, 0x31, 0x60, 0x00, 0xf0, 0x11, 0xc0,
0x00, 0x80, 0x10, 0x00, 0x00, 0xc0, 0x20, 0x00, 0x00, 0x60, 0x20, 0x00, 0x00, 0x18, 0x60, 0x00,
0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
DHT dht(DHT_PIN, DHT_TYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
unsigned long dht_last_read = 0;
unsigned long pot_last_read = 0;
int humidity = 100;
int temperature = 0;
int pot_val = 0;
int humidity_last_val = 0;
unsigned long humidity_last_change = 0;
int set_humidity = 50;
int new_set_humidity = 50;
bool motor_on = false;
bool display_pot = false;
void refreshDisplay(int temp, int hum) {
display.clearDisplay();
display.setTextSize(1);
display.drawBitmap(-9,0, temperature_icon_bitmap, 32, 32, WHITE);
display.drawBitmap(59,3, drop_icon_bitmap, 28, 28, WHITE);
display.setCursor(21,6);
display.setTextSize(3);
display.print(temp);
display.setCursor(91, 6);
display.print(hum);
display.display();
}
void showPotDisplay(int set_hum) {
display.clearDisplay();
display.drawBitmap(0,0, fan_icon_bitmap, 32, 32, WHITE);
display.setCursor(40,6);
display.setTextSize(3);
display.println(set_hum);
display.display();
}
void setup() {
Serial.begin(9600);
Serial.println(F("Started"));
delay(1000);
pinMode(MOTOR_RELAIS_PIN, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
delay(2000);
Serial.println(F("Display connected"));
// display.setFont(&FreeSerif9pt7b);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
Serial.println(freeMemory());
display.display();
Serial.println(F("Display refresh"));
dht.begin();
Serial.println(F("DHT connected"));
}
void loop() {
if (millis() > pot_last_read + POT_SAMPLE_RATE){
pot_val = analogRead(ANA_POT_PIN);
new_set_humidity = map(pot_val, 1023, 0, 0, 20);
set_humidity = new_set_humidity * 5;
pot_last_read = millis();
}
if (millis() > dht_last_read + 2000) {
float new_humidity = dht.readHumidity();
float new_temperature = dht.readTemperature();
if (!isnan(new_humidity)) {
humidity = new_humidity;
Serial.println(F("Read hum"));
}
if (!isnan(new_temperature)) {
temperature = new_temperature;
Serial.println(F("Read temp"));
}
dht_last_read = millis();
Serial.print(F("Pot Val: "));
Serial.println(pot_val);
Serial.print(F("Set hum: "));
Serial.println(set_humidity);
Serial.print(F("Hum: "));
Serial.println(humidity);
}
if (set_humidity != humidity_last_val) {
humidity_last_change = millis();
humidity_last_val = set_humidity;
}
if (millis() > humidity_last_change + DISPLAY_TIMEOUT_MS) {
display_pot = false;
}
else {
display_pot = true;
}
if (display_pot) {
showPotDisplay(set_humidity);
}
else {
refreshDisplay(temperature, humidity);
}
if (humidity > set_humidity) {
motor_on = true;
} else {
motor_on = false;
}
digitalWrite(MOTOR_RELAIS_PIN, motor_on);
}
Show us a schematic, even a hand drawn one.