Buen día,
Estoy armando algo simple con un paso a paso Nema 17, lo tenía casi listo pero se me ocurrio agregarle un TM1637 para que me muestre la cantidad de vueltas que ingreso con el encoder rotativo y que una vez que doy start me diga cuentas vueltas restan a medida que el motor gira.
Tengo básicamente dos problemas:
1- El motor, al variar su velocidad resulta que no en todos los puntos es sereno (en muchos puntos tiembla o bibra). Como se soluciona esto? estoy usando la AccelStepper justamente porque dicen que es la mejorcita.
2- El problema que surgió al colocar el TM1637: con solo conectarlo al arduino y encenderlo hace que el motor empiece a girar. Acaso hay un problema de compatibilidad?.
Este es el código hasta el momento:
#include <AccelStepper.h>
#include <GFButton.h>
#include <TM1637Display.h>
const int pinBoton = 4;
int id_dato = 0;
GFButton boton(pinBoton);
// Pote display
int change = 0;
int encoderVal = 0;
const int clkPin = 2;
const int dtPin = 3;
long count_rest = 0;
//Inicializa display
const int CLK = 10;
const int DIO = 11;
TM1637Display display(CLK, DIO);
int cant_vueltas = 0;
long valor_pote = 0;
int vel = 0;
const int dirPin = 5;
const int stepPin = 6;
const int motorInterfaceType = 1;
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup()
{
stepper.setMaxSpeed(2000);
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(CLK, OUTPUT);
pinMode(DIO, OUTPUT);
display.setBrightness(3);
}
void loop()
{
change = getEncoderTurn();
encoderVal = encoderVal + (change);
cant_vueltas = encoderVal * 10;
display.showNumberDec(cant_vueltas, false);
if(not boton.isPressed()){
delay(50);
if(not boton.isPressed()){
id_dato = id_dato + 1;
cant_vueltas = cant_vueltas * 200;
Motores();
}
}
}
void Motores() {
stepper.setCurrentPosition(0);
// 200 pasos = 1 vuelta
while(stepper.currentPosition() < cant_vueltas)
{
valor_pote = analogRead(A0);
if(valor_pote == 0){ valor_pote = 50;}
if((valor_pote > 125) & (valor_pote <= 224)){ valor_pote = 150;}
if((valor_pote > 225) & (valor_pote <= 324)){ valor_pote = 250;}
if((valor_pote > 325) & (valor_pote <= 424)){ valor_pote = 350;}
if((valor_pote > 425) & (valor_pote <= 524)){ valor_pote = 450;}
if((valor_pote > 525) & (valor_pote <= 624)){ valor_pote = 550;}
if((valor_pote > 625) & (valor_pote <= 724)){ valor_pote = 650;}
if((valor_pote > 725) & (valor_pote <= 824)){ valor_pote = 750;}
if((valor_pote > 825) & (valor_pote <= 924)){ valor_pote = 850;}
if(valor_pote > 920){ valor_pote = 950;}
vel = valor_pote;
stepper.setSpeed(vel);
stepper.runSpeed();
count_rest = ((cant_vueltas - stepper.currentPosition())/200);
display.showNumberDec(count_rest, false);
}
}
int getEncoderTurn(void)
{
static int oldA = HIGH;
static int oldB = HIGH;
int result = 0;
int newA = digitalRead(dtPin);
int newB = digitalRead(clkPin);
if (newA != oldA || newB != oldB)
{
if (oldA == HIGH && newA == LOW)
{
result = (oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}