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.