Go Down

Topic: Trying to make a VU-Meter with Peak Hold to fall down please help! (Read 3808 times) previous topic - next topic

wilson3682

Hi, i'm new to the Arduino programming, I've been trying to make this vu-meter with peak hold and make the top led to fall down slowly, the code works but the top led falls too fast. Can someone help me please?. Thanks!

Here is the code:

Code: [Select]


int value=0;
int audioValue;                 // Analog value read from audio channel
int maxAudioValue = 0;          // Maximum analog value read from audio channel
int ledLevel;
const int ledCount = 10;
const int numReadings = 20;
int readings[numReadings];
int peak =0;
boolean showPeak = false;
int peakValue = 0;
long lastPeak = 0;

void setup (){

for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}

void loop (){
int ledPins[] = {2,3,4,5,6,7,8,9,10,11};
for (int i = 0; i < ledCount; i++)
pinMode(ledPins[i], OUTPUT);

value = analogRead(A1);
if (value > maxAudioValue)
maxAudioValue = value;

ledLevel = map(value, 0, 1023, 0, 50)-1;
if (ledLevel > ledCount) {
 ledLevel = ledCount;
 }

//===============================================================  
if (millis() > (lastPeak + 2000))
{
  peakValue = peakValue -1;
}
 if (ledLevel > peakValue)
{
  peakValue = ledLevel;
  lastPeak = millis();
  
}

 for (int i=0; i<10; i++)  {
  if (peakValue > ledLevel) {
  digitalWrite(ledPins[i], (i <= ledLevel || (!showPeak && i == peakValue-1)));
  
  
  delay(1);
}
//==================================================================

else{
   digitalWrite(ledPins[i], LOW);

delay(5);
}
}
}




Grumpy_Mike

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

It would be a whole lot simpler if you used an envelope follower, then you can control the decay with the value of what is shown as 100K.


wilson3682

Thanks everyone.
I got it to work with the Adafruit's microphone and code from:
http://tech-zen.tv/index.php?option=com_content&view=article&id=265:vu-meter-episode-23&catid=20&Itemid=204 For those interested here is the code.
An a video https://www.youtube.com/watch?v=C5gmt7x1QWI

Update: New Video with more effects. https://www.youtube.com/watch?v=hTV2mioBmoM

Enjoy it!

Code: [Select]

//Arduino UNO vu-meter with Adafruit's Microphone and code.
//Also code from http://tech-zen.tv/index.php?option=com_content&view=article&id=265:vu-meter-episode-23&catid=20&Itemid=204
//Modified by Wilson Carvajal  https://www.youtube.com/watch?v=C5gmt7x1QWI

#define DC_OFFSET  6  // DC offset in mic signal - if unsure, leave 0
#define NOISE     10 // Noise/hum/interference in mic signal
#define SAMPLES   60  // Length of buffer for dynamic level adjustment
#define TOP       10 // Allow dot to go slightly off scale
int
vol[SAMPLES],       // Collection of prior volume samples
lvl       = 10,      // Current "dampened" audio level
minLvlAvg = 0,      // For dynamic adjustment of graph low & high
maxLvlAvg = 512;
int value=0;
int maxAudioValue = 0;          // Maximum analog value read from audio channel
int ledLevel;
const int ledCount = 10;
const int numReadings = 20;
int readings[numReadings];
boolean showPeak = false;
int peakValue = 0;
long lastPeak = 0;

void setup (){
  analogReference(EXTERNAL);
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  readings[thisReading] = 0;
}

void loop (){
  int ledPins[] = {2,3,4,5,6,7,8,9,10,11};
  for (int i = 0; i < ledCount; i++)
  pinMode(ledPins[i], OUTPUT);

  uint8_t  i;
  uint16_t minLvl, maxLvl;
  int n, height;
  n   = analogRead(A0); // Raw reading from mic
  n  = abs(n - 512 - DC_OFFSET); // Center on zero
  n  = (n <= NOISE) ? 0 : (n - NOISE);     // Remove noise/hum
  lvl = ((lvl * 7) + n) >> 3;    // "Dampened" reading (else looks twitchy)

  // Calculate bar height based on dynamic min/max levels (fixed point):
  height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);

  //value = analogRead(A1);
  if (n > maxAudioValue)
  maxAudioValue = n;

  ledLevel = map(n, 0, 1023, 0, 500)-1;
  if (ledLevel > ledCount) {
  ledLevel = ledCount;
  }

  if (millis() > (lastPeak + 1000))
  {
  peakValue = peakValue -1;
  
  }
  if (ledLevel > peakValue)
  {
  peakValue = ledLevel;
  lastPeak = millis();

  }

  for (int i=0; i<10; i++)  {
  if (peakValue > ledLevel) {
  digitalWrite(ledPins[i], (i <= ledLevel || (!showPeak && i == peakValue-1)));
  
  
  delayMicroseconds(4150);
  }

  }
}


Go Up