2

I have a problem with the Teensy, its EEPROM and another Array.

My program contains two Arrays, the first one is a 128x4 long Array, the other one a 2560x2 double Array. The first Array is meant to be written to the EEPROM once a certain time (=number of loops) is reached. The other Array is meant to be written to an SD-Card or just the terminal, can't deiced yet.

My problem is that whenever I uncomment the save()-function the first 17 data-quadruples in the EEPROM get somehow overwritten by random data. Always the first 17 with would be the first 68 numbers or 272 byte.

I have tried renameing the save()-function, but the onlything that works for now is to comment the function in the loop()-function out so that it won't be called at all. I even have enough RAM left, the Arduino IDE tells me I am using about 73% of the RAM for variables.

Could it be that be creating more variables somewhere else the Teensy overwrites the Array for the EEPROM? But than again why doesn't that happen if I don't write into the everything-Array?

Thank you in advance for any help!

Greetings,

John

PS: Please excuse my not perfect English for it's not my native language.

//Includes
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <SPI.h>
#include <String.h>
#include <EEPROM.h>

elapsedMillis looptime = 0; //time for each loop in ms
int loopnumber = 0; //number of loops
elapsedMillis overalltime = 0; //overalltime in ms
const int iterationtime = 25; //time for each loop in ms

//EEPROM
//Deklarations
int nsd = 0; //counter for saving into eepromArray
const int maxArrayrows = 128; //Max number of row in eepromArray
int iArray = 0; //Number of used rows in eepromArray
long eepromArray[128][4]; //Array for EEPROM-storage
bool writtentoEEPROM = false;
double everything[2560][2]; //Big Araay with a pair of numbers of each loop

// Some more variables
double z, zpunkt, alpha, angle;

void setup() {
 // Just inititialing stuff, nothing to do with the Arrays
}

void loop() {
  //Calculating more stuff

  ++nsd;
  if (nsd >= 20) {
    WritetoArray();
  }

  save(loopnumber);

  ++loopnumber;

  looptime = 0;
}


//********************************************************
//
// Save Data in Array. If full write into EEPROM
//
// long: 4 byte
// Storeage needed for each quadruple: 4*4 byte = 16 byte
// EEPROM-size: 2 kibyte
// Max number of quadruples: 2048 byte / 16 byte = 128
// Quadrupels per second: 2
// Max time: 128 / 2 = 64 s
//
//********************************************************
void WritetoArray() {
  int scale = 10000;
  long zArray = (long)(scale * z);
  long zpunktArray = (long)(scale * zpunkt);
  long alphaArray = (long)(scale * alpha);
  long angleArray = (long)(scale * angle);
  if (iArray < maxArrayrows) {
    eepromArray[iArray][0] = zArray;
    eepromArray[iArray][1] = zpunktArray;
    eepromArray[iArray][2] = alphaArray;
    eepromArray[iArray][3] = angleArray;
    ++iArray;
  }
  else {
    if (writtentoEEPROM == false) {
      WritetoEEPROM();
      writtentoEEPROM = true;
    }
    while(true){
      for(int n = 0; n < 2560; ++n){
        Serial.printf("%f, %f\n", everything[n][0], everything[n][1]);
      }
      Serial.println("----------");
    }
  }
  nsd = 0;
}


//********************************************************
//
// Write Data to EEPROM
//
//********************************************************
void WritetoEEPROM() {
  byte data;
  int adress = 0;
  int  i = 0;
  while (adress < 2048) {
    for (int n = 0; n <= 3; ++n) {
      for (int a = 0; a <= 3; ++a) {
        data = (eepromArray[i][n] >> ((3 - a) * 8)) & 0x000000ff;
        EEPROM.write(adress + a, data);
      }
      adress = adress + 4;
    }
    ++i;
  }
}

//********************************************************
//
// Save data-pair for later usage
//
//********************************************************
void save(int l) {
  everything[l][0] = z;
  everything[l][1] = angle;
}
1
  • Are you never resetting loopnumber to 0 once it gets greater than 2559?
    – Majenko
    Commented Jun 14, 2016 at 15:05

1 Answer 1

2

In your loop(), you increment repeatedly loopnumber without ever resetting it. You should at the very least replace

loopnumber++ ;

with

loopnumber = (loopnumber+1)%2560 ;

Otherwise, when loopnumber becomes >= 2560, your save() function will begin writing outside the 'everything' array and overwriting whatever is written in the adjacent positions of memory.

2
  • 1
    You mean loopnumber = (loopnumber+1)%2560. What you wrote is undefined behavior. Commented Jun 14, 2016 at 19:44
  • @Edgar Bonet : You seem to be right. I fixed the code accordingly. Commented Jun 14, 2016 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.