Go Down

Topic: VU meter with peak hold (Read 3962 times) previous topic - next topic

thelukewarm

Hi, I am trying to do a VU meter with a "peak hold" effect in a 12LED bar like this one http://www.youtube.com/watch?v=AWiVHym2jKQ and I already have the VU meter code but I can't make the peak hold effect, my main problem is that I don't know how to store the analog value so that the Arduino leaves the peak led HIGH while it continues reading the audio input.
I'm not very experienced on this stuff so any suggestions will be helpful  :smiley-yell:

My code is this:

void setup (){
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}

void loop (){
int value = analogRead(A0);
value = map (value, 0, 127, 0, 12);

switch (value) {
case 0:
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break; 
case 1:
  digitalWrite(2,HIGH);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
case 2:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
case 3:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
case 4:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
case 5:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
case 6:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
case 7:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
case 8:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
  case 9:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
  case 10:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,HIGH);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
  delay(20);
  break;
  case 11:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,HIGH);
  digitalWrite(12,HIGH);
  digitalWrite(13,LOW);
  delay(20);
  break;
  case 12:
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  digitalWrite(5,HIGH);
  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(11,HIGH);
  digitalWrite(12,HIGH);
  digitalWrite(13,HIGH);
  delay(20);
  break;
}

}
"Anything that can go wrong will go wrong"

Magician

similar topic:
http://arduino.cc/forum/index.php/topic,73935.0/topicseen.html

CrossRoads

Instead of switch(value), try this.

create an ledarray for the pins:
ledarray[2,3,4,5,6,7,8,9,10,11,12,13];

then before reading again, keep the prior analog read:

if (value>oldvalue){
oldvalue = value;
}

next read the analog pin, and write  the bargraphs:

for (x=2 to value){
digitalWrite (ledarray
  • , HIGH);
    next x;
    }
    if (value<13){
    for (x=(value+1) to 13){
    digitalWrite(ledarray
  • , LOW);
    next x;
    }
    }
    if (oldvalue>value){  // keep the high LED lit up
    digitalWrite(ledarray[oldvalue], HIGH);
    highvaluecount = highvaluecount-1;  //provide a way for it to fall back down
    if (highvaluecount ==0){
    oldvalue = oldvalue-1;
    }
    }

    }
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

thelukewarm

Thanks, now I am able to keep the oldvalue HIGH but I can't make it go down, I think I need a delay to make the "go down one by one effect" visible to the eye but when I use it all the program gets affected by the delay and the analogRead becomes out of sync. Here's what I've got if you can help me:

int pinArray[] = {2,3,4,5,6,7,8,9,10,11,12,13};
int valor=0;
int oldvalue=0;

void setup (){
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}

void loop (){
if(valor>oldvalue){
   oldvalue=valor;
}

valor = analogRead(A0);
valor = map (valor, 0, 127, 0, 12);

for(int i=0; i<=valor; i++){
  digitalWrite(pinArray, HIGH);
}
   
if (valor<13){
   for(int i=valor+1; i<=13; i++){
     digitalWrite(pinArray, LOW);
  }
}

if(oldvalue>valor){
digitalWrite(pinArray[oldvalue], HIGH);
}

oldvalue=oldvalue-1;
delay(100);

}
"Anything that can go wrong will go wrong"

macegr

Create another global variable and use it to maintain a counter that tracks the number of loop executions. After so many, decrement the value controlling the visible level. That's the code-efficient way to do it.

Or if you want it to be time-accurate, at the expense of messing with LONG types, use the millis() timer to trigger a regular decrement ala BlinkWithoutDelay example.
Unique RGB LED Modules and Arduino shields: http://www.macetech.com/store

Magician

May be you'll find it helpful:
http://docs.macetech.com/doku.php/rusty_vu

robtillaart

hint:
Code: [Select]

int pinArray[] = {2,3,4,5,6,7,8,9,10,11,12,13};

void setup ()
{
for(int i=2; i<=13; i++) pinMode(pinArray[i] ,OUTPUT);
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

thelukewarm

#7
Oct 10, 2011, 05:34 am Last Edit: Oct 10, 2011, 05:39 am by CrossRoads Reason: 1
Sorry I forgot about this post XD I kinda solved the problem counting the number of times the Arduino reads de code and after a lot of reads the peak LED goes down by one, here's the code if someone is interested and thanks :)
Code: [Select]

int pinArray [] = {2,3,4,5,6,7,8,9,10,11,12,13};
int valor=0;
int valoranterior=0;
int x=0;

void setup (){
for(int i=2; i<=13; i++) pinMode(pinArray[i] ,OUTPUT);
}

void loop (){
if(valor>valoranterior){
valoranterior=valor;}
int p=0;
if(p=900000000){
  valor=valor/900000000;
  p=0;}
valor = analogRead(A0);
valor = map (valor, 0, 127, 0, 12);

switch (valor) {
case 0:
 digitalWrite(2,LOW);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;  
case 1:
 digitalWrite(2,HIGH);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
case 2:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
case 3:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
case 4:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
case 5:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
case 6:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,HIGH);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
case 7:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,HIGH);
 digitalWrite(8,HIGH);
 digitalWrite(9,LOW);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
case 8:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,HIGH);
 digitalWrite(8,HIGH);
 digitalWrite(9,HIGH);
 digitalWrite(10,LOW);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
 case 9:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,HIGH);
 digitalWrite(8,HIGH);
 digitalWrite(9,HIGH);
 digitalWrite(10,HIGH);
 digitalWrite(11,LOW);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
 case 10:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,HIGH);
 digitalWrite(8,HIGH);
 digitalWrite(9,HIGH);
 digitalWrite(10,HIGH);
 digitalWrite(11,HIGH);
 digitalWrite(12,LOW);
 digitalWrite(13,LOW);
 delay(20);
 break;
 case 11:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,HIGH);
 digitalWrite(8,HIGH);
 digitalWrite(9,HIGH);
 digitalWrite(10,HIGH);
 digitalWrite(11,HIGH);
 digitalWrite(12,HIGH);
 digitalWrite(13,LOW);
 delay(20);
 break;
 case 12:
 digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,HIGH);
 digitalWrite(8,HIGH);
 digitalWrite(9,HIGH);
 digitalWrite(10,HIGH);
 digitalWrite(11,HIGH);
 digitalWrite(12,HIGH);
 digitalWrite(13,HIGH);
 delay(20);
 break;
}
if(valoranterior>valor){
digitalWrite(pinArray[valoranterior-1], HIGH);
}

p=p+1;
x=x+.001;
if(x=2000000000){
valoranterior=valoranterior-1;
delay(20);}
}


"Anything that can go wrong will go wrong"

robtillaart

Quote
int p=0;
if(p=900000000){
   valor=valor/900000000;
   p=0;}


think 9000000 is out of range for an int, => use long

and the if condition should be  ==  iso  =

my 2 cents,
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

dangnoi

Dear Guy, Can I apply to ATMEL microcontroller? AT89Cxxxx and do you mind if I need you help to show the example of source code.

robtillaart

The forum is here to ask questions and help each other so post your questions, include your code sofar, tell about the goal of the project, the hardware used, schematics, and tell as much as you can about the problem encountered. What did you expect and what did you got. The more you tell the better answer you can expect.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up