# Frequency generator/resonator - Help needed!

Hello everybody!

I need help with my new project.
I want to use arduino to,

• generate a frequency in a range of 500hz to 20khz
• search this whole range to find the frequency that gives the higher voltage to analog input, and lock into this frequency for 10sec
I have included arduinOSC library which is a very simple and stable square wave generator.

Here is my code:

``````#include <arduinOSC.h>
int freq = 500;
int goodf= 0;
int senspin = A0;
int volt = 1;
int voltmax = 0;
boolean lock = false;

void setup()
{

arduinOSC::initOSC(WAVEFORM_SQUARE);
pinMode(senspin, INPUT);
}

void loop()
{
if (lock==false)
{
for (freq=500; freq==20000; freq++)
{
arduinOSC::setOSCFrequency(freq);
if (voltmax < volt)
{
voltmax = volt;
}
else
{
lock = true;
goodf = freq;
arduinOSC::setOSCFrequency(goodf);
}
delay(50);
}
arduinOSC::setOSCFrequency(goodf);
}
else {
lock=false;
delay(10000);
}

}
``````

when i run this code on my Leonardo, I get an output frequency on pin 9 but it doesen’t change as I higher the voltage.
any help would be greatful.
Zis

arduinosc_0.1.zip (15.6 KB)

but it doesen’t change as I higher the voltage.

Why are there no Serial.begin() and Serial.print() statements in the code?

What is supplying the voltage that you think you are measuring?

``````     if (voltmax < volt)
{
voltmax = volt;
}
``````

You are performing a comparison, then an assignment, then a read. Looks like completely the wrong order of operations to me.

this line : ** for (freq=500; freq==20000; freq++)** looks wrong. should be freq<=20000

Thanks everybody for helping,

I came up with a code that is working.
But there are some debugging that i can’t make them work properly.

the new code is this:

``````#include <LiquidCrystal.h>
#include <arduinOSC.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int hz = 500;
int freq= 0;
int volt = 0;
int vmax = 0;

void setup()
{

lcd.begin(16, 2);
lcd.setCursor(0,0);
//Serial.begin(9600);
arduinOSC::initOSC(WAVEFORM_SQUARE);
pinMode(A0, INPUT);

}

void loop()
{
// vmax = 0;                //use if you want to set a new lock frequency each time
for (hz=3000; hz<=20000; hz++)
{
arduinOSC::setOSCFrequency(hz);
delay(1);
if (vmax < volt)
{
vmax = volt;
freq = hz;
}
}
arduinOSC::setOSCFrequency(freq);
//Serial.println(vmax);
//Serial.println(freq);
lcd.setCursor(1,0);
lcd.print(freq);

delay(10000);

}
``````

But… lcd.print() or other lcd functions do not work at all in the loop(in setup works)
also… Serial.flush() does not work. My serial screen fills up with my output
any ideas??

Thanks