harry11733
Member
I would like to make brief pulses of white noise. I can do this by directly writing random numbers to the DAC using a timer. This approach works fine but it is limited in the range of volumes it can generate so I tried using the audio shield. The audio shield gives the required range of volumes that I need but introduced a new problem with the timing.
The code listing should put out a 10 ms pulse of white noise. I checked this on the scope and the duration of the pulse is quite variable from trial to trial, by relatively large amounts +/- 2ms. I am probably doing this wrong but not sure how to fix problem. Since the calls to the audio objects are made from within the timer's callback function it is not a problem of priority, as far as I can tell.
The code listing should put out a 10 ms pulse of white noise. I checked this on the scope and the duration of the pulse is quite variable from trial to trial, by relatively large amounts +/- 2ms. I am probably doing this wrong but not sure how to fix problem. Since the calls to the audio objects are made from within the timer's callback function it is not a problem of priority, as far as I can tell.
Code:
#include <Audio.h>
#include "IntervalTimer.h"
// objects
IntervalTimer timer1;
AudioSynthNoiseWhite whitenoise;
AudioOutputI2S i2s1;
AudioConnection patchCord1(whitenoise, 0, i2s1, 1);
AudioConnection patchCord2(whitenoise, 0, i2s1, 0);
AudioControlSGTL5000 sgtl5000;
// constants
const int period1 = 50; // us 20kHz
const int baudrate = 115200;
// trial parameters
const unsigned long pulse2Start = 200000; // 200 ms, start of second pulse is fixed
float Background_Vol = 0.0001; // background volume, Scale 0.0001 - 1.0
float Amp_Vol = 0.8; // 0.8 corresponds to the maximum undistorted output for full scale signal, higher values noticeably increase background noise
float Volume1 = 0.5; // volume of first pulse
unsigned long Duration1 = 10000; // duration of first pulse
unsigned long start = 0; // time at start of trial
bool Background_on = true;
void setup() {
// digital pin
pinMode(ledPin, OUTPUT);
// Audio board setup
AudioMemory(10);
whitenoise.amplitude(Background_Vol);
sgtl5000.enable();
sgtl5000.volume(Amp_Vol);
timer1.begin(timer1_callback, period1);
Serial.begin(baudrate);
}
void loop() {
while ( Serial.available() > 0 ) {
String inString = Serial.readStringUntil( '\n' );
// parse the string for a command, use 'startsWith' to ignore newline character
if ( inString.startsWith( "ASR" ) ) {
run_Trial();
} else {
// ignore unknown commands
}
}
} // main loop
void run_Trial (void) {
start = micros(); // determines the start time of the trace, needed to time white noise output
Serial.flush();
} // run_Trial
void timer1_callback(void) {
unsigned long timeNow = micros() - start; //time at start of function
// timing of pulse
if ( (timeNow >= pulse2Start) && (timeNow < (pulse2Start + Duration1)) ){
if (Background_on == true) {
whitenoise.amplitude(Volume1);
Background_on = false;
}
}
else if (Background_on == false) {
whitenoise.amplitude(Background_Vol);
Background_on = true;
}
} // timer1_callback