This code is very sensible and have spectrometer VU.
Note: ajust the potenciometer with optimal sensibility.
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
double volts;
int led = 13;
int threshold = 500; //Change This
int volume;
int maxvolume;
const int sampleWindow = 3; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
unsigned int peakToPeak = 50;
LiquidCrystal_I2C lcd( 0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE );
// create custom characters for LCD
byte level0[8] = { 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000};
byte level1[8] = { 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111};
byte level2[8] = { 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111};
byte level3[8] = { 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111};
byte level4[8] = { 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
byte level5[8] = { 0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
byte level6[8] = { 0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
byte level7[8] = { 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
int strobe = 4; // strobe pins on digital 4
int res = 5; // reset pins on digital 5
int left[7]; // store band values in these arrays
int right[7];
int band;
void setup() {
analogReference(INTERNAL);
lcd.clear();
lcd.begin(16,2);
Serial.begin(9600); // For debugging
pinMode(led, OUTPUT);
maxvolume = 0;
lcd.begin(16, 2);
lcd.clear();
lcd.createChar(9,level0);
lcd.createChar(1,level1);
lcd.createChar(2,level2);
lcd.createChar(3,level3);
lcd.createChar(4,level4);
lcd.createChar(5,level5);
lcd.createChar(6,level6);
lcd.createChar(7,level7);
lcd.setCursor(0,1);
lcd.print("Left");
lcd.setCursor(11,1);
lcd.print("Right");
pinMode(res, OUTPUT); // reset
pinMode(strobe, OUTPUT); // strobe
digitalWrite(res,LOW); // reset low
digitalWrite(strobe,HIGH); //pin 5 is RESET on the shield
}
void readMSGEQ7()
// Function to read 7 band equalizers
{
for( band = 0; band < 7; band++ )
{
volume = analogRead(A0); // Reads the value from the Analog PIN A0
/*
//Debug mode
Serial.println(volume);
delay(100);
*/
if(volume>=threshold){
digitalWrite(led, HIGH); //Turn ON Led
}
else{
digitalWrite(led, LOW); // Turn OFF Led
}
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
left[band] = peakToPeak; // store left band reading
right[band] = peakToPeak; // ... and the right
Serial.println(peakToPeak);
}
}
void loop() {
readMSGEQ7();
// display values of left channel on LCD
//band = peakToPeak * 50;
for( band = 0; band < 7; band++ )
{
lcd.setCursor(band,0);
if (left[band]>=12) { lcd.write(7); } else
if (left[band]>=11) { lcd.write(6); } else
if (left[band]>=10) { lcd.write(5); } else
if (left[band]>=9) { lcd.write(4); } else
if (left[band]>=8) { lcd.write(3); } else
if (left[band]>=5) { lcd.write(2); } else
if (left[band]>=4) { lcd.write(1); } else
if (left[band]>=0) { lcd.write(9); }
}
delay(50);
// display values of right channel on LCD
for( band = 0; band < 7; band++ )
{
lcd.setCursor(band+9,0);
if (right[band]>=12) { lcd.write(7); } else
if (right[band]>=11) { lcd.write(6); } else
if (right[band]>=10) { lcd.write(5); } else
if (right[band]>=9) { lcd.write(4); } else
if (right[band]>=8) { lcd.write(3); } else
if (right[band]>=5) { lcd.write(2); } else
if (right[band]>=4) { lcd.write(1); }
if (right[band]>=0) { lcd.write(9); }
}
}