How to add time/duration to series of frequencies

I am relatively new to arduino and have been messing around with using it to control an AD9850 frequency generator. Found a great tutorial and everything works just as expected. However, what I'd like to do is the ability of adding in a series of frequencies in the Arduino code along with set time frames for each.

So example

300hz -- 1 minute
800 hz -- 30 seconds
5hz -- 5 minutes

etc...

I assume I can do this by adding onto the existing code used in the tutorial. Here that is...

Any help from those more experiences than I would be greatly appreciated. Thanks in advance...

Arduino_FreqGeneratorCode.ino (1.69 KB)

You would set the frequency, start a timer, when the timer expires set the next frequency etc.

You could post the code, so that those us us posting from phones can see it.

Have a look at how to manage timing using millis() in several things at a time

...R

Hello AWOL,

I attached the .ino file but here is the raw code. The last line is where the single frequency can manually be entered. Again, my idea is to add a string of frequencies along with the desired run duration of each so it runs through them without manually changing and uploading after each one.

Any thoughts are appreciated.

/*

#define W_CLK 8 // Pin 8 - connect to AD9850 module word load clock pin (CLK)
#define FQ_UD 9 // Pin 9 - connect to freq update pin (FQ)
#define DATA 10 // Pin 10 - connect to serial data load pin (DATA)
#define RESET 11 // Pin 11 - connect to reset pin (RST).

#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }

// transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
for (int i=0; i<8; i++, data>>=1) {
digitalWrite(DATA, data & 0x01);
pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high
}
}

// frequency calc from datasheet page 8 = * /2^32
void sendFrequency(double frequency) {
int32_t freq = frequency * 4294967295/125000000; // note 125 MHz clock on 9850
for (int b=0; b<4; b++, freq>>=8) {
tfr_byte(freq & 0xFF);
}
tfr_byte(0x000); // Final control byte, all 0 for 9850 chip
pulseHigh(FQ_UD); // Done! Should see output
}

void setup() {
// configure arduino data pins for output
pinMode(FQ_UD, OUTPUT);
pinMode(W_CLK, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(RESET, OUTPUT);

pulseHigh(RESET);
pulseHigh(W_CLK);
pulseHigh(FQ_UD); // this pulse enables serial mode - Datasheet page 12 figure 10
}

void loop() {
sendFrequency(10.e6); // freq
while(1);
}

LarryD:
You would set the frequency, start a timer, when the timer expires set the next frequency etc.

Ok, but how do you draft this so that it is all handled in the arduino code at one time. The code now allows me to set a single frequency and upload it to the arduino board -- which works great. It creates a single frequency which runs indefinitely until its powered off or a new single frequency is uploaded.

I want to be able to write a series of frequencies along with each one having a desired run duration, which is then uploaded one time to run through its process.

Any ideas on the best way to modify the .ino provided to do this?

Robin2:
Have a look at how to manage timing using millis() in several things at a time

...R

Very informative Robin2. So how do you think it best to incorporate into the existing .ino file I've already been using?

We solved this recently for someone who wanted an Arduino to play "Happy Birthday" or something similar. Have a dig through the archives.

pletchman:
Very informative Robin2. So how do you think it best to incorporate into the existing .ino file I've already been using?

Maybe it would be better to ask yourself how you could incoporate your code into several things at a time (and obviously discarding parts that you don't need) ?

Also, have a look at planning and implementing a program.

You already have a function to set the frequency sendFrequency() so it should be straigthforward to call that at the appropriate moment.

...R

Robin2:
Maybe it would be better to ask yourself how you could incoporate your code into several things at a time (and obviously discarding parts that you don't need) ?

Also, have a look at planning and implementing a program.

You already have a function to set the frequency sendFrequency() so it should be straigthforward to call that at the appropriate moment.

...R

Great tutorials Robin2. I'm beginning to incorporate some of the information now.