1
\$\begingroup\$

I just need a small help to completed my project. As my output is printing 2 result but only 1 output is saving to the eeprom. How can I save multiple data into eeprom? I'm just showing the setup and loop only cause the rest is not that important.

1

2

3

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  Serial.println("Hit 'y' and RETURN, if you want to calibrate...");
  Serial.println("Hit 's' and RETURN, if you want to collect data...");
  Serial.println("Hit 'x' and RETURN, if you want to clear the cal data in EEPROM...");
  Serial.println("If not, 'n' and RETURN");
  Serial.println(' ');
  Serial.println("\n");
  keyResponse=readkey();
  //keyResponse='n';
  if (keyResponse=='y'){
      calibration();
  }
   if (keyResponse=='s'){
      Serial.println("Collecting data...................");
  }
  else if (keyResponse=='x'){ 
    while (!eeprom_is_ready()); // Wait for EEPROM to be ready
    cli();
    calData.written=0;
    eeprom_write_block((const void*)&calData, (void*)0, sizeof(calData));
    Serial.println("Cal data is cleared...");
    sei();    
  }
  else {
     while (!eeprom_is_ready()); // Wait for EEPROM to be ready
     cli();
     eeprom_read_block((void*)&calData, (void*)0, sizeof(calData));
     sei();
     if (calData.written==99){
       Serial.println("Using previously stored cal data from EEPROM");
       output[0]=calData.dx; output[1]=calData.dy; output[2]=calData.dz;
       output[3]=calData.dv; output[4]=calData.dd;
     }
     else {
        Serial.println("no previous cal data, using default");
     }
    data();
    exit(0);
  }
  readAcc(ValueX,ValueY,ValueZ,'x',10);
  gXA = mapAcc(ValueX, midX, sensX); //cannot let gXA at 0, will create err.
  gYA = mapAcc(ValueY, midY, sensY);
  gZA = mapAcc(ValueZ, midZ, sensZ); 
  GNA = norm(gXA,gYA,gZA); 
  preTime=micros();
}
void loop() {

int i=0;
  while(loopI<=2){
 // read the both analog in values:
            readAcc(ValueX,ValueY,ValueZ,'x',10);
curTime=micros();
dT=curTime-preTime;
preTime=curTime;
aT=aT+dT;
gX = mapAcc(ValueX, midX, sensX);
gY = mapAcc(ValueY, midY, sensY);
gZ = mapAcc(ValueZ, midZ, sensZ);

LPF(gX,gXP,gXA,gXins);
LPF(gY,gYP,gYA,gYins);
LPF(gZ,gZP,gZA,gZins);
gNins=norm(gXins,gYins,gZins);
cap(gNins,0.01,1);

gXinsA=(gXinsA+gXins)/2;
gYinsA=(gYinsA+gYins)/2;
gZinsA=(gZinsA+gZins)/2;
gNinsA=(gNinsA+gNins)/2;

GN = norm(gX,gY,gZ);
LPF(GN,GNP,GNA,GNins);
cap(GNins,GNlowB,GNhighB);
GNinsA=(GNinsA+GNins)/2;

Max(gNinsMax,gNins);
Max(GNinsMax,GNins);

reset_vel(gNins, gNlowB, vel);
reset_vel(GNins, GNlowB, VEL);

vel=vel+9.81e-6*gNins*dT;
VEL=VEL+9.81e-6*GNins*dT;
act_dist=act_dist+9.81e-6*vel*dT;
act_DIST=act_DIST+9.81e-6*VEL*dT;

actCnt=actCnt+1;

if (actCnt>=10){
actGN=sqrt(actGN_acc/10);
actGN_acc=0;
actgN=sqrt(actgN_acc/10);
actgN_acc=0;

dist=dist+act_dist;  act_dist=0;
DIST=DIST+act_DIST;  act_DIST=0;
actCnt=0;
}
actgN_acc=actgN_acc+(gNins*gNins);
actGN_acc=actGN_acc+(GNins*GNins);

dispCnt=dispCnt+1;
if (dispCnt>=40){
display_result(0);
loopI++;

dispCnt=0;
}
gXP=gX; gYP=gY; gZP=gZ;  GNP=GN;
  }         
output[0] = gXins;
output[1] = gYins;
output[2] = gZins;
output[3] = VEL;
output[4] = DIST;
  Serial.println("Cal data written to EEPROM..."); // store to EEPROM   
  calData=(calData_t){99 , 0,0,0,0,0,0,output[0],output[1],output[2],output[3],output[4]};
  while (!eeprom_is_ready()); // Wait for EEPROM to be ready
  eeprom_write_block((const void*)&calData, (void*)0, sizeof(calData));

 if(loopI>2)
 {   
   exit(0);
 }
}

