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.
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);
}
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 leastsizeof(calData)
higher. So if the size ofcalData
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\$