//Libraries
#include <DHT.h>;
//Constants
#define DHTPIN 14 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Variables
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
#include "OneButton.h"
#define pinLED1 16
#define pinButton1 12
#define pinLED2 0
#define pinButton2 13
// Setup a new OneButton on pin A1.
OneButton button1(pinButton1);
OneButton button2(pinButton2);
void IRAM_ATTR checkTicks() {
// include all buttons here to be checked
button1.tick();
button2.tick();
}
int buzzer = 15 ;
/* OLED */
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold18pt7b.h>
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
//wifi icon size: 20W*15H px
const unsigned char wifi_icon [] PROGMEM = {
0x07, 0xc0, 0x1f, 0xf0, 0x7c, 0x7c, 0x70, 0x1c,
0xc0, 0x06, 0xc3, 0x86, 0x0f, 0xe0, 0x1c, 0x70,
0x18, 0x30, 0x00, 0x00, 0x03, 0x80, 0x03, 0x80
};
const unsigned char temp_icon[] =
{
0b00000001, 0b11000000, // ###
0b00000011, 0b11100000, // #####
0b00000111, 0b00100000, // ### #
0b00000111, 0b11100000, // ######
0b00000111, 0b00100000, // ### #
0b00000111, 0b11100000, // ######
0b00000111, 0b00100000, // ### #
0b00000111, 0b11100000, // ######
0b00000111, 0b00100000, // ### #
0b00001111, 0b11110000, // ########
0b00011111, 0b11111000, // ##########
0b00011111, 0b11111000, // ##########
0b00011111, 0b11111000, // ##########
0b00011111, 0b11111000, // ##########
0b00001111, 0b11110000, // ########
0b00000111, 0b11100000, // ######
};
/* Potensio */
const int potPin = 34;
int potValue = 0;
// setup code here, to run once:
void setup() {
Serial.begin(115200);
// enable the standard led on pin 13.
pinMode(pinLED1, OUTPUT); // sets the digital pin as output
pinMode(pinLED2, OUTPUT); // sets the digital pin as output
//digitalWrite(pinLED1, LOW);
//digitalWrite(pinLED2, LOW);
attachInterrupt(digitalPinToInterrupt(pinButton1), checkTicks, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinButton2), checkTicks, CHANGE);
// link the doubleclick function to be called on a doubleclick event.
button1.attachClick(click1);
button1.attachDoubleClick(doubleclick);
button2.attachDoubleClick(doubleclick2);
button2.attachClick(doubleclick2);
pinMode(buzzer, OUTPUT); // inisiasi pin untuk buzzer
digitalWrite(buzzer, LOW);
dht.begin();
/*OLED SETUP */
Wire.begin(OLED_SDA, OLED_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print("WIFI");
display.setCursor(0,12);
display.print("VOL ");
// display.drawRect(38, 12, display.width()-40, 5, WHITE);
display.drawRect(38, 12, 87, 5, WHITE);
display.setCursor(0,23);
display.print("Green");
display.drawCircle(42,26, 4, WHITE);
display.setCursor(62,23);
display.print("RED");
display.drawCircle(90,26, 4, WHITE);
display.setCursor(0,35);
display.print("Temp :");
display.setCursor(0,47);
display.print("Hum :");
display.drawBitmap(113, 30, temp_icon, 16, 16, 1);
display.display();
Serial.println("SYSTEM TESTING ");
} // setup
// main code here, to run repeatedly:
int xcount = 0 ;
int xNilai_baca = 0 ;
void loop() {
// keep watching the push button:
button1.tick();
button2.tick();
cek_potensio();
xcount = xcount + 1 ;
if (xcount == 1000){
check_temp();
xcount = 0 ;
}
delay(10);
} // loop
void cek_potensio(){
potValue = analogRead(potPin);
if (potValue != xNilai_baca) {
int percentage1 = map(potValue, 0, 4095, 0, 85);
int percentage2 = map(xNilai_baca, 0, 4095, 0, 85);
display.fillRect(39, 13, percentage2 , 3, INVERSE);
display.fillRect(39, 13, percentage1 , 3, WHITE);
// display.fillRect(percentage, 13, 88 , 3, INVERSE);
xNilai_baca = potValue;
display.display();
Serial.print(" Potensio :");
Serial.print(potValue);
Serial.print(" : ");
Serial.println(percentage1);
}
};
void click1() {
static int m = LOW;
// reverse the LED
m = !m;
digitalWrite(pinLED1, m);
if (m == 1 ) {
display.fillCircle(42,26, 3, WHITE);
} else {
display.fillCircle(42,26, 3, INVERSE);
}
display.display();
Serial.println(m);
Serial.println("Green Click 1 ");
} // doubleclick
void doubleclick() {
static int m = LOW;
// reverse the LED
if (m == 1 ) {
display.fillCircle(42,26, 3, WHITE);
} else {
display.fillCircle(42,26, 3, INVERSE);
}
display.display();
Serial.println(m);
Serial.println("Green Click 2 ");
} // doubleclick
void doubleclick2() {
static int m = LOW;
// reverse the LED
m = !m;
digitalWrite(pinLED2, m);
if (m == 1 ) {
display.fillCircle(90,26, 3, WHITE);
} else {
display.fillCircle(90,26, 3, INVERSE);
}
display.display();
Serial.println("RED Click ");
BuzzerPendek() ;
} // doubleclick
void BuzzerPendek() {
Serial.println("Buzz ON ");
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(100);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(50);
Serial.println("Buzz OFF ");
}
void check_temp() {
hum = dht.readHumidity();
temp= dht.readTemperature();
display.setCursor(38,35);
display.print(temp);
display.setCursor(38,47);
display.print(hum);
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
// You can implement other code in here or just wait a while
}