Go Down

Topic: (Random) Brightness output to control volume level (Read 996 times) previous topic - next topic

_buchan

Hello, I'm new to Arduino and am trying to set up a simple circuit whereby the brightness of the LED affects the volume of a sound. The brighter the LED, the louder the volume.

My code currently makes the LED flash like a heartbeat, starting the beating loop at random times.

Here is my code:
Code: [Select]

int ledPin = 9;    // LED connected to digital pin
int val = 0;

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

void loop()  {

val = analogRead(ledPin);
Serial.print(val);

  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
  analogWrite(ledPin, fadeValue);         
    // wait for 20 milliseconds to see the dimming effect   
    delay(20);                           
  }
 
  delay (random(20, 3000));
  analogWrite(ledPin, 0);
  delay (80);
  analogWrite(ledPin, 255); 
  delay (100);
  analogWrite(ledPin, 0);
  delay (80);
  analogWrite(ledPin, 255); 

  // fade out from not-quite-max to min in increments of 5 points:
  for(int fadeValue = 200 ; fadeValue >= 0; fadeValue -=random(2,20)) {
    // sets the value (range from 0 to 200):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }
}


I understand that if I want to control sound, I'll have to go through Processing. To do that, I assume that I need Serial Monitor to be printing out the brightness values going into the LED.
Obviously, the current Serial.print command does not work at all.

I need guidance on converting the Serial.print from 0  - 1023 to 0 - 255, printing the correct values correctly, and how I'm actually going to play the sound. I was thinking of just having the sound playing on a loop in something like VLC, and have processing control the volume within VLC.

Thank you.

DVDdoug

#1
Jan 05, 2016, 07:37 pm Last Edit: Jan 05, 2016, 07:39 pm by DVDdoug
So what you are trying to do is control the brightness of the LED and and the sound volume together...   The brightness of the LED isn't important, it's the variable that's controlling the brightness. 

What kind of sound?  An MP3 (or other audio file), or a simpler tone?

Quote
I assume that I need Serial Monitor to be printing out the brightness values going into the LED.

Obviously, the current Serial.print command does not work at all.
It doesn't work by itself, you need something running on the computer-side to read the data and do something with it.  But otherwise, it's not so obvious to me...

Quote
I need guidance on converting the Serial.print from 0  - 1023 to 0 - 255, printing the correct values correctly
You can use the map() function for that.   But if you can figure-out how to use 0-1023, I'm not sure why you can't use 0-255. 

Quote
and how I'm actually going to play the sound. I was thinking of just having the sound playing on a loop in something like VLC, and have processing control the volume within VLC.
I can't help you with that.  I've never interfaced the Arduino with the computer (except for using the serial monitor).       It's probably easier to interface with the Windows volume control than trying to access the application's volume control.    But, I said easier,   I didn't say easy...

Do you have to start/stop/select the sound, or only control the volume.

Or use digital pots, controlled by the Arduino, and pass the audio out of the computer and through the pots.

Or, maybe you can get an audio shield for the Arduino and play the sound through that instead of using the computer.

_buchan

Quote
So what you are trying to do is control the brightness of the LED and and the sound volume together...
No I don't want to control the brightness. I have the LED pulsing in a heartbeat-like way, and it starts at random times. That's what this part of the code does:
Code: [Select]

delay (random(20, 3000));
  analogWrite(ledPin, 0);
  delay (80);
  analogWrite(ledPin, 255); 
  delay (100);
  analogWrite(ledPin, 0);
  delay (80);
  analogWrite(ledPin, 255);


I just want the volume of a sound file to increase and decrease at the same time the LED brightness increases and decreases, going with the fading of the LED, so it's gradual, rather than the sound immediately being full volume, then immediately going off.

Quote
Do you have to start/stop/select the sound, or only control the volume.
The mp3 file can be constantly playing on a loop, with just the volume being affected.

Part of me is thinking that I should do all the coding in Processing, and have all the signals going into Arduino and something like Minim?

_buchan

So I got the code working in Processing to control the LED
Code: [Select]
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

int ledPin = 9;

void setup()
{
  //println(Arduino.list());
  arduino = new Arduino(this, "/dev/cu.usbmodem1411", 57600);
  arduino.pinMode(ledPin, Arduino.OUTPUT);
 
}

void draw()
{  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    arduino.analogWrite(ledPin, fadeValue);         
    // wait for 20 milliseconds to see the dimming effect   
    delay(20);                           
  }
 
  delay(1000);
  arduino.analogWrite(ledPin, 0);
  delay (80);
  arduino.analogWrite(ledPin, 255); 
  delay (100);
  arduino.analogWrite(ledPin, 0);
  delay (80);
  arduino.analogWrite(ledPin, 255); 

  // fade out from not-quite-max to min in increments of 5 points:
  for(int fadeValue = 200 ; fadeValue >= 0; fadeValue -=random(2,20)) {
    // sets the value (range from 0 to 200):
    arduino.analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }
}


Don't suppose anyone knows how to translate those arduino.analogWrite values into values which can control the volume of playback in Minim? I'm guessing this is more a question for the Processing forums...

Go Up