I have the following code, where I'm reading interruptions from an encoder and summing them, then everytime 300 ms have passed, I calculate the frequency of the motor. I also print all of that in an LCD. The problem is that it appears the code never runs beyond the 300 ms.
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define encoderA 2
#define encoderB 3
const int potPin = A0;
float valor, Vmedida;
const float volt_ref = 4.93;
long oldPosition = -999;
long newPosition;
long ticks = 0;
const int periodo = 300; // tiempo de muestreo (ms)
int tiempoActual = 0;
float freqEncoder;
long listaEncoder[5000];
long listaHall[5000];
long contador = 0;
byte mas_menos[8] = {
B00100,
B00100,
B11111,
B00100,
B00100,
B00000,
B11111,
};
byte ohms[8] = {
B00000,
B01110,
B10001,
B10001,
B10001,
B01010,
B11011,
};
void setup() {
Serial.begin(115200);
lcd.createChar(0, mas_menos);
lcd.createChar(1, ohms);
lcd.begin(16, 2);
pinMode(encoderA, INPUT);
pinMode(encoderB, INPUT);
attachInterrupt(digitalPinToInterrupt(encoderB), sumarTicks, RISING);
}
void loop() {
/*
if (digitalRead(encoderA) == 1) {
ticks++;
}
*/
Serial.println(millis());
if (millis()-tiempoActual >= periodo) {
valor = analogRead(potPin);
Vmedida = valor/1024.0*volt_ref;
freqEncoder = ticks/(periodo/1000/60)/14; // Convierto periodo (ms) a minutos, calculo ticks por minuto y divido entre 14 ticks por revolucion, asi obtengo rev/min
listaEncoder[contador];
mostrarLCD(freqEncoder, Vmedida);
Serial.print("Frecuencia del encoder: "); Serial.println(freqEncoder);
Serial.print("Resistencia: ("); Serial.print(Vmedida); Serial.print(" +- ");
//Serial.print(incertidumbre);
Serial.println(") Ohms");
contador++;
ticks = 0;
lcd.clear();
tiempoActual = millis();
}
}
void mostrarLCD(float freqEncoder, float Vmedida) { // agregar incertidumbre como parámetro cuando se tenga
lcd.setCursor(0, 0);
lcd.print("Ticks: ");
lcd.print(ticks);
lcd.setCursor(0, 1);
lcd.print("Resistencia: ");
lcd.print("(");
lcd.print(Vmedida);
lcd.print(" ");
lcd.write(byte(0));
lcd.print(" ");
//lcd.print(incertidumbre);
lcd.print(") ");
lcd.write(byte(1));
}
void sumarTicks() {
ticks++;
}
As you can see, I print millis() on the Serial monitor to see what's happening and it only prints until 293 and the program stops completely.
I tried not using interrupts and just manually summing every time the digital pin is detected as high, but the same happens.