#define x_paso 2 // Set the STEP Pin for X Axis Motor #define x_dire 5 // Set the DIR Pin for X Axis Motor #define x_habi 8 // Set the ENABLE Pin for X Axis Motor #define x_limite 9 // Set the Movement Limit Pin int retardo = 1000; // Delay between steps in microseconds int pasos_por_segundo = 500; // Number of steps per second (motor speed) unsigned long tiempo_avance_1 = 6000; // Initial advance time in milliseconds unsigned long tiempo_parada_1 = 5000; // Initial wait time in milliseconds unsigned long tiempo_avance_2 = 12000; // Second advance time in milliseconds unsigned long tiempo_parada_2 = 5000; // Second wait time in milliseconds unsigned long tiempo_avance_3 = 11000; // Third advance time in milliseconds unsigned long tiempo_parada_3 = 5000; // Third hold time in milliseconds unsigned long tiempo_avance_4 = 2000; // Quarter advance time in milliseconds unsigned long tiempo_parada_4 = 10000; // Fourth wait time in milliseconds unsigned long tiempo_retroceso = 31000; // Rollback time in milliseconds void setup() { pinMode(x_paso, OUTPUT); pinMode(x_dire, OUTPUT); pinMode(x_habi, OUTPUT); pinMode(x_limite, INPUT_PULLUP); // Configure move limit pin as input with pull-up resistor Serial.begin(9600); // Initialize serial communication Serial.println("Use the 's' command to start the movement."); } void loop() { if (Serial.available() > 0) { char comando = Serial.read(); if (comando == 's') { iniciarMovimiento(); } } } void iniciarMovimiento() { // Advance for 6 seconds avanzar(tiempo_avance_1); // Wait for 5 seconds delay(tiempo_parada_1); // Advance for 12 seconds avanzar(tiempo_avance_2); // Wait for 5 seconds delay(tiempo_parada_2); // Advance for 11 seconds avanzar(tiempo_avance_3); // Wait for 5 seconds delay(tiempo_parada_3); // Advance for 2 seconds avanzar(tiempo_avance_4); // Wait for 10 seconds delay(tiempo_parada_4); // Rollback for 31 secondss retroceder(tiempo_retroceso); } void avanzar(unsigned long tiempo) { digitalWrite(x_dire, LOW); // Set forward direction digitalWrite(x_habi, LOW); // Enable the engine unsigned long tiempo_inicio = millis(); while (millis() - tiempo_inicio < tiempo) { digitalWrite(x_paso, HIGH); delayMicroseconds(1000000 / pasos_por_segundo); digitalWrite(x_paso, LOW); delayMicroseconds(1000000 / pasos_por_segundo); } digitalWrite(x_habi, HIGH); // Disable the engine } void retroceder(unsigned long tiempo) { digitalWrite(x_dire, HIGH); // Set backward direction digitalWrite(x_habi, LOW); // Enable the engine unsigned long tiempo_inicio = millis(); while (millis() - tiempo_inicio < tiempo) { digitalWrite(x_paso, HIGH); delayMicroseconds(1000000 / pasos_por_segundo); digitalWrite(x_paso, LOW); delayMicroseconds(1000000 / pasos_por_segundo); } digitalWrite(x_habi, HIGH); // Disable the engine