analog input code question

hey,
im running an audio signal through a TIP31 transistor to switch a 5v signal on and off. i want to then send that signal to a larger transistor to switch on and off a 12vdc power supply signal which is opening/closing a solenoid valve. im fairly new to this, so im not sure how to make this circuit without using the arduino, although im sure it could be done easily.
right now im sending an audio signal to the TIP31 which switches on and off an LED. i tried sending the + from there to an analog input on the arduino. it worked, but my code isnt quite accomplishing the same accuracy as the original LED is. any ideas?hardware or software? thanks

code:

int ledPin = 12; // LED connected to digital pin 12
int analogPin = 0; // music connected to analog pin 0
int val = 0; // variable to store the read value
int threshold = 700; // threshold how much force to control on or off
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
if (val >= threshold) {
digitalWrite(ledPin, HIGH); // sets the LED on
delay(100);
} else {
digitalWrite(ledPin, LOW); // sets the LED off
delay(100);
}
}

Hi erinbanwell,

the analogRead gives only a momentary input. I think it is better to read the analog input during the 100ms Delay, calculate the maximum during this time and compare this value to the threshhold.

good luck mike_pa

do you think you code help me out with some code example of what your talking about? i thought that delay was stopping all actions/processes, so how could it read during a delay? thanks for your help. erinbanwell

Can you describe a little more clearly what it is you are trying to accomplish?

It sounds like you want to control a mechanical solenoid based on some characteristic of an audio signal. The code you posted indicates that you are sampling the audio signal about 10 times per second and powering the solenoid when the sample is above 700.

Since audio signals fluctuate very quickly it seems unlikely that your 10Hz samples will represent anything recognizable in the audio signal.

Are you trying to do something like a sound level meter?

Yes it does sound like you are a little off in your use. It your intent is to activate a LED and solenoid when the audio level reaches or is above a certain volume then you really should do a little hardware conditioning on the audio signal before you wire and read it with the Arduino analog input pin.

You should first rectify the audio signal with a bridge rectifier and then followed with a RC filter. This will then give you a smooth DC voltage level that reflects the average voltage level of the audio signal level at any given point in time when you read the analog input pin.

Remember the Arduino analog input pin is designed to process DC voltages only that can vary from ground to +5vdc. An audio signal is a AC voltage that has both positive and negative instantaneous values.

Lefty