Frequency Sweep Loop

Hello

I am trying to code a frequency sweep for the arduino, I want it to go from 1 Hz (on/off = 500,000 microseconds) to 10,000 Hz (on/off = 500 microseconds) (on time = off time = 50% duty) and then from 10,000 Hz back down to 1 Hz and keep sweeping back and forth

This is coming out of pin 13, labeled PulsePin, using delayMicroseconds.

I am not sure how to make the loop work, I know basically what I want it to do, but not the proper way to code the loop.

In half english and half code I would like it to go something like this (but maybe there is a much better way to do this)

long sweep = 500000
int sweepDirection = 1 // 1 is increasing frequency, 2 is decreasing frequency
int PulsePin = 13

loop // I need help on defining the loop control conditions

sweep = 500000 // starting case for 1 Hz
sweepDirection = 1 // starting case for increasing frequency

digitalWrite(PulsePin, HIGH)
delayMicroseconds(sweep)
digitalWrite(PulsePin, LOW)
delayMicroseconds(sweep)

(if I want to have it pulse the same frequency multiple times before changing frequency I can repeat the above 4 lines as desired, or loop it for the desired number of pulses)

//then i want to subtract 500 from sweep

if sweepDirection is 1
... then
... ... if sweep is greater than 1000 // checks if at end of sweep
... ... ... then // not at end of sweep
... ... ... ... sweep = sweep - 500 // remove 500
... ... ... else // yes at end of sweep
... ... ... ... sweepDirection = 2 // flip sweep direction to decreasing
... ... ... ... sweep = sweep + 500 // add 500 to start sweeping in decreasing direction
... else //sweepDirection is 2 (or not 1) // already on decreasing sweep
... ... if sweep is less than 500000 // checks if at end of sweep
... ... ... then // not at end of sweep
... ... ... ... sweep = sweep + 500 // add 500
... ... ... else // yes at end of sweep
... ... ... ... sweepDirection = 1 // flip sweep direction to increasing
... ... ... ... sweep = sweep - 500 // remove 500 to start sweeping in increasing direction

end of loop

-=-=-

This will pulse an optocoupler, that will pulse an SCR, that will turn on an off a larger power source that powers the audio output.
Here is the circuit now,


I currently have it hooked up to a potentiometer to vary the frequency manually, but I would like to set up the frequency sweep as described.

Thanks for your help.

I just realized that the period would sweep linearly but the frequency would sweep as 1/x which is not what i am looking for, so i will have to adjust the formula so that the frequency varies linearly, and the period will vary as 1/x. I can fix this, but I am still hoping for some assistance with the proper way to format the loop code.

Hi Donald

Sorry I do not have an answer butttttt I am doing close to the same thing I need from 1 HZ To1000HZ using a potentiometer My problem is I can not get below 50 HZ it just cuts off.were you able to get below 50HZ with yout potentiometer set up. will you post the code that you used.Any help would be great

Thanks Doug

If u're going to check if it's greater than 1000, and a different condition (less than 500000), it will be hard to do it with if's i think.
I think you'd better do it with a for condition.
(e.g.

   for(unsigned long sweep=500000;sweep>1000;sweep+= 500);
{
       //code to execute
}

and the same for the other direction, so you're sure that it won't get stuck, as it will the way you're writing it now.
if u check sweep > 1000 , and if it's false it goes to the +500 part, it will be 1500, then it is > 1000 , and will get subtracted by 500 again, not going over these two values.
If u get what i mean ?

But i suggest trying to write it yourselves, that way you learn the most, definitely, and if you can't figure out or have questions, people here will help gladly.
Also they have something to comment on then, instead of being asked to write your code :slight_smile:

Regards