Can't print a variable with my lcd

Hi, I’ve been working on a project and I’m stucked in this part. I use a microphone (MAX9814) to measure the sound and I get a variable which I want to represent on my lcd board.

The problem is that the lcd prints this variable and, when the microphone gets another value, it prints it again after the value that is already printed. I want it to substitute the value in the lcd each time the microphone gets a new one.

Here is my code I hope you guys can help me.

#include <LiquidCrystal.h>

const int sampleWindow = 250; 
unsigned int sample;

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
int COLS=16;
int ROWS=2;

void setup()
{
 lcd.begin(COLS, ROWS);
 Serial.begin(9600);
 lcd.setCursor(0,0);
 lcd.clear();
}
void loop()
{
 unsigned long startMillis= millis(); 
 unsigned int peakToPeak = 0; 
 unsigned int signalMax = 0;
 unsigned int signalMin = 1024;

 while (millis() - startMillis < sampleWindow) // millis() sigue aumentando pero startMillis mantiene el valor inicial por lo que creamos un intervalo de tiempo que en cierto momento supera a sample Window
 {
 sample = analogRead(0);
 if (sample < 1024) // en este if estamos definiendo un máximo y un mínimo, en la primera lectura durante el intervalo ambos serán iguales pero conforme se vayan haciendo mas lecturas se irán definiendo los valores máximo y mínimo
 {
 if (sample > signalMax) 
 {
 signalMax = sample;
 }
 else if (sample < signalMin)
 {
 signalMin = sample;
 }
 }
 }
 peakToPeak = signalMax - signalMin; //de esta forma se define la amplitud de la onda, que viene ligada a la presión sonora y por tanto a los decibelios que queremos obtener aunque no conocemos la relación
 double volts = (peakToPeak * 10.0) / 1024; //transforma la amplitud en voltaje que envía a arduino

 
int pordec = volts*19;
unsigned int porent = pordec;


 lcd.clear();
 delay(2);
 lcd.print(porent);
 lcd.setCursor(0,0);

 Serial.println(porent);
}

The “pordec” and “porent” thing is what I used to transform volts (from 0 to 5.2 aprox.) to a percentage that is easier to understand.

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

Pin 1 is the hardware serial TX pin. Use a different pin for the LCD.

The clear() function is slow and can cause flickering of the display. I use spaces and setCursor() to clear the old data.

lcd.setCursor(0,0);
lcd.print("                "); // 16 spaces for a 2x16 display
lcd.setCursor(0,0);
lcd.print(porent);

Thank you very much, it worked!!! I really appreciate it because I was very frustrated with this and you helped me to continue with my project, again, thank you.

This topic was automatically closed after 45 days. New replies are no longer allowed.