// ================= PIN DEFINITIONS =================
#define V_STEP 14
#define V_DIR 12
#define H_STEP 18
#define H_DIR 19
#define EN_PIN 27
// ================= SETTINGS =================
const int stepsPerRow = 800; // Vertical movement
const int stepsPerCol = 600; // Horizontal movement
const int ROWS = 6;
const int COLS = 3;
int currentRow = 0;
int currentCol = 0;
// ================= SETUP =================
void setup() {
Serial.begin(115200);
pinMode(V_STEP, OUTPUT);
pinMode(V_DIR, OUTPUT);
pinMode(H_STEP, OUTPUT);
pinMode(H_DIR, OUTPUT);
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // Enable A4988
Serial.println("Enter bin number (0–17):");
}
// ================= LOOP =================
void loop() {
if (Serial.available()) {
int bin = Serial.parseInt();
if (bin >= 0 && bin < ROWS * COLS) {
moveToBin(bin);
} else {
Serial.println("Invalid bin number");
}
}
}
// ================= FUNCTIONS =================
void stepMotor(int stepPin, int dirPin, int steps, bool dir) {
digitalWrite(dirPin, dir);
for (int i = 0; i < steps; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}
}
void moveToBin(int bin) {
int targetRow = bin / COLS;
int targetCol = bin % COLS;
Serial.print("Target Row: ");
Serial.print(targetRow);
Serial.print(" | Target Column: ");
Serial.println(targetCol);
int rowDiff = targetRow - currentRow;
int colDiff = targetCol - currentCol;
if (rowDiff != 0) {
stepMotor(V_STEP, V_DIR, abs(rowDiff) * stepsPerRow, rowDiff > 0);
currentRow = targetRow;
}
if (colDiff != 0) {
stepMotor(H_STEP, H_DIR, abs(colDiff) * stepsPerCol, colDiff > 0);
currentCol = targetCol;
}
Serial.print("Reached Bin ");
Serial.println(bin);
}