#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Keypad.h>
#define pinXStep 13
#define pinXDir 10
#define pinZStep 11
#define pinZDir 12
// LCD, Tuş Takımı ve Step Motorları Başlatma
LiquidCrystal_I2C lcd(0x27, 16, 2);
AccelStepper stepperX(AccelStepper::DRIVER, pinXStep, pinXDir);
AccelStepper stepperZ(AccelStepper::DRIVER, pinZStep, pinZDir);
MultiStepper steppers; // MultiStepper object to control multiple motors
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'.','0','#','D'}
};
byte rowPins[ROWS] = { 9, 8, 7, 6 };
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
float wireThickness = 0.75; // Tel Kalınlığı
float bobbinWidth = 5.0; // Bobin Genişliği
int layers = 3; // Katman Sayısı
float turns; // Hesaplanan Tur Sayısı
int totalTurns; // Toplam X ekseni dönüş sayısı
float maxSpeed = 1200.0;
float acceleration = 600.0;
int menuIndex = -1; // 0 = Bobin Genişliği, 1 = Tel Kalınlığı, 2 = Katman Sayısı
String inputString = ""; // Tuş takımından gelen girişi depolamak için
long currentZPosition = 0; // Z ekseninin mevcut pozisyonu
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
// lcd.print("Bobin Genisligi:");
stepperX.setMaxSpeed(maxSpeed);
stepperX.setAcceleration(acceleration);
stepperZ.setMaxSpeed(maxSpeed);
stepperZ.setAcceleration(acceleration);
// Add steppers to the MultiStepper object
steppers.addStepper(stepperX);
steppers.addStepper(stepperZ);
}
void loop() {
char key = keypad.getKey();
if (key) {
switch (key) {
case 'A': // Bir sonraki menü öğesine geç
menuIndex++;
if (menuIndex > 2) menuIndex = 0;
updateMenu();
break;
case 'B': // Giriş değerini kaydet
if (menuIndex == 0) {
bobbinWidth = inputString.toFloat();
} else if (menuIndex == 1) {
wireThickness = inputString.toFloat();
} else if (menuIndex == 2) {
layers = inputString.toInt();
}
inputString = "";
updateMenu();
break;
case 'C': // Mevcut girişi temizle
inputString = "";
lcd.clear();
updateMenu();
break;
case 'D': // Sarma işlemini başlat
turns = bobbinWidth / wireThickness;
totalTurns = turns * layers;
startWinding();
break;
default: // Sayısal girişleri işleme
inputString += key;
lcd.setCursor(0, 1);
lcd.print(inputString);
break;
}
}
}
void updateMenu() {
lcd.clear();
if (menuIndex == 0) {
lcd.print("Bobin Genisligi:");
} else if (menuIndex == 1) {
lcd.print("Tel Kalinligi:");
} else if (menuIndex == 2) {
lcd.print("Katman Sayisi:");
}
}
void startWinding() {
lcd.clear();
lcd.print("Sariliyor...");
float zStepPerXTurn = wireThickness; // Adjust based on your actual Z-axis movement per X-axis turn
for (int layer = 0; layer < layers; layer++) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tur:");
lcd.print(layer+1);
lcd.setCursor(0, 1);
lcd.print("Tur toplam:");
lcd.print(turns);
for (int i = 0; i < turns; i++) {
long positions[2]; // Array of desired stepper positions
// Update positions
positions[0] = stepperX.currentPosition() + 200; // X ekseni 1 tur (200 adım)
positions[1] = currentZPosition + (layer % 2 == 0 ? zStepPerXTurn * 200 : -zStepPerXTurn * 200); // Z ekseni yönü katman yönüne göre
steppers.moveTo(positions);
steppers.runSpeedToPosition(); // Blocks until all are in position
currentZPosition += (layer % 2 == 0 ? zStepPerXTurn * 200 : -zStepPerXTurn * 200); // Z ekseninin mevcut pozisyonunu güncelle
}
lcd.clear();
}
lcd.clear();
lcd.print("Sarma Tamamlandi");
while (1); // İşlemi durdur
}