Photon WIFI disabled after assigning a value to an array?

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.

@mindless, I'm not sure about out-of-bounds writing but looking at your arrays, you have over 49KB allocate to those. Each double takes 8 bytes, and each int takes 4 bytes. This may explain the heap out of memory issue.

Also, when using DAC1 with any DeviceOS after 0.4.9, you should not have pinMode(DAC1, OUTPUT);

NOTE: Device OS version 0.4.6 and 0.4.7 only - not applicable to versions from 0.4.9 onwards: While for PWM pins one single call to pinMode(pin, OUTPUT); sets the pin mode for multiple analogWrite(pin, value); calls, for DAC pins you need to set pinMode(DAC, OUTPUT); each time you want to perform an analogWrite().

2 Likes

I would not expect a heap out of memory error since your arrays are statically declared and are not on the heap. You didn’t say at what index into the array the failure occurs (value of I) but I would agree that the size of your arrays are probably involved. That’s where you should direct your attention

1 Like

You can try addding SYSTEM_THREAD(ENABLED) at the top of your code.

However, claiming a huge amount of RAM for static variables leaves little room for the heap and AFAICT the system will need some minimum heap space in case the connection is lost and needs to be reestablished.

If you can afford to use float and int16_t instead of double and int you'll be winning back 50% of the RAM your arrays currently claim.

1 Like