#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <Keypad.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int homeSwitchPin = 12; // Pin for the homing limit switch
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {9, 8, 7, 6};
byte colPins[KEYPAD_COLS] = {5, 4, 3, 2};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '=', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
AccelStepper stepper(AccelStepper::DRIVER, 10, 11); // Define stepper motor driver pins for STEP and DIR
// Variables to hold values
float DEGER_ORANTI = 20.03;
int homingPosizyon = 10;
float maxSpeed = 1000.0;
float acceleration = 500.0;
int menuIndex = 0; // Variable to keep track of the menu state
// EEPROM addresses
const int addrMaxSpeed = 0;
const int addrAcceleration = 10;
const int addrDEGER_ORANTI = 20;
const int addrHomingPosizyon = 30;
int stepPosition = 0; // Target position for the stepper motor
String inputString = ""; // String to hold the input from the keypad
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 } // Ş (büyük harf)
};
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
EEPROM.get(addrMaxSpeed, maxSpeed);
EEPROM.get(addrAcceleration, acceleration);
EEPROM.get(addrDEGER_ORANTI, DEGER_ORANTI);
EEPROM.get(addrHomingPosizyon, homingPosizyon);
pinMode(homeSwitchPin, INPUT_PULLUP); // Configure the limit switch pin
pinMode(A0, INPUT_PULLUP);
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(acceleration);
// Özel karakterleri oluştur
for (int i = 0; i < 8; i++) {
lcd.createChar(i, turkce_karakterler[i]);
}
Reklam();
lcd.setCursor(0, 1);
lcd.write(0); // ı
lcd.write(1); // ğ
lcd.write(2); // ü
lcd.write(3); // ş
lcd.write(4); // ö
lcd.write(5); // ç
lcd.write(6); // İ
lcd.write(7); // Ş
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(4); // ö
lcd.print("l");
lcd.write(5); // ç
lcd.write(2); // ü
lcd.print(":");
lcd.setCursor(0, 1);
lcd.print("Siper:");
lcd.print(stepPosition);
}
void Reklam() {
// Kayan yazı için metin
String text = "Bismillahirrahmanirrahim";
// Metnin uzunluğu
int textLength = text.length();
// LCD'nin genişliği
int lcdWidth = 16;
// Metni LCD'de kaydır
for (int i = 0; i < textLength + lcdWidth; i++) {
// LCD'yi temizle
lcd.clear();
// Başlangıç ve bitiş noktalarını hesapla
int start = max(0, i - lcdWidth + 1);
int end = min(textLength, i + 1);
// LCD'ye yazdırılacak kısmı al
String displayText = text.substring(start, end);
// Metni LCD'nin ortasında hizala
lcd.setCursor(lcdWidth - (i - start + 1), 0);
lcd.print(displayText);
lcd.setCursor(0, 1);
lcd.print("YK Dekor");
// Bir süre bekle
delay(100);
}
}
void loop() {
// Home reset using A0 pin
if (digitalRead(A0) == LOW) {
stepper.stop();
inputString = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Durdur");
lcd.setCursor(0, 1);
lcd.print("Siper:");
}
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
lcd.clear();
inputString += key;
lcd.setCursor(0, 0);
lcd.write(4); // ö
lcd.print("l");
lcd.write(5); // ç
lcd.write(2); // ü
lcd.print(":");
lcd.print(inputString);
} else if (key == '#') {
menuIndex = (menuIndex + 1) % 4; // Cycle through menu options
displayMenu();
inputString = ""; // Clear input string when switching menu
} else if (key == 'D') {
handleMenuOption();
} else if (key == '*') {
if (inputString.indexOf('.') == -1) { // Allow only one decimal point
inputString += '.';
lcd.setCursor(0, 1);
lcd.print("Input: ");
lcd.print(inputString);
}
} else if (key == 'A') {
int num = inputString.toInt();
if (num >= 0 && num <= 1260) {
stepPosition = num;
stepper.moveTo(stepPosition);
inputString = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(4); // ö
lcd.print("l");
lcd.write(5); // ç
lcd.write(2); // ü
lcd.print(":");
lcd.setCursor(0, 1);
lcd.print("Siper:");
lcd.print(stepPosition);
} else {
inputString = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(4); // ö
lcd.print("l");
lcd.write(5); // ç
lcd.write(2); // ü
lcd.print(":");
}
} else if (key == 'B') {
int num = inputString.toInt();
if (num >= 0 && num <= 1260) {
stepPosition = num;
stepper.setCurrentPosition(stepPosition);
inputString = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(4); // ö
lcd.print("l");
lcd.write(5); // ç
lcd.write(2); // ü
lcd.print(":");
lcd.setCursor(0, 1);
lcd.print("Siper:");
lcd.print(stepPosition);
} else {
inputString = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(4); // ö
lcd.print("l");
lcd.write(5); // ç
lcd.write(2); // ü
lcd.print(":");
lcd.setCursor(0, 1);
lcd.print("Siper:");
lcd.print(stepPosition);
}
} else if (key == 'C') {
inputString = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(4); // ö
lcd.print("l");
lcd.write(5); // ç
lcd.write(2); // ü
lcd.print(":");
lcd.setCursor(0, 1);
lcd.print("Siper:");
lcd.print(stepPosition);
} else if (key == '=') {
menuIndex = (menuIndex + 1) % 4; // Cycle through menu options
displayMenu();
inputString = ""; // Clear input string when switching menu
}
}
stepper.run();
}
void displayMenu() {
lcd.clear();
lcd.setCursor(0, 0);
switch (menuIndex) {
case 0:
lcd.print("1: Max Speed");
lcd.setCursor(0, 1);
lcd.print(maxSpeed);
break;
case 1:
lcd.print("2: Acceleration");
lcd.setCursor(0, 1);
lcd.print(acceleration);
break;
case 2:
lcd.print("3: DEGER_ORANTI");
lcd.setCursor(0, 1);
lcd.print(DEGER_ORANTI);
break;
case 3:
lcd.print("4: Homing Pos");
lcd.setCursor(0, 1);
lcd.print(homingPosizyon);
break;
}
}
void handleMenuOption() {
lcd.clear();
switch (menuIndex) {
case 0: // Update max speed
maxSpeed = inputString.toFloat();
stepper.setMaxSpeed(maxSpeed);
EEPROM.put(addrMaxSpeed, maxSpeed);
lcd.print("Max Speed: ");
lcd.setCursor(0, 1);
lcd.print(maxSpeed);
break;
case 1: // Update acceleration
acceleration = inputString.toFloat();
stepper.setAcceleration(acceleration);
EEPROM.put(addrAcceleration, acceleration);
lcd.print("Accel: ");
lcd.setCursor(0, 1);
lcd.print(acceleration);
break;
case 2: // Update DEGER_ORANTI
DEGER_ORANTI = inputString.toFloat();
EEPROM.put(addrDEGER_ORANTI, DEGER_ORANTI);
lcd.print("DEGER_ORANTI: ");
lcd.setCursor(0, 1);
lcd.print(DEGER_ORANTI);
break;
case 3: // Update homing position
homingPosizyon = inputString.toInt();
EEPROM.put(addrHomingPosizyon, homingPosizyon);
lcd.print("Homing Pos: ");
lcd.setCursor(0, 1);
lcd.print(homingPosizyon);
break;
}
inputString = ""; // Clear input string after handling
}
void homeStepper() {
stepper.setMaxSpeed(500.0); // Reduce speed for homing
stepper.setAcceleration(100.0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S");
lcd.write(0); // ı
lcd.print("f");
lcd.write(0); // ı
lcd.print("rlama");
// Move towards the limit switch
stepper.moveTo(-10000); // Move a large number of steps in the negative direction
while (digitalRead(homeSwitchPin) == HIGH) {
stepper.run();
}
stepper.stop(); // Stop the motor
stepper.setCurrentPosition(0); // Set current position as 0
stepper.setMaxSpeed(1200.0); // Restore speed
stepper.setAcceleration(600.0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S");
lcd.write(0); // ı
lcd.print("f");
lcd.write(0); // ı
lcd.print("rlama Tamam");
delay(1000); // Wait 1 second to display the message
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(4); // ö
lcd.print("l");
lcd.write(5); // ç
lcd.write(2); // ü
lcd.print(":");
lcd.setCursor(0, 1);
lcd.print("Siper:");
lcd.print(stepPosition);
}