const int stepPin = 4;
const int speed_switchA = 26;
const int speed_switchB = 27;
const int stepMode = 25;
int Full_Steps_Revolution = 200;
const int Half_Steps_Revolution = Full_Steps_Revolution * 2;
int MS1 = 19;
//Velocidades (Speeds, Milisegundos):
const int speed1 = 50;
const int speed2 = 25;
const int speed3 = 10;
const int speed4 = 5;
const float half_step_divisor = 2.0;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(speed_switchA, INPUT_PULLDOWN);
pinMode(speed_switchB, INPUT_PULLDOWN);
pinMode(stepMode, INPUT_PULLDOWN);
pinMode(MS1, OUTPUT);
Serial.begin(115200);
}
void loop() {
bool switchA_State = digitalRead(speed_switchA);
bool switchB_State = digitalRead(speed_switchB);
bool stepMode_State = digitalRead(stepMode);
float selectedSpeed;
String speed_Display;
if (switchA_State == LOW && switchB_State == LOW) {
selectedSpeed = speed1;
speed_Display = " #1: ";
} else if (switchA_State == LOW && switchB_State == HIGH) {
selectedSpeed = speed2;
speed_Display = " #2: ";
} else if (switchA_State == HIGH && switchB_State == LOW) {
selectedSpeed = speed3;
speed_Display = " #3: ";
} else {
selectedSpeed = speed4;
speed_Display = " #4: ";
}
int steps_by_Revolution;
String step_Mode_Display;
// Modo de pasos completos:
if (stepMode_State == LOW) {
digitalWrite(MS1, LOW);
steps_by_Revolution = Full_Steps_Revolution;
Serial.print("Velocidad de Paso");
Serial.print(speed_Display);
Serial.print(selectedSpeed * 2);
Serial.print(" ms");
Serial.print(" | ");
step_Mode_Display = "Modo: Paso completo";
}
// Modo de medios pasos:
else {
digitalWrite(MS1, HIGH);
steps_by_Revolution = Half_Steps_Revolution;
selectedSpeed /= half_step_divisor;
Serial.print("Velocidad de Paso");
Serial.print(speed_Display);
Serial.print(selectedSpeed*2);
Serial.print(" ms");
Serial.print(" | ");
step_Mode_Display = "Modo: Medio paso";
}
Serial.println(step_Mode_Display);
// Ciclo de Revolución:
for(int i = 0; i < steps_by_Revolution; i++) {
digitalWrite(stepPin, HIGH);
delay(selectedSpeed);
digitalWrite(stepPin, LOW);
delay(selectedSpeed);
}
// Calculo para el tiempo de revolución:
unsigned long Revolution_Time = steps_by_Revolution * (selectedSpeed * 2);
Serial.print("Tiempo de revolucion: ");
Serial.print(Revolution_Time/1000);
Serial.print(" segundos");
Serial.print(" | ");
Serial.print("Cantidad de pasos realizados: ");
Serial.println(steps_by_Revolution);
Serial.println("\n");
delay(1000);
}