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.