Ok so I am working on making a portable vu meter using the arduino. Last week I had a basic one running using standard LED's and this code
/*
|| A simple VU meter using arduino with analog input smoothing
||
|| Contributed:
|| Alexander Brevig
*/
//defines
#define NUMBER_OF_LEDS 8
#define NUMBER_OF_SAMPLES 20
#define DEBUG true
#define VU_METER_DISPLAY_DELAY 10
//analog in pins
byte voltageReferencePin = 0;
byte voltagePin = 2;
//digital out pins
//store the leds in an array, this way changing them later wont be a hazzle
byte ledPins[NUMBER_OF_LEDS] = {
6 , 7, 8 , 9 , 10 , 11 , 12 , 13 };
//convert analogRead to VU ledscale
byte voltageComparisonThresholds[] = {
0 , 10 , 20 , 30 , 40 , 50, 60, 70 };
int voltageReference = 0; //contain analogRead(voltageReferencePin);
//sample variables
int samples[NUMBER_OF_SAMPLES] = {
0};
int sample = 0;
int sampleTotal = 0;
byte sampleIndex = 0;
void setup() {
for (byte i=0; i<NUMBER_OF_LEDS; i++){
pinMode(ledPins[i],OUTPUT);
}
pinMode(voltageReferencePin,INPUT);
pinMode(voltagePin,INPUT);
if(DEBUG){
Serial.begin(9600);
}
}
void loop(){
//SMOOTH See Examples->Analog->Smoothing
sampleTotal -= samples[sampleIndex];
samples[sampleIndex] = analogRead(voltageReferencePin);//read value
sampleTotal += samples[sampleIndex++];
if (sampleIndex >= NUMBER_OF_SAMPLES) {
sampleIndex = 0;
}
sample = sampleTotal / NUMBER_OF_SAMPLES;
if(DEBUG){
Serial.print("virtual vu: ");
}
//DISPLAY_RESULT
//see Examples->Digital->Loop
for (byte i=0; i<NUMBER_OF_LEDS; i++){
if ( analogRead(voltagePin) >= voltageReference + voltageComparisonThresholds[i] ) {
digitalWrite(ledPins[i],HIGH);
if(DEBUG){
Serial.print("|");
}
}
else{
digitalWrite(ledPins[i],LOW);
if(DEBUG){
Serial.print(" ");
}
}
delay(VU_METER_DISPLAY_DELAY);
}
if(DEBUG){
Serial.println(" ");
}
}
I then moved on to use a digitally addressable RGB LED strip I purchased from adafruit.
Working with a friend of mine we came up with this code.
#include "HL1606strip.h"
#define STRIP_D 4
#define STRIP_C 3
#define STRIP_L 2
#define NUMBER_OF_LEDS 16
#define NUMBER_OF_SAMPLES 10
#define DEBUG true
#define VU_METER_DISPLAY_DELAY 10
byte voltageReferencePin = 0;
byte voltagePin = 2;
byte voltageComparisonThresholds[] = {0, 5, 10, 15, 20, 25, 30, 35,
40, 45, 50, 55, 60, 65, 70, 75};
int voltageReference = 0;
int temp = 0;
int samples[NUMBER_OF_SAMPLES] = {0};
int sample = 0;
int sampleTotal = 0;
byte sampleIndex = 0;
HL1606strip strip = HL1606strip(STRIP_D, STRIP_L, STRIP_C, 16);
void setup(void)
{
pinMode(voltageReferencePin,INPUT);
pinMode(voltagePin,INPUT);
if(DEBUG)
{
Serial.begin(9600);
}
for(int i=1; i<=NUMBER_OF_LEDS; i++)
{
strip.setLEDcolor(i, BLACK);
}
strip.writeStrip();
}
void loop(void)
{
//SMOOTH See Examples->Analog->Smoothing
sampleTotal -= samples[sampleIndex];
samples[sampleIndex] = analogRead(voltageReferencePin);//read value
sampleTotal += samples[sampleIndex++];
if (sampleIndex >= NUMBER_OF_SAMPLES)
{
sampleIndex = 0;
}
sample = sampleTotal / NUMBER_OF_SAMPLES;
if(DEBUG)
{
Serial.print("virtual vu: ");
}
//DISPLAY_RESULT
//see Examples->Digital->Loop
for (byte i=0; i<NUMBER_OF_LEDS; i++)
{
if(analogRead(voltagePin) >= voltageReference + voltageComparisonThresholds[i])
{
strip.setLEDcolor(i, GREEN);
if(DEBUG)
{
Serial.print("|");
}
}
else
{
strip.setLEDcolor(i, BLACK);
if(DEBUG)
{
Serial.print(" ");
}
}
delay(VU_METER_DISPLAY_DELAY);
}
if(DEBUG)
{
Serial.println(" ");
}
strip.writeStrip();
}
Several problems have arisen though.
One the LED strip likes to strobe blue when it should be off.
Two the basic vu meter program no longer works, instead of displaying a smooth bar it will have gaps in the middle of it amd generally make no sense at all.
Let me know what you guys think, I am generally new at working with arduino's and programming so I am a bit lost and over my head with this project. :~