Hola, estoy intentando usar un Arduino uno para contar grados en una mesa rotativa, usando un encoder rotativo de 360 pulsos. Con la cuadratura son 1440 pulsos. Uso un código que he encontrado, que divide los 1440, y presenta grados con dos decimales, pero a veces "se vuelve loco" y cuenta números aleatoriamente. Solo quiero que cuente directamente los pulsos como 0,25, sin ninguna fórmula rara, creo que sería más fiable. Gracias
Hello , I'm trying to use an Arduino uno to show degrees for a rotary table, whit a rotary encoder, it has 360 pulses per rev. So cuadrature precision is 1440 ppr. I've used a code found on the net, it has a formula to divide 1440 / 360, but some times it adds random numbers. I just want the pulses beeing count as 0,25, but no find the way. Any help? Thanks.
Este es el código que uso:
#include <LCD16x2.h>
#include <Wire.h>
LCD16x2 lcd;
const int C1 = 3; // Entrada de la señal A del encoder.
const int C2 = 2; // Entrada de la señal B del encoder.
volatile int n = 0;
volatile byte ant = 0;
volatile byte act = 0;
unsigned long lastTime = 0; // Tiempo anterior
unsigned long sampleTime = 20; // Tiempo de muestreo
double P = 0;
double R = 1440;
void setup()
{
const int analog_pin = A0;
Wire.begin();
pinMode(analog_pin, INPUT);
int value = analogRead(analog_pin);
map(value, 0, 50, 0, 10);
lcd.lcdSetBlacklight(value);
Wire.begin();
lcd.lcdClear();
pinMode(C1, INPUT);
pinMode(C2, INPUT);
attachInterrupt(digitalPinToInterrupt(C1), encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(C2), encoder, CHANGE);
}
void loop() {
if (millis() - lastTime >= sampleTime || lastTime==0)
{ // Se actualiza cada sampleTime (milisegundos)
lastTime = millis();
P = (n*360.0)/R;
lcd.lcdGoToXY(1,1);
lcd.lcdWrite("GRADOS");
lcd.lcdGoToXY(10,1);
lcd.lcdWrite(P,2);
}
}
// Encoder precisión cuádruple.
void encoder(void)
{
ant=act;
if(digitalRead(C1)) bitSet(act,1); else bitClear(act,1);
if(digitalRead(C2)) bitSet(act,0); else bitClear(act,0);
if(ant == 2 && act ==0) n++;
if(ant == 0 && act ==1) n++;
if(ant == 3 && act ==2) n++;
if(ant == 1 && act ==3) n++;
if(ant == 1 && act ==0) n--;
if(ant == 3 && act ==1) n--;
if(ant == 0 && act ==2) n--;
if(ant == 2 && act ==3) n--;
}