How can I get a smooth signal from a chaotic audio one?

My goal is to make a led fade in the rythm of music. I've connected one channel the analog pin on the arduino UNO throught a capacitor and a resistance. However I have 2 issues:

1: My audio signal vary betwen around 150 and 500 bits

2: It oscillates a lot so my led would turn on and off (I want to make it fade)

How can I make it vary betwen 0 and 255 and make it more smooth (to not oscillate that hard)? And Which components should I use exactly? I'm a newbie on this subject.

...You didn't show us your schematic and you didn't show us your code.

What's connected? Is it a microphone board? A line-level or headphone-level signal?

Is the signal biased? I'm guessing no... Typically you bias the input (most microphone boards are biased) because the Arduino can't read the negative half of the AC audio waveform. Then you can subtract-out the bias in software. Negative voltages can actually damage the Arduino, and/or the signal can be "damaged" (distorted).

1: My audio signal vary betwen around 150 and 500 bits

I'm guessing that's the result of analogRead()? That's not "bits", it's a regular-old integer.

2: It oscillates a lot so my led would turn on and off (I want to make it fade)

Of course the raw-sampled audio data will "look random." You are sampling a waveform that swings between positive and negative, and since it's positive half the time and negative half the time it has an average value of zero. So typically, you'd find the peaks, the average (or moving average) of the peaks, or the average of the absolute value, or the average of the positive values, etc.

How can I make it vary betwen 0 and 255

Once you get useful readings you can use the map() function to get 0-255. Look at the Smoothing Example for smoothing.

DVDdoug: What's connected? Is it a microphone board? A line-level or headphone-level signal?

It is a headphone jack, I divided mo phone's jack by 2 so I can connect it to the Arduino and the speakers.

I'm guessing that's the result of analogRead()?

Yes it is.

Sorry for not sharing the code but the issue is that I don't even know how it should look. There's something that I wrote but it's a part of one big code so I share just a part of it.

For the moment I can fix the color for an rgb led RED GREEN andBLUE. Then the program is supoposed to take those values, divide them by 255 and multiply by the average value of analogRead.

#define LED_RED_PIN             9
#define LED_GREEN_PIN           10
#define LED_BLUE_PIN            11
#define CHANNEL_PIN             A0

float RED_VALUE =               25;             //Define the initial value for red colour
float GREEN_VALUE =             255;             //Define the initial value for green colour
float BLUE_VALUE =              255;             //Define the initial value for blue colour

int Level_Value =      0;

void setup() {
  Serial.begin(9600);

  pinMode(LED_RED_PIN, OUTPUT);     //Analog output for the red colour
  pinMode(LED_GREEN_PIN, OUTPUT);   //Analog output for the green colour
  pinMode(LED_BLUE_PIN, OUTPUT);    //Analog output for the blue colour

}

void loop() {
  int Level_Value = analogRead(CHANNEL_PIN);


  analogWrite(LED_RED_PIN, (RED_VALUE / 255) * Level_Value);
  analogWrite(LED_GREEN_PIN, (GREEN_VALUE / 255) * Level_Value);
  analogWrite(LED_BLUE_PIN, (BLUE_VALUE / 255) * Level_Value);
  Serial.println(Level_Value);

}

NOTE: As I don't use the capacitor the signal is in a good range 0;255

Good keywords to search for are "envelope detection" and "VU meter"

throught a capacitor and a resistance.

In series with the input? That's not right... The input will "float" to an undefined value. And then your signal will ride on top of that floating input.

Since you are not reading 0-255 you should use the [u]map()[/u] function.

And look at the [u]Smoothing Example[/u]. That's a moving average. You'll want to slow it down. The example has a 1ms delay (it's updated 1000 times per second). For a "fade" you'll probably want to update around 10 times per second or less and you may want more elements in your array, etc.

float RED_VALUE =               25;             //Define the initial value for red colour

It's not only your "initial value", you never change it. And it doesn't need to be a float.

analogWrite(LED_GREEN_PIN, (GREEN_VALUE / 255) * Level_Value);

I think I know what you're trying to do, but since GREEN-VALUE is always 255... that doesn't do much...

There is also no timing/delay so you're not going to see a "fade".

You're also not constraining Level_Value to 255 or less. I've never tried it, but I assume analogWrite() "rolls-over" if you exceed 255. That is, if you write 256 you'll have zeros in the 8 least-significant bits so you'll probably be writing zero.

I've never tried it, but I assume analogWrite() "rolls-over" if you exceed 255. That is, if you write 256 you'll have zeros in the 8 least-significant bits so you'll probably be writing zero.

Yes that is what it does.