Hey Guys,
So I am working on a project that takes a single point running-array-discrete-fourier-transform of an incoming signal and I am having trouble with one of the statements in my code. Specifically, when the new value in demodulatedSamples[i] is calculated (see the if-block in ‘loop’), the photon enters WIFI.off mode even though I have no code that would cause this directly. In WIFI.off mode, the code also stops executing, which suggests some strange exception somewhere. Here is my code:
#include <math.h>
// declare constants
const double pi = 3.1416;
const int divisions = 2048;
const int resolution = 4096-1;
const double freqeuency = 5000;
// declare arrays
double demodulatedSamples[divisions] = {0};
double cosTable[divisions];
int dacTable[divisions];
int samples[divisions] = {0};
//declare increments;
int i = 0;
//declare temporary storage
double val = 0;
double calc = 0;
int sample = 0;
void setup() {
//Start up
Serial.begin(9600);
pinMode(DAC1, OUTPUT);
//Define lookup tables
for (int x = 0; x < divisions; x++) {
val = 2*pi*x/(divisions-1);
cosTable[x] = (2/divisions)*cos(freqeuency*val);
calc = resolution*(0.5*cos(val)+0.5);
dacTable[x] = floor(calc);
}
}
void loop() {
if (i >= divisions) i = 0;
analogWrite(DAC1, dacTable[i]);
//Serial.printlnf("testing %d", dacTable[i]);
sample = analogRead(A0);
calc = cosTable[i]*(sample - samples[i])/resolution;
if (i > 0) {
demodulatedSamples[i] = calc + demodulatedSamples[i-1];
} else {
demodulatedSamples[i] = calc + demodulatedSamples[divisions-1];
}
samples[i] = sample;
i++;
}
As I said before, I narrowed the issue down to the two statements in the ‘if’-block in the ‘loop’ section of the code.My thoughts is that this is some kind of strange overflow. (although the arrays are pre-allocated, so I don’t know how that could be) I also don’t seem to get a ‘heap out of memory’ error (at least not right of the bat). Needless to say, this one has got me stumped. Any ideas how I could get around this?
Thanks in advance.
PS. My firmware is updated to version 1.1.1 and the device is still pretty new.