Updated:

  else {
     while (!eeprom_is_ready()); // Wait for EEPROM to be ready
     cli();
     eeprom_read_block((void*)&calData, (void*)(i* sizeof(calData)),sizeof(calData));
     sei();
     if (calData.written==99){
       Serial.println("Using previously stored cal data from EEPROM");
       output[0]=calData.dx; output[1]=calData.dy; output[2]=calData.dz;
       output[3]=calData.dv; output[4]=calData.dd;
     }
     else {
        Serial.println("no previous cal data, using default");
     }
    data();
    exit(0);
  }
\$\endgroup\$
2
  • \$\begingroup\$ I'm not clear of the problem. You save calData to the eeprom starting from address 0 , if you want to store another copy you can save it to another address that is at least sizeof(calData) higher. So if the size of calData is 20 bytes then the first copy will occupy eeprom address 0-19 and you can write the second copy to eeprom address 20-39 \$\endgroup\$
    – alexan_e
    Commented Aug 4, 2014 at 9:36
  • \$\begingroup\$ can u show me some example to store first copy and second copy? how about if i loop 10 times, will i able to store ten copy into EEPROM? \$\endgroup\$
    – Fufu Alex
    Commented Aug 4, 2014 at 9:47

1 Answer 1

1
\$\begingroup\$

The method I'm suggesting is something like

for (uint8_t i = 0; i < 10; i++)
{
        eeprom_write_block(&calData, i*sizeof(calData), sizeof(calData));

}

As an example, if the sizeof(calData) is 10bytes then the 10 copies will be written to the following eeprom address:

0*10 = 0 
1*10 = 10 
2*10 = 20 
3*10 = 30 
4*10 = 40 
5*10 = 50 
6*10 = 60
7*10 = 70
8*10 = 80
9*10 = 90  
\$\endgroup\$
9
  • \$\begingroup\$ I have one question. how to i add this in my code. eeprom_busy_wait() someone suggest me to use this instead of loop. \$\endgroup\$
    – Fufu Alex
    Commented Aug 4, 2014 at 13:42
  • \$\begingroup\$ @FufuAlex You can add a variable i that increments in each write (i++) and when it reaches 10 you can set it to 0 again. The eeprom_busy_wait() is not needed, refer to this post \$\endgroup\$
    – alexan_e
    Commented Aug 4, 2014 at 14:00
  • \$\begingroup\$ i add this method for(uint8_t i = 0; i < 2; i++){ eeprom_write_block((const void*)&calData, (void*)0+(i* sizeof(calData)),sizeof(calData)); but i got an error EEPROM.ino: In function 'void loop()': EEPROM:488: error: pointer of type 'void *' used in arithmetic \$\endgroup\$
    – Fufu Alex
    Commented Aug 5, 2014 at 3:40
  • \$\begingroup\$ @FufuAlex use eeprom_write_block((const void*)&calData, (void*)(i* sizeof(calData)),sizeof(calData)) \$\endgroup\$
    – alexan_e
    Commented Aug 5, 2014 at 8:38
  • \$\begingroup\$ Thanks. no more error but i cant display my previous result. do i need to make some changing in my void setup()? \$\endgroup\$
    – Fufu Alex
    Commented Aug 5, 2014 at 13:04

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.