Cómo contar decimales / How to count decimals

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--; 

    }

use the encoder library

Thanks for answer, but that seems not suitable for me... maybe there's something simpler, as a code line telling the Arduino: "don't count 1 by 1, but 0,25 by 0,25"
Sorry I'm completely new on this.

why ??


your code needs a critical section when using n in the loop.

Why do you even want that? Just count 1 by 1. Then one revolution is 1440 divisions.

The moment you want to do calculations, and you need this arbitrary number of 360 divisions per full rotation, you just calculate that from the number your encoder gives you. Mind that the Arduino doesn't care AT ALL on how many divisions there are in a revolution, or whether you are counting revolutions or whatever. It is just us lowly humans that put such importance to specific numbers. Luckily computers are really good in making calculations, so they can present their numbers in any way you feel fit.

Furthermore, computers count in binary, that are integer numbers. Trying to make them count or calculate in floating point values is VERY inefficient. Integer calculations are fast and efficient.

Thanks all for answers, I'll try ways