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:
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.