#include <ezButton.h>
#include <Stepper.h>
// --------- Driver Stepper ----------
const int PIN_STEP = 17;
const int PIN_DIR = 16;
const int PIN_ENABLE = 21; // A4988 ENABLE (activo en LOW)
// --------- Botones ----------
const int PIN_BTN1 = 25;
ezButton button1(PIN_BTN1);
const int PIN_BTN2 = 26;
ezButton button2(PIN_BTN2);
const int PIN_BTN3 = 32;
ezButton button3(PIN_BTN3);
const int TEST_LED = 27;
// -----Variables Velocidad-----
volatile int direction=1;
volatile int speed=0;
volatile int delayValue=1;
const int PIN_TEMP=34;
const int resolution = 8; // Resolución en bits (8 bits = 0-255)
void setup() {
Serial.begin(115200);
pinMode(34, INPUT);
analogRead(34);
button1.setDebounceTime(50);
button2.setDebounceTime(50);
button3.setDebounceTime(50);
// Pines A4988
pinMode(PIN_STEP, OUTPUT);
pinMode(PIN_DIR, OUTPUT);
pinMode(PIN_ENABLE, OUTPUT);
digitalWrite(PIN_ENABLE, LOW); // habilitado
// Encoder
// Botones
pinMode(PIN_BTN1, INPUT);
pinMode(PIN_BTN2, INPUT);
pinMode(TEST_LED, OUTPUT);
digitalWrite(TEST_LED,LOW);
// NTC
pinMode(PIN_TEMP, INPUT);
attachInterrupt(Btn1_GPIO, Ext_INT1_ISR, RISING);
Serial.println("Ready");
}
void loop() {
button1.loop();
button2.loop();
button3.loop();
/*int lecturaAnalogica=0;
float lectura_f=0;
lecturaAnalogica=analogRead(34);
lectura_f=(((float)lecturaAnalogica)/4095.0)*4095; */
//analogWrite(33, 2000);
if (button1.isPressed()){
speed++;
if (speed>3) speed=3;
}
if (button2.isPressed()){
speed--;
if (speed<-3) speed=-3;
}
if (button3.isPressed()){
Serial.print("Velocidad:");
Serial.print(speed);
if (speed!=0){
direction=getSign(speed);
delayValue=getDelay(speed);
digitalWrite(PIN_DIR, direction); // Definimos la dirección de giro
for (i=0; i<100; i++){
digitalWrite(PIN_STEP, HIGH);
delay(delayValue);
digitalWrite(PIN_STEP, LOW);
delay(delayValue);
}
}
}
}
bool getSign(int x) {
if (x >= 0) {
return 1;
} else if (x < 0) {
return 0;
} else {
return 0;
}
}
int getDelay(int speed) {
switch (speed) {
case 0:
return 0; // motor detenido
case 1:
case -1:
return 30; // delay para velocidad 1
case 2:
case -2:
return 20; // delay para velocidad 2
case 3:
case -3:
return 5; // delay para velocidad 3
default:
return 0; // por si el valor está fuera de rango
}
}