// --- COLEGIO REUVEN FEUERESTEIN ---
// --- LAURA, MAIA, RODRIGO, JUAN JOSE ---
// --- PROFESORES DIEGO Y RICHI ---
// --- PINES DE LOS 3 MOTORES NEMA 17 (A4988) ---
const int stepX = 2;
const int dirX = 3;
const int stepY = 4;
const int dirY = 5;
const int stepZ = 6;
const int dirZ = 7;
// --- PINES DEL JOYSTICK ---
const int pinVert = A0; // Controla Eje Y
const int pinHorz = A1; // Controla Eje X
const int pinBoton = A2; // Presionar para cambiar Eje Z (Lapiz)
// Variables de estado
bool estadoLapiz = false; // false = Arriba, true = Abajo
bool ultimoBoton = HIGH;
const int PASOS_Z = 100;
void setup() {
// Pines de los motores como salida
pinMode(stepX, OUTPUT);
pinMode(dirX, OUTPUT);
pinMode(stepY, OUTPUT);
pinMode(dirY, OUTPUT);
pinMode(stepZ, OUTPUT);
pinMode(dirZ, OUTPUT);
// Pin del botón con resistencia interna de tirón (Pullup)
pinMode(pinBoton, INPUT_PULLUP);
}
void loop() {
// Lectura de valores analógicos del Joystick (0 a 1023)
int valorHorz = analogRead(pinHorz); // Eje X
int valorVert = analogRead(pinVert); // Eje Y
int lecturaBoton = digitalRead(pinBoton);
// --- CONTROL DEL EJE X (Movimiento Izquierda / Derecha) ---
if (valorHorz > 700) { // Mover a la derecha
darPaso(stepX, dirX, HIGH);
} else if (valorHorz < 300) { // Mover a la izquierda
darPaso(stepX, dirX, LOW);
}
// --- CONTROL DEL EJE Y (Movimiento Arriba / Abajo) ---
if (valorVert > 700) { // Mover hacia adelante
darPaso(stepY, dirY, HIGH);
} else if (valorVert < 300) { // Mover hacia atrás
darPaso(stepY, dirY, LOW);
}
// --- CONTROL DEL EJE Z (Cambio de estado con el Click del Joystick) ---
if (lecturaBoton == LOW && ultimoBoton == HIGH) { // Se presionó el botón
estadoLapiz = !estadoLapiz; // Cambiar estado
moverLapizZ(estadoLapiz);
delay(200); // Evitar rebotes del botón
}
ultimoBoton = lecturaBoton;
}
// Función rápida para mover 1 solo paso según el Joystick
void darPaso(int pinStep, int pinDir, bool sentido) {
digitalWrite(pinDir, sentido);
digitalWrite(pinStep, HIGH);
delayMicroseconds(800);
digitalWrite(pinStep, LOW);
delayMicroseconds(800);
}
// Función para subir/bajar el Eje Z completamente
void moverLapizZ(bool bajar) {
digitalWrite(dirZ, bajar ? LOW : HIGH);
for (int i = 0; i < PASOS_Z; i++) {
digitalWrite(stepZ, HIGH);
delay(20);
digitalWrite(stepZ, LOW);
delay(20);
}
}