#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>
// #include <AltSoftSerial.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Pins
#define BT1 2
#define BT2 3
#define BT3 4
#define BTS7960_PWM 5
#define SUMOTOR 6
#define ISIALARM 7
#define ALTSOFTWARE_RX 8
#define ALTSOFTWARE_TX 9
#define BUZZER 10
#define FANALARM 11
#define SUALARM 12
#define NEMALARM 13
#define SUSICAKLIK A0
#define KAPILIMIT A1
#define BTS7960_IO_1 A2
#define BTS7960_IO_2 A3
// 0x3C genellikle SSD1306 OLED'ler için kullanılan I2C adresidir.
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
volatile bool button1Pressed = false;
volatile bool button2Pressed = false;
bool button3Pressed = false;
bool button3Pressed3sec = false;
bool mypikon_mode = false;
unsigned long BT3_pressTime_previous = 0;
unsigned long read_ntc_previous = 0;
const unsigned int interval1 = 1500; // 1000ms = 1 saniye
bool menu_bool = false;
bool pikon_bool = false;
bool anasayfa_bool = true;
byte menu_pages = 1;
byte menu_pages_last = 0;
bool pikon_run_bool = false;
bool SUISI_TEMP_bool = false;
unsigned long currentMillis;
int SUISI_TEMP;
byte SUISI_TEMP_Alarm;
// AltSoftSerial altSerial(8,9); // RX, TX
// ISR'ler (Interrupt Service Routine)
void handleButton1()
{
// Debounce interrupt trigger
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
unsigned int temp = 0;
// If interrupts come faster than 35ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 35)
{
temp++;
if ((temp % 2) == 0)
{
button1Pressed = false;
}
else
{
button1Pressed = true;
}
}
last_interrupt_time = interrupt_time;
}
void handleButton2()
{
// Debounce interrupt trigger
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
unsigned int temp = 0;
// If interrupts come faster than 35ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 35)
{
temp++;
if ((temp % 2) == 0)
{
button2Pressed = false;
}
else
{
button2Pressed = true;
}
}
last_interrupt_time = interrupt_time;
}
void Read_NTC10k()
{
if ((currentMillis - read_ntc_previous) >= 100)
{
read_ntc_previous = currentMillis;
float a = 639.5, b = -0.1332, c = -162.5;
float Rntc, Vntc;
Vntc = (analogRead(SUSICAKLIK) * 5.0) / 1023.0;
Rntc = 10000.0 * ((5.0 / Vntc) - 1);
SUISI_TEMP = a * pow(Rntc, b) + c;
}
}
byte degisim_oku(byte max, byte min, byte data)
{
if (button1Pressed && (data < max))
{
button1Pressed = false;
data = data + 1;
delay(250);
}
else if (button1Pressed && (data >= max))
{
button1Pressed = false;
data = min;
delay(250);
}
else if (button2Pressed && (data > min))
{
button2Pressed = false;
data = data - 1;
delay(250);
}
else if (button2Pressed && (data <= min))
{
button2Pressed = false;
data = max;
delay(250);
}
return data;
}
void SUISI()
{
if (SUISI_TEMP_bool)
{
display.clearDisplay();
display.setCursor(0, 0);
display.print("SUISI_ALARM == ");
display.print(SUISI_TEMP_Alarm);
Serial.println(SUISI_TEMP_Alarm);
delay(1000);
SUISI_TEMP_Alarm = degisim_oku(40, 0, SUISI_TEMP_Alarm);
if (button3Pressed3sec)
{
// save the data to eeprom
button3Pressed3sec = false;
SUISI_TEMP_bool = false;
anasayfa_bool = true;
}
}
}
void anasayfa()
{
if (anasayfa_bool)
{
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Beyaz yazı
display.setCursor(0, 0); // Ekranın sol üst köşesi
display.print("ISI:");
display.print(menu_pages);
display.println("C"); // ISI DEĞERİ
display.print("NEM:");
display.print(menu_pages);
display.println("%"); // ISI DEĞERİ
display.print("SUISI:");
display.print(SUISI_TEMP);
display.println("C"); // ISI DEĞERİ
display.display(); // Ekrana yazdır
}
}
void menu_alt1()
{
if (menu_pages != menu_pages_last)
{
if (menu_pages == 1)
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("(1)PIKON (-X-)");
display.println("(2)SUISI ");
display.println("(3)SUHIZ ");
display.println("(4)ZAMAN ");
display.println("(5)NEMAL ");
display.println("(6)ISIAL ");
display.println("(7)BAKIM ");
display.println(" VERSION :: 1.0.0");
display.display(); // Ekrana yazdır
}
else if (menu_pages == 2)
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("(1)PIKON ");
display.println("(2)SUISI (-X-)");
display.println("(3)SUHIZ ");
display.println("(4)ZAMAN ");
display.println("(5)NEMAL ");
display.println("(6)ISIAL ");
display.println("(7)BAKIM ");
display.println(" VERSION :: 1.0.0");
display.display(); // Ekrana yazdır
}
else if (menu_pages == 3)
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("(1)PIKON ");
display.println("(2)SUISI ");
display.println("(3)SUHIZ (-X-)");
display.println("(4)ZAMAN ");
display.println("(5)NEMAL ");
display.println("(6)ISIAL ");
display.println("(7)BAKIM ");
display.println(" VERSION :: 1.0.0");
display.display(); // Ekrana yazdır
}
else if (menu_pages == 4)
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("(1)PIKON ");
display.println("(2)SUISI ");
display.println("(3)SUHIZ ");
display.println("(4)ZAMAN (-X-)");
display.println("(5)NEMAL ");
display.println("(6)ISIAL ");
display.println("(7)BAKIM ");
display.println(" VERSION :: 1.0.0");
display.display(); // Ekrana yazdır
}
else if (menu_pages == 5)
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("(1)PIKON ");
display.println("(2)SUISI ");
display.println("(3)SUHIZ ");
display.println("(4)ZAMAN ");
display.println("(5)NEMAL (-X-)");
display.println("(6)ISIAL ");
display.println("(7)BAKIM ");
display.println(" VERSION :: 1.0.0");
display.display(); // Ekrana yazdır
}
else if (menu_pages == 6)
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("(1)PIKON ");
display.println("(2)SUISI ");
display.println("(3)SUHIZ ");
display.println("(4)ZAMAN ");
display.println("(5)NEMAL ");
display.println("(6)ISIAL (-X-)");
display.println("(7)BAKIM ");
display.println(" VERSION :: 1.0.0");
display.display(); // Ekrana yazdır
}
else if (menu_pages == 7)
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("(1)PIKON ");
display.println("(2)SUISI ");
display.println("(3)SUHIZ ");
display.println("(4)ZAMAN ");
display.println("(5)NEMAL ");
display.println("(6)ISIAL ");
display.println("(7)BAKIM (-X-)");
display.println(" VERSION :: 1.0.0");
display.display(); // Ekrana yazdır
}
}
}
void pikon_run()
{
if (pikon_run_bool)
{
if (button1Pressed)
{
button1Pressed = false;
digitalWrite(BTS7960_IO_1, HIGH);
digitalWrite(BTS7960_IO_2, LOW);
analogWrite(BTS7960_PWM, 255);
delay(500);
button1Pressed = false;
analogWrite(BTS7960_PWM, 0);
}
else if (button2Pressed)
{
button2Pressed = false;
digitalWrite(BTS7960_IO_1, LOW);
digitalWrite(BTS7960_IO_2, HIGH);
analogWrite(BTS7960_PWM, 255);
delay(500);
button2Pressed = false;
analogWrite(BTS7960_PWM, 0);
}
}
}
void pikon()
{
if (pikon_bool)
{
if (mypikon_mode == true)
{
mypikon_mode = true;
display.clearDisplay();
display.setCursor(0, 0);
display.print("PIKON == ");
display.print("ON");
}
else if (mypikon_mode == false)
{
mypikon_mode = false;
display.clearDisplay();
display.setCursor(0, 0);
display.print("PIKON == ");
display.print("OFF");
}
display.display(); // Ekrana yazdır
if (button3Pressed)
{
button3Pressed = false;
if (mypikon_mode == true)
{
mypikon_mode = false;
}
else
{
mypikon_mode = true;
}
}
if ((mypikon_mode == false) && button3Pressed3sec)
{
button3Pressed3sec = false;
display.clearDisplay();
pikon_bool = false;
pikon_run_bool = false;
anasayfa_bool = true;
}
else if ((mypikon_mode == true) && button3Pressed3sec)
{
button3Pressed3sec = false;
display.clearDisplay();
display.setCursor(0, 0);
pikon_run_bool = true;
display.display(); // Ekrana yazdır
}
}
}
void menu()
{
if (menu_bool == true)
{
menu_pages = degisim_oku(7, 1, menu_pages);
menu_alt1();
if (((menu_pages == 1) && button3Pressed))
{
button3Pressed = false;
menu_bool = false;
pikon_bool = true;
}
else if ((menu_pages == 2) && button3Pressed)
{
button3Pressed = false;
menu_bool = false;
SUISI_TEMP_bool = true;
}
else if ((menu_pages == 3) && button3Pressed)
{
button3Pressed = false;
/* code */
}
else if ((menu_pages == 4) && button3Pressed)
{
button3Pressed = false;
/* code */
}
else if ((menu_pages == 5) && button3Pressed)
{
button3Pressed = false;
/* code */
}
else if ((menu_pages == 6) && button3Pressed)
{
button3Pressed = false;
/* code */
}
else if ((menu_pages == 7) && button3Pressed)
{
button3Pressed = false;
/* code */
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(BT1, INPUT_PULLUP); // Button 1 - interrupt YUKARI
pinMode(BT2, INPUT_PULLUP); // Button 2 - interrupt AŞAĞI
pinMode(BT3, INPUT_PULLUP); // Button 3 - polling
pinMode(BTS7960_PWM, OUTPUT); // BTS7960 MOTOR PWM
pinMode(SUMOTOR, OUTPUT); // Dozaj Su MOTOR PWM BJT Mosfet Kontrol
pinMode(ISIALARM, OUTPUT); // Isıtıcı Alarm Rölesi
// pinMode(ALTSOFTWARE_RX, OUTPUT); // Altsoftware Serial 8
// pinMode(ALTSOFTWARE_TX, OUTPUT); // Altsoftware Serial 9
pinMode(BUZZER, OUTPUT); // PWM Buzzer Çıkışı
pinMode(FANALARM, OUTPUT); // Fan Alarm Rölesi
pinMode(SUALARM, OUTPUT); // Su Isıtıcı Rölesi
pinMode(NEMALARM, OUTPUT); // Nem Alarm Rölesi
pinMode(SUSICAKLIK, INPUT); // Nem Alarm Rölesi
pinMode(KAPILIMIT, INPUT); // Nem Alarm Rölesi
pinMode(BTS7960_IO_1, OUTPUT); // Nem Alarm Rölesi
pinMode(BTS7960_IO_2, OUTPUT); // Nem Alarm Rölesi
attachInterrupt(digitalPinToInterrupt(2), handleButton1, LOW);
attachInterrupt(digitalPinToInterrupt(3), handleButton2, LOW);
// OLED ekranı başlat
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.begin(9600);
Serial.println(F("OLED ERR!"));
while (true)
; // Dur
}
display.setTextSize(1); // Yazı boyutu
}
void loop()
{
currentMillis = millis();
if (digitalRead(4) == LOW)
{
if ((currentMillis - BT3_pressTime_previous) >= interval1)
{
BT3_pressTime_previous = currentMillis;
button3Pressed3sec = true;
button3Pressed = false;
delay(100);
}
else if (((currentMillis - BT3_pressTime_previous) >= (interval1 / 4)) && (button3Pressed3sec == false))
{
button3Pressed = true;
}
}
else
{
BT3_pressTime_previous = currentMillis;
////////////////BT3 END
if ((button3Pressed3sec && menu_bool == false) && (anasayfa_bool == true))
{
menu_bool = true;
button3Pressed3sec = false;
button3Pressed = false;
anasayfa_bool = false;
display.clearDisplay();
delay(100);
}
Read_NTC10k();
anasayfa();
menu();
pikon();
pikon_run();
SUISI();
}
}NTC 10K