#define SD_CS_PIN 10 // Replace 10 with your actual CS pin number
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <Keypad.h>
#include <EEPROM.h>
#include <SPI.h>
#include <SdFat.h> // SdFat kütüphanesi eklendi
SdFat sd; // SdFat nesnesi oluşturuldu
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Stop switch pin
const int hareketver1 = 6;
const int hareketver2 = 7;
float DEGER_ORANTI = 20.03;
int homingPosizyon = 10;
float maxSpeed = 1000.0;
float acceleration = 600.0;
int tekrar = 0;
// Keypad setup
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A0, A1, A2, A3};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '=', 'D'}
};
AccelStepper stepper(AccelStepper::DRIVER, 8, 9);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
String inputString = "";
String KomutSatir = "";
int menuIndex = -1;
int stepPosition = 0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
DEGER_ORANTI = 20.00;
maxSpeed = 1200.0;
acceleration = 600.0;
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(acceleration);
// Set relay pins as outputs
pinMode(hareketver1, OUTPUT);
pinMode(hareketver2, OUTPUT);
// Ensure relays are off initially
digitalWrite(hareketver1, HIGH);
digitalWrite(hareketver2, HIGH);
if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SD init failed");
while (1); // Stop the program if SD card initialization fails
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SD Kart Basarili");
delay(2000);
lcd.clear();
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
inputString += key;
lcd.setCursor(0, 0);
lcd.print(inputString);
} else if (key == 'A') {
handleMenuOption();
} else if (key == 'B') {
KomutSatir = "";
} else if (key == 'C') {
inputString = "";
} else if (key == 'D') {
menuIndex = (menuIndex + 1) % 10; // Cycle through menu options
displayMenu();
} else if (key == '=') {
writeToSDCard(KomutSatir);
inputString = "";
KomutSatir="";
} else if (key == '*') {
StartStepmotor(tekrar);
}
}
stepper.run();
}
void displayMenu() {
lcd.clear();
lcd.setCursor(0, 0);
switch (menuIndex) {
case 0:
lcd.print(" M0:T tekrar");
break;
case 1:
lcd.print("M1: G Kordinat");
inputString = "G";
break;
case 2:
lcd.print("M2: X Kordinat");
inputString = "X";
break;
case 3:
lcd.print("M3: F Feedrate");
inputString = "F";
break;
case 4:
lcd.print("M4: P bekleme");
inputString = "P";
break;
case 5:
lcd.print("M5: . nokta ");
inputString = ".";
break;
case 6:
lcd.print("M6: - Eksi");
inputString = "-";
break;
case 7:
lcd.print("M7:direk rakam");
break;
case 8:
lcd.print("M8: R Role1 ");
inputString = "R";
break;
case 9:
lcd.print("M9: R2 Röle2 ");
inputString = "N";
break;
}
lcd.setCursor(0, 1);
lcd.print(KomutSatir);
lcd.setCursor(8, 1);
lcd.print(menuIndex);
lcd.print("/0-12");
}
void handleMenuOption() {
lcd.clear();
int numara = inputString.toInt();
switch (menuIndex) {
case 0:
tekrar = numara;
lcd.setCursor(0, 1);
lcd.print("tekrar =");
lcd.print(tekrar);
inputString = ""; // Clear input string after handling
break;
case 1:
KomutSatir += inputString;
break;
case 2:
KomutSatir += inputString;
break;
case 3:
KomutSatir += inputString;
break;
case 4:
KomutSatir += inputString;
break;
case 5:
KomutSatir += inputString;
break;
case 6:
KomutSatir += inputString;
break;
case 7:
KomutSatir += inputString;
break;
case 8:
KomutSatir += inputString;
break;
case 9:
KomutSatir += inputString;
break;
default:
break;
}
lcd.setCursor(0, 1);
lcd.print(KomutSatir);
inputString = ""; // Clear input string after handling
}
void writeToSDCard(String data) {
File myFile;
myFile = sd.open("data.txt", FILE_WRITE);
if (myFile) {
myFile.println(data);
myFile.close();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Yazma Basarili");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Yazma Hatali");
}
delay(500);
lcd.clear();
}
void StartStepmotor(int repeatCount) {
for (int i = 0; i < repeatCount; i++) {
File myFile;
myFile = sd.open("data.txt", FILE_READ);
if (myFile) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Kayit isleniyor:");
String data = "";
while (myFile.available()) {
char ch = myFile.read();
if (ch == '\n') {
processCommand(data);
data = "";
} else {
data += ch;
}
}
if (data.length() > 0) {
processCommand(data); // For the last line
}
myFile.close();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dosya okuma hatasi");
}
lcd.clear();
lcd.setCursor(8, 1);
lcd.print("Tekrar=");
lcd.print(repeatCount);
}
}
void processCommand(String command) {
int G_value = -1;
int X_value = -1;
int F_value = -1;
int R_value = -1;
int N_value = -1;
int P_value = -1;
int T_value = -1;
// Parse command string
int gIndex = command.indexOf('G');
int xIndex = command.indexOf('X');
int fIndex = command.indexOf('F');
int rIndex1 = command.indexOf("R");
int rIndex2 = command.indexOf("N");
int pIndex = command.indexOf('P');
int tIndex = command.indexOf('T');
if (gIndex != -1) {
int endIndex = command.indexOf(' ', gIndex);
if (endIndex == -1) endIndex = command.length();
G_value = command.substring(gIndex + 1, endIndex).toInt();
}
if (xIndex != -1) {
int endIndex = command.indexOf(' ', xIndex);
if (endIndex == -1) endIndex = command.length();
X_value = command.substring(xIndex + 1, endIndex).toInt();
}
if (fIndex != -1) {
int endIndex = command.indexOf(' ', fIndex);
if (endIndex == -1) endIndex = command.length();
F_value = command.substring(fIndex + 1, endIndex).toInt();
}
if (rIndex1 != -1) {
int endIndex = command.indexOf(' ', rIndex1);
if (endIndex == -1) endIndex = command.length();
R_value = command.substring(rIndex1 + 1, endIndex).toInt();
}
if (rIndex2 != -1) {
int endIndex = command.indexOf(' ', rIndex2);
if (endIndex == -1) endIndex = command.length();
N_value = command.substring(rIndex2 + 1, endIndex).toInt();
}
if (pIndex != -1) {
int endIndex = command.indexOf(' ', pIndex);
if (endIndex == -1) endIndex = command.length();
P_value = command.substring(pIndex + 1, endIndex).toInt();
}
if (tIndex != -1) {
int endIndex = command.indexOf(' ', tIndex);
if (endIndex == -1) endIndex = command.length();
T_value = command.substring(tIndex + 1, endIndex).toInt();
}
// Process parsed values
switch (G_value) {
case 0:
stepmotor(X_value, F_value);
break;
}
switch (R_value) {
case 0:
digitalWrite(hareketver1, HIGH); // Turn relay 2 on
break;
case 1:
digitalWrite(hareketver1, LOW); // Turn relay 1 off
break;
}
switch (N_value) {
case 0:
digitalWrite(hareketver2, HIGH); // Turn relay 2 on
break;
case 1:
digitalWrite(hareketver2, LOW); // Turn relay 2 off
break;
}
if (P_value != -1) {
/*
int beklemeyap = millis() / 1000;
inputString = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Durdur");
for (int i = 0; i < P_value; i++) {
}
*/
delay(1000);
lcd.clear();
}
}
void stepmotor(int X, int F) {
// Implement the step motor control logic here
if (X >= 0 && X <= 1200) { // Check if the position is within valid range
stepper.setMaxSpeed(F); // Set the feedrate as the maximum speed
stepper.moveTo(X); // Move to the specified position
while (stepper.distanceToGo() != 0) {
stepper.run();
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("X:");
lcd.print(X);
lcd.setCursor(0, 1);
lcd.print("F:");
lcd.print(F);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Olculer Hatali");
}
}