Hi folks,
I'm working on generating a 40 Hz click train, where 10ms of noise will be output at 40Hz for 3 seconds repeatedly. I was originally using an Arduino Uno and a square pulse in place of noise, but I just got a Due for the DAC to do "noisy clicks".
I've put my code below. I'm not exactly sure what my issue is, but when I hook up DAC0 to an oscilloscope, all I'm getting is noise - not the noisy clicks at 40Hz, but low-power ambient noise. Although the square pulse from the Uno was sufficient to drive some computer speakers, I am aware that this signal may require amplification to drive the speakers, but it should at the very least be visible w/ a scope. Some possible issues may be the way I'm calling AnalogWrite() or map(), but I'm really at a loss on this one. No issues when I compile or upload the script, and staring at the IDE and Arduino Reference Pages hasn't done me much good, so I've turn to you nice people.
#include "math.h"
const int artifactPin = 13;
int Z = 4;
int S = 1;
int A = 1;
const int L = 1000;
double sig[L];
void setup()
{
analogWriteResolution(12);
randomSeed(S);
//generate stochastic signal 10ms long
for (int i = 0; i < L; i++)
{
sig[i] = random(0,1);
}
//find sum of squared values in signal
double sigsum = 0;
for (int i = 0; i < L; i++)
{
sigsum = sigsum + pow(sig[i],2);
}
//modulate signal with amplitude using sum of squared values
double amp = A / sqrt(2) * L / sigsum;
for (int i = 0; i < L; i++)
{
sig[i] = sig[i] * amp;
}
//rescale signal to 0-4096
for (int i = 0; i < L; i++)
{
sig[i] = map(sig[i],0,amp,0,4096);
}
}
void loop()
{
delay(15000);
//120 clicks over three seconds
for (int i = 0; i < 120; i++)
{
//1000 samples per click
for (int j = 0; j < L; j++)
{
if (i == 0)
{
digitalWrite(artifactPin,HIGH);
analogWrite(DAC0, sig[j]);
delayMicroseconds(10);
}
else
{
analogWrite(DAC0, sig[j]);
delayMicroseconds(10);
}
}
digitalWrite(artifactPin,LOW);
analogWrite(DAC0,0);
//15 second interstimulus period
delay(15);
}
}
Any help or advice would be hugely appreciated. Thanks!