#include <Keypad.h>
// 4 satır ve 6 sütunlu keypad tanımlaması
//#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <AccelStepper.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define LIMIT_SWITCH_PIN 12
//#define homeBaslat 9
#define ENABLE_PIN 13 // Motor enable/disable pini
int homingPosizyon = 10;
int hafizaButton1 = 340;
int hafizaButton2 = 40;
int hafizaButton3 = 500;
int yonDegis = 0;
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=false;
unsigned long interval=60000;
// EEPROM addresses
const int addrMaxSpeed = 0;
const int addrAcceleration = 4;
const float addrDEGER_ORANTI = 8;
const int addrHomingPosizyon = 12;
const int addrsiperBaslangic = 16;
const int addrsiperBitis = 20;
const int addrhafizaButton0 = 24;
const int addrhafizaButton1 = 28;
const int addrhafizaButton2 = 32;
const int addrhafizaButton3 = 36;
const int addryonDegis = 40;
const int addmaxMinutes = 44;
int menuIndex = -1;
bool Hafizaenteratla = false;
float operandsStack[100]; // Stack to hold operands
int operandIndex = 0; // Index for operand stack
char operatorsStack[100]; // Stack to hold operators
int operatorIndex = 0; // Index for operator stack
//float result = 0;
// I2C LCD tanımlama
// Keypad tanımlama
const byte ROWS = 5;
const byte COLS = 6;
// Tuşların sırası (4x6 matris)
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' }
};
// Satır ve sütun pinleri (Arduino bağlantıları)
byte rowPins[ROWS] = {2,3,4,5,6};
byte colPins[COLS] = {A0,A1,A2,A3,8,7};
// Keypad nesnesi oluşturuluyor
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputString = "";
float result = 0.0;
String currentInput = ""; // To hold the current input expression
AccelStepper stepper(AccelStepper::DRIVER, 10, 11);
float DEGER_ORANTI = 20.00;
int siperBaslangic = 1;
int siperBitis = 1300;
float stepPosition = 0;
float maxSpeed = 1000.0;
float acceleration = 500.0;
byte turkce_karakterler[9][8] = {
{ 0x0,0x0,0xC,0x4,0x4,0x4,0xE,0x0 }, // ı
{ 0xE,0x0,0xE,0x11,0xF,0x1,0xE,0x0 }, // ğ
{0x0,0xA,0x0,0x11,0x11,0x11,0xE,0x0}, // ü
{ 0x0,0xF,0x10,0xE,0x1,0x1E,0x4,0x0 }, // ş
{ 0x0,0xA,0x0,0xE,0x11,0x11,0xE,0x0}, // ö
{ 0x0,0xE,0x11,0x10,0x11,0xE,0x4,0x0 }, // ç
{ 0x4,0x0,0xE,0x4,0x4,0x4,0xE,0x0}, // İ
{ 0xF,0x10,0x10,0xE,0x1,0x5,0x1E,0x0 } , // Ş (büyük harf)
{0xE,0x0,0xF,0x10,0x13,0x11,0xF,0x0 } //Ğ
};
void setup() {
// Serial.begin(9600);
lcd.init();
//lcd.begin();
lcd.backlight();
for (int i = 0; i < 8; i++) {
lcd.createChar(i, turkce_karakterler[i]);
}
// Karakterleri ekrana yazdır
lcd.setCursor(0, 0);
lcd.print("Turkce Karakterler:");
lcd.setCursor(0, 1);
lcd.write(byte(0)); // ı
lcd.write(byte(1)); // ğ
lcd.write(byte(2)); // ü
lcd.write(byte(3)); // ş
lcd.write(byte(4)); // ö
lcd.write(byte(5)); // ç
lcd.write(byte(6)); // İ
lcd.write(byte(7)); // Ş
lcd.write(byte(8)); // Ğ (bu karakter dinamik olarak yüklenmeli)
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(addrhafizaButton1, hafizaButton1);
EEPROM.get(addrhafizaButton2, hafizaButton2);
EEPROM.get(addrhafizaButton3, hafizaButton3);
EEPROM.get(addryonDegis, yonDegis);
EEPROM.get(addmaxMinutes, maxMinutes);
pinMode(ENABLE_PIN, OUTPUT); // Enable pini çıkış olarak ayarlanır
pinMode(LIMIT_SWITCH_PIN, INPUT);
// digitalWrite(StopSwitchPin, LOW); // Başlangıçta motor etkin
digitalWrite(LIMIT_SWITCH_PIN, HIGH);
digitalWrite(ENABLE_PIN, HIGH); // Başlangıçta motor etkin
//yüklerken silinecek
DEGER_ORANTI = 20.00;
homingPosizyon = 10;
maxSpeed = 1200.0;
acceleration = 600.0;
siperBaslangic = 1;
siperBitis = 1200;
hafizaButton1 = 340;
hafizaButton2 = 752;
hafizaButton3 = 800;
yonDegis=0;
maxMinutes=1;
//yüklerken silinecek
if (yonDegis == 0) {
stepper.setPinsInverted(false, false, false);
} else {
stepper.setPinsInverted(true, false, false);
}
// Declare key variable outside the loop
char key;
int ilkstepPosition = EEPROM.get(addrHomingPosizyon, homingPosizyon);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Oto ayar:");
lcd.print(ilkstepPosition);
lcd.setCursor(0, 0);
lcd.print("siper haf");
lcd.write(byte(0)); // ı
lcd.print("za");
//sifirlama1();
startMillis=millis();
do {
key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
currentInput += key;
lcd.setCursor(12, 0);
lcd.print(currentInput);
}
}
} while (key != 'C' && key != 'Z'); // Corrected loop condition
// Validate and set initial stepper position
if (key == 'C') {
minutes=0;
waitingForinput=false;
B_hafiza_al(); // Function for 'B' key
}
if (key == 'Z') {
digitalWrite(ENABLE_PIN, LOW);
sifirlama1(); // Function for 'A' key
}
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(acceleration);
// key = "";
}
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);
}
void loop() {
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;
}
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) % 15;
displayMenu();
} else if (press == 'D') {
handleMenu_Option();
} else if (press == 'J') {
displayMemoryData(hafizaButton1, "h1");
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
if (Hafizaenteratla) {
A_fonksiyon();
}
} else if (press == 'K') {
displayMemoryData(hafizaButton2, "h2");
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
if (Hafizaenteratla) {
A_fonksiyon();
}
} else if (press == 'L') {
displayMemoryData(hafizaButton3, "h3");
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
if (Hafizaenteratla) {
A_fonksiyon();
}
} else if (press == 'Z') {
sifirlama1(); // Function for 'A' key
}else if (press == 'V') {
Stopmotor();
}else if (press == 'W') {
digitalWrite(ENABLE_PIN,LOW);
startMillis=millis();
minutes=0;
waitingForinput=false;
stepPosition -= 0.10; // 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.10; // 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();
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();
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 == 'R') {
}
else if (press == '.') { // '*' tuşu nokta olarak kullanılmış
currentInput += '.'; // Noktayı ekle
updateLCD_Olcu(); // LCD ekranını güncelle
// }
} else { // Sayılar veya diğer karakterler
inputString += press;
updateLCD_Olcu(); // Ekranı güncelle
}
}
stepper.run(); // Motorun çalışmaya devam etmesini sağla
if (!stepper.isRunning()) {
checkMemoryInputs();
}
}
// Hafıza girişlerini kontrol eden fonksiyon
void checkMemoryInputs() {
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);
}
}
}
}
// 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 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);
}
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();
}
}
D2
D3
D4
D5
D6