# Delay in waveform generator

Hello all,

I am using arduino due to create waveform generator. When I was referring the code of simplewaveform generator, here I came across delayMicroseconds. Why it has used? Is it to modify the frequency of the wave? The code is as follows

``````#include "Waveforms.h"

#define oneHzSample 1000000/maxSamplesNum  // sample for the 1Hz signal expressed in microseconds

const int button0 = 2, button1 = 3;
volatile int wave0 = 0, wave1 = 0;

int i = 0;
int sample;

void setup() {
analogWriteResolution(12);  // set the analog output resolution to 12 bit (4096 levels)

attachInterrupt(button0, wave0Select, RISING);  // Interrupt attached to the button connected to pin 2
attachInterrupt(button1, wave1Select, RISING);  // Interrupt attached to the button connected to pin 3
}

void loop() {
// Read the the potentiometer and map the value  between the maximum and the minimum sample available
// 1 Hz is the minimum freq for the complete wave
// 170 Hz is the maximum freq for the complete wave. Measured considering the loop and the analogRead() time
sample = map(analogRead(A0), 0, 4095, 0, oneHzSample);
sample = constrain(t_sample, 0, oneHzSample);

analogWrite(DAC0, waveformsTable[wave0][i]);  // write the selected waveform on DAC0
analogWrite(DAC1, waveformsTable[wave1][i]);  // write the selected waveform on DAC1

i++;
if(i == maxSamplesNum)  // Reset the counter to repeat the wave
i = 0;

delayMicroseconds(sample);  // Hold the sample value for the sample time
}

// function hooked to the interrupt on digital pin 2
void wave0Select() {
wave0++;
if(wave0 == 4)
wave0 = 0;
}

// function hooked to the interrupt on digital pin 3
void wave1Select() {
wave1++;
if(wave1 == 4)
wave1 = 0;
}
``````

It determines the sample rate. There is always a sample rate when an analog waveform is represented digitally.

For example, CD audio is sampled 44,100 times per second. When you play the music the ADC "connects the dots" to re-create the continuous audio waveform.

The sample rate doesn't normally control the frequency of the waveform, but it can... If you play CD audio at 22,050 Hz, it will play at half-speed and all of the frequencies in the music will be cut in half.

The [u]Audacity website[/u] has a little tutorial about digital sampling.

Of course, when you generate the waveform algorithmically there is no sampling (analog-to-digital step), and the samples may not be stored in memory.

Yes the delaymicroseconds() sets the time between samples. When you are generating a waveform using a fixed number of samples in the table this will indeed determine the frequency.

Steve