casemod:
Can someone advice how to convert an analog reading into a variable frequency pulse (10 to 1Khz) using the arduino?
My idea won't give you a perfect result but it might be good enough.
int outpin = 13;
bool output;
unsigned long start_time;
unsigned long half_period;
void setup()
{
output = false;
}
void loop()
{
if (output == false)
{
int V_in = analogRead(0);
int frequency = map(V_in, 0, 1023, 10, 1000); // Convert analog value to frequency.
half_period = (1000000/(2*frequency)); // Half time-period in microseconds.
output = true;
}
while (output)
{
if ( (micros()-start_time) > half_period )
{
digitalWrite(outpin,!digitalRead(outpin)); // Toggle the output pin.
start_time = micros();
}
}
}
I verified the code but I can't test it because my Arduino serial programmer isn't working right now.