#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <Keypad.h>
#include <WiFi.h>
#include <WebServer.h>
#include <EEPROM.h>
#define LIMIT_SWITCH_PIN 11
//#define homeBaslat 9
#define ENABLE_PIN 13 // Motor enable/disable pini
#define SENSOR_BACKOFF_STEPS 400
#define INITIAL_HOMING_STEPS -500
#define SAFE_POSITION_STEPS 4000
#define BACKOFF_STEPS 400
#define FINE_HOMING_START -500
#define SAFE_POSITION 4000 // Safe position steps
#define POSITION_RATIO 10
String inputString = "";
float result = 0.0;
String currentInput = ""; // To hold the current input expression
char press;
int menuIndex = -1;
float DEGER_ORANTI = 20.00;
int siperBaslangic = 1;
int siperBitis = 1300;
float stepPosition = 0;
float maxSpeed = 1000.0;
float acceleration = 500.0;
int homingPosizyon = 10;
int hafizaButton0 = 700;
int hafizaButton1 = 340;
int hafizaButton2 = 40;
int hafizaButton3 = 500;
int yonDegis = 0;
String rxLine; // PC'den gelen satır biriktirme tamponu
bool Hafizaenteratla = false;
int initial_homing = -1;
unsigned long startMillis=0;
unsigned int previousMillis = 0; // Zamanı takip etmek için değişken
int minutes=0;
int maxMinutes=0;
bool waitingForinput=true;
unsigned long interval=180000;
//===================eeprom=======================
const int addrMaxSpeed = 0;//1-50000
const int addrAcceleration = 4;//1-50000
const float addrDEGER_ORANTI = 8;//00.00
const int addrHomingPosizyon = 12;//10-1500
const int addrsiperBaslangic = 16;//0-50
const int addrsiperBitis = 20;//10-1500
const int addrhafizaButton0 = 24;//0000
const int addrhafizaButton1 = 28;//0000
const int addrhafizaButton2 = 32;//0000
const int addrhafizaButton3 = 36;//0000
const int addryonDegis = 40;//0-1
const int addmaxMinutes = 44;//000
// ===================== LCD =====================
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_ROWS 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_ROWS);
// ===================== WiFi =====================
// ==== WiFi AP Ayarları ====
const char* ssid = "ESP32_JSON";
const char* password = "12345678";
WebServer server(80);
// ===================== Stepper =====================
AccelStepper stepper(AccelStepper::DRIVER, 14, 5);
#define HOME_BTN 34 // ESP32'de uygun pin seç (ör: GPIO34)
// ===================== Keypad =====================
const byte ROWS = 5;
const byte COLS = 6;
byte rowPins[ROWS] = {23,19,18,17,16};
byte colPins[COLS] = {32,33,25,26,27,4};
char keys[ROWS][COLS] = {
{'J', '+','1', '2', '3', 'A' },
{'K', '-','4', '5', '6', 'B' },
{'L', '/','7', '8', '9', 'C' },
{'Z', '=','.', '0', 'M', 'D' },
{'V', 'W','Q', 'S', 'E', 'R' }
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// ===================== Değişkenler =====================
uint16_t charSet[8] = {0};
int charSetSize = sizeof(charSet) / sizeof(charSet[0]);
int currentCharIndex = 0;
int displayIndex = 0;
// ===================== Türkçe karakterler =====================
byte turkce_karakterler[8][8] = {
{ B00000, B00000, B00100, B00100, B00100, B00100, B00100, B00000 }, // ı
{ B10000, B10000, B10110, B11001, B10001, B10001, B10001, B00000 }, // ğ
{ B01010, B00000, B00000, B10001, B10001, B10001, B01110, B00000 }, // ü
{ B00000, B00000, B11111, B00001, B01111, B10001, B01111, B00000 }, // ş
{ B01010, B00000, B01110, B10001, B10001, B10001, B01110, B00000 }, // ö
{ B00000, B00000, B01110, B10000, B10000, B10000, B01110, B00000 }, // ç
{ B01110, B10001, B10000, B11100, B10000, B10001, B01110, B00000 }, // İ
{ B00000, B00000, B11111, B00001, B01111, B10001, B01111, B00000 } // Ş
};
// ===================== Setup =====================
void setup() {
Wire.begin(21, 22); // SDA=21, SCL=22
Serial.begin(115200);
// --- EEPROM Başlat ---
EEPROM.begin(64); // 48 byte gerekiyor, güvenli olsun diye 64 ayırdık
// --- LCD Başlat ---
lcd.init();
lcd.backlight();
for (int i = 0; i < 8; i++) {
lcd.createChar(i, turkce_karakterler[i]);
}
// --- Açılış Mesajı ---
lcd.clear();
lcd.print("Bismillah");
lcd.setCursor(0, 1);
lcd.print("siper haf");
lcd.write(byte(0)); // ı
lcd.print("za");
// --- İlk keypad kontrolü ---
char key;
do {
key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
currentInput += key;
lcd.setCursor(12, 1);
lcd.print(currentInput);
delay(200);
}
else if (key == 'C') {
minutes = 0;
waitingForinput = true;
B_hafiza_al();
break;
}
else if (key == 'V') {
digitalWrite(ENABLE_PIN, LOW);
minutes = 0;
waitingForinput = true;
sifirlama1();
break;
}
}
} while (waitingForinput);
// --- WiFi Başlat ---
WiFi.softAP(ssid, password);
lcd.print("AP baslatildi");
lcd.print(WiFi.softAPIP());
// --- Web Sunucu ---
server.begin();
// --- IP ekrana yaz ---
IPAddress myIP = WiFi.softAPIP();
lcd.print("AP baslatildi");
lcd.print(myIP);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Baslatildi");
lcd.setCursor(0, 1);
lcd.print(myIP.toString());
delay(3000);
lcd.clear();
// --- EEPROM'dan ayarları al ---
EEPROM.get(addrMaxSpeed, maxSpeed);
EEPROM.get(addrAcceleration, acceleration);
EEPROM.get(addrDEGER_ORANTI, DEGER_ORANTI);
EEPROM.get(addrHomingPosizyon, homingPosizyon);
EEPROM.get(addrsiperBaslangic, siperBaslangic);
EEPROM.get(addrsiperBitis, siperBitis);
EEPROM.get(addrhafizaButton0, hafizaButton0);
EEPROM.get(addrhafizaButton1, hafizaButton1);
EEPROM.get(addrhafizaButton2, hafizaButton2);
EEPROM.get(addrhafizaButton3, hafizaButton3);
EEPROM.get(addryonDegis, yonDegis);
EEPROM.get(addmaxMinutes, maxMinutes);
// --- Pin ayarları ---
pinMode(ENABLE_PIN, OUTPUT);
pinMode(LIMIT_SWITCH_PIN, INPUT);
digitalWrite(ENABLE_PIN, HIGH); // Başlangıçta motor devre dışı
// --- Varsayılan değerler (yüklerken silinecek) ---
DEGER_ORANTI = 20.00;
homingPosizyon = 10;
maxSpeed = 1200.0;
acceleration = 600.0;
siperBaslangic = 1;
siperBitis = 1200;
hafizaButton0 = 200;
hafizaButton1 = 340;
hafizaButton2 = 752;
hafizaButton3 = 800;
yonDegis = 0;
maxMinutes = 1;
// --- Stepper ayarı ---
if (yonDegis == 0) {
stepper.setPinsInverted(false, false, false);
} else {
stepper.setPinsInverted(true, false, false);
}
startMillis = millis();
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(acceleration);
// --- İlk mesaj tekrar ---
int ilkstepPosition; EEPROM.get(addrHomingPosizyon, ilkstepPosition);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bismillah");
lcd.setCursor(0, 1);
lcd.print("siper haf");
lcd.write(byte(0));
lcd.print("za");
// --- Ölçü ekranı ---
updateLCD_Olcu();
}
// ===================== Loop =====================
void loop() {
if (!stepper.isRunning()) { checkMemoryInputs(); }
// --- Stepper çalıştır ---
stepper.run();
}
void parseCommand(String cmd) {
int start = 0;
while (start < cmd.length()) {
int comma = cmd.indexOf(',', start);
if (comma == -1) comma = cmd.length();
String token = cmd.substring(start, comma);
handleToken(token);
start = comma + 1;
}
}
void handleToken(String token) {
token.trim();
if (token.startsWith("h1=")) {
hafizaButton0 = token.substring(3).toInt();
// Serial.println("H1 = " + String(hafizaButton1));
} else if (token.startsWith("h2=")) {
hafizaButton1 = token.substring(3).toInt();
// Serial.println("H2 = " + String(hafizaButton2));
} else if (token.startsWith("h3=")) {
hafizaButton2 = token.substring(3).toInt();
// Serial.println("H3 = " + String(hafizaButton3));
} else if (token.startsWith("h4=")) {
hafizaButton3 = token.substring(3).toInt();
// Serial.println("H4 = " + String(hafizaButton4));
} else if (token.startsWith("all=")) {
// Serial.println("Tüm değerler gönderildi:");
// Serial.println("H1 = " + String(hafizaButton1));
// Serial.println("H2 = " + String(hafizaButton2));
// Serial.println("H3 = " + String(hafizaButton3));
// Serial.println("H4 = " + String(hafizaButton4));
}
}
// Hafıza girişlerini kontrol eden fonksiyon
void checkMemoryInputs() {
char press=keypad.getKey();
if (press != NO_KEY) {
if (press >= '0' && press <= '9') {
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
currentInput += press;
updateLCD_Olcu();
}
if (press == '+' || press == '-' || press == '/' || press == '*') {
if (result > 0) { inputString = result; }
currentInput += press;
inputString += press; // Operatörü string'e ekle
updateLCD_Olcu(); // Ekranı güncelle
} else if (press == '=') {
if (inputString.length() > 0) {
result = calculateResult(inputString);
lcd.setCursor(0, 1);
lcd.print("Sonu");
lcd.write(byte(5)); // ç
lcd.print(": ");
lcd.setCursor(8, 1);
// lcd.print(result);
currentInput =String(result,2) ; // Sonucu yeni input olarak ayarla
inputString = result;
lcd.print(currentInput);
}
if (Hafizaenteratla) {
A_fonksiyon();
}
if (result > 0) {
lcd.setCursor(15, 0);
lcd.print("H");
}
} else if (press == 'A') {
if (currentInput == "") {
return; // Eğer giriş boşsa fonksiyonu çağırmadan çık
}
A_fonksiyon(); // Geçerli giriş varsa fonksiyonu çağır
} else if (press == 'B') {
clearInput();
updateLCD_Olcu();
} else if (press == 'C') {
B_hafiza_al();
} else if (press == 'M') {
lcd.clear();
menuIndex = (menuIndex + 1) % 16;
displayMenu();
} else if (press == 'D') {
handleMenu_Option();
} else if (press == 'J') {
displayMemoryData(hafizaButton0, "h1");
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
if (Hafizaenteratla) {
A_fonksiyon();
}
} else if (press == 'K') {
displayMemoryData(hafizaButton1, "h2");
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
if (Hafizaenteratla) {
A_fonksiyon();
}
} else if (press == 'L') {
displayMemoryData(hafizaButton2, "h3");
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
if (Hafizaenteratla) {
A_fonksiyon();
}
} else if (press == 'V') {
digitalWrite(ENABLE_PIN,LOW);
sifirlama1(); // Function for 'A' key
}else if (press == 'Z') {
displayMemoryData(hafizaButton3, "h4");
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
if (Hafizaenteratla) {
A_fonksiyon();
}
// parseCommand("h1=101,h2=303,h3=505");
// ESP.restart();
// Stopmotor();
}else if (press == 'W') {
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
stepPosition -= 0.50; // Update position
stepper.moveTo(stepPosition* DEGER_ORANTI);
while (stepper.distanceToGo() != 0) { // Ensure the motor completes movement
stepper.run();
}
updateLCD_Olcu();
}else if (press == 'Q') {
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
stepPosition += 0.50; // Update position
stepper.moveTo(stepPosition* DEGER_ORANTI);
while (stepper.distanceToGo() != 0) { // Ensure the motor completes movement
stepper.run();
}
updateLCD_Olcu();
}else if (press == 'S') {
int num = currentInput.toInt();
float hedefPozisyon = (stepPosition - num) ;
if (hedefPozisyon <= siperBaslangic) {
lcd.setCursor(0, 0);
lcd.print("HATALI HEDEF ");
delay(1000);
updateLCD_Olcu();
return;
}
stepPosition -= num; // Update position
stepper.moveTo(stepPosition* DEGER_ORANTI);
while (stepper.distanceToGo() != 0) { // Ensure the motor completes movement
stepper.run();
}
updateLCD_Olcu();
}else if (press == 'E') {
int num = currentInput.toInt();
float hedefPozisyon = (stepPosition + num) ;
if (hedefPozisyon >= siperBitis) {
lcd.setCursor(0, 0);
lcd.print("HATALI HEDEF ");
delay(1000);
updateLCD_Olcu();
return;
}
stepPosition += num;
stepper.moveTo(stepPosition * DEGER_ORANTI);
while (stepper.distanceToGo() != 0) {
stepper.run();
}
updateLCD_Olcu(); // Ekranı güncelle
}
else if (press == 'R') {
digitalWrite(ENABLE_PIN,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("G");
lcd.write(byte(2)); // ü
lcd.write(byte(5)); // ç
lcd.print(" kapal");
lcd.write(byte(0)); // ı
waitingForinput=true;
lcd.setCursor(0, 1);
lcd.print("Siper : ");
lcd.setCursor(8, 1); // Değeri yeniden yaz
lcd.print(stepPosition);
}
else if (press == '.') { // '*' tuşu nokta olarak kullanılmış
currentInput += '.'; // Noktayı ekle
updateLCD_Olcu(); // LCD ekranını güncelle
// }
}
delay(100);
}
if(!waitingForinput){
if(millis()-startMillis >=interval){
startMillis=millis();
minutes++;
// Serial.print("dakika");
// Serial.print(minutes);
if(minutes>=maxMinutes){
digitalWrite(ENABLE_PIN,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("G");
lcd.write(byte(2)); // ü
lcd.write(byte(5)); // ç
lcd.print(" kapal");
lcd.write(byte(0)); // ı
waitingForinput=true;
lcd.setCursor(0, 1);
lcd.print("Siper : ");
lcd.setCursor(8, 1); // Değeri yeniden yaz
lcd.print(stepPosition);
}
}
}
}
void lcdPrintLine(uint8_t line, const String &msg) {
lcd.setCursor(0, line);
String out = msg;
while (out.length() < 16) out += ' ';
lcd.print(out.substring(0, 16));
}
void updateLCD_Olcu() {
lcd.clear();
if (result > 0) {
lcd.setCursor(15, 0);
lcd.print("H");
}
lcd.setCursor(0, 0);
lcd.write(byte(4)); // ö
lcd.print("l");
lcd.write(byte(5)); // ç
lcd.write(byte(2)); // ü
lcd.print(" : ");
// Eski girdiyi temizle
lcd.setCursor(7, 0); // "Olcu :" sonrası
lcd.print(" "); // Önceki girdiyi sil
lcd.setCursor(7, 0); // Girdiyi yeniden yaz
lcd.print(currentInput);
// "Siper:" yazısını yazdır
lcd.setCursor(0, 1);
lcd.print("Siper : ");
// Eski siper değerini temizle
lcd.setCursor(8, 1); // "Siper:" sonrası
lcd.print(" "); // Önceki değeri sil
lcd.setCursor(8, 1); // Değeri yeniden yaz
lcd.print(stepPosition);
//hafıza
lcd.setCursor(0, 2);
lcd.print("H1:"); lcd.print(hafizaButton0);
lcd.setCursor(10, 2);
lcd.print("H2:"); lcd.print(hafizaButton1);
lcd.setCursor(0, 3);
lcd.print("H3:"); lcd.print(hafizaButton2);
lcd.setCursor(10, 3);
lcd.print("H4:"); lcd.print(hafizaButton3);
}
void clearInput() {
currentInput = "";
result = 0;
inputString = "";
lcd.setCursor(7, 0); // Start after "Olcu :"
lcd.print(" "); // Clear previous input
lcd.setCursor(0, 1);
lcd.print("Siper: "); // Clear the second line
menuIndex = -1;
updateLCD_Olcu();
}
void A_fonksiyon() {
int num = currentInput.toInt();
if (num >= siperBaslangic && num <= siperBitis) {
stepPosition = num;
stepper.moveTo(stepPosition * DEGER_ORANTI);
inputString = "";
currentInput = "";
updateLCD_Olcu();
} else {
lcd.setCursor(0, 0);
lcd.print("HATALI HEDEF ");
unsigned long startTime = millis();
while (millis() - startTime < 1000) {
stepper.run(); // Ensure the motor can still operate
}
updateLCD_Olcu();
}
}
// LCD'de hafıza verisini gösteren fonksiyon
void displayMemoryData(int buttonValue, const char* memoryLabel) {
lcd.clear();
currentInput = String(buttonValue); // Integer'ı string'e çevir
lcd.setCursor(0, 0);
lcd.write(byte(4)); // ö
lcd.print("l");
lcd.write(byte(5)); // ç
lcd.write(byte(2)); // ü
lcd.print(" : ");
lcd.print(currentInput);
lcd.print(" ");
lcd.print(memoryLabel);
}
void Stopmotor() {
stepper.stop();
currentInput = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motor Durduruldu");
lcd.setCursor(0, 1);
int currentPos = stepper.targetPosition();
lcd.print("Pozisyon:");
lcd.print(currentPos / DEGER_ORANTI);
}
D2
D3
D4
D5
D6