In the setup function i clear the EEPROM if a specific button is clicked. in the loop function at the start i have this code:
if(millis() - last_sample >= 180){
sampler();
EEPROM.get(stateEEPROMAdress, stateCode);
stateCode = getState(stateCode);
EEPROM.put(stateEEPROMAdress, stateCode);
Serial.println(stateCode);
}
and a bunch of sampling code. then at the end of the loop i have:
if(millis() - last_xbee >= 900){
EEPROM.get(packetEEPROMAdress,packetCount );
EEPROM.get(stateEEPROMAdress,stateCode);
if(!initializing || (stateCode!= 0 && stateCode != 1)){
telemetry[2] = packetCount++;}
telemetry[21] =stateCode;
EEPROM.put(packetEEPROMAdress, packetCount);
.... and printing codes...
I also at the start of the sketch i have defined :
const int packetEEPROMAdress = 0;
const int stateEEPROMAdress = packetEEPROMAdress + sizeof(int);
and this is the getState function. simple state determination from sensor values:
int getState(const int stateCode=0){
int outState;
//state 0
if(stateCode == 0 &&(fabs(verticalSpeed) < 2 || fabs(relativeAltitude) < 5)&&initializing){
outState = 0;
//true should change later to see is launch botton is on
} else
if(missionReady && !initializing && stateCode == 0){
outState = 1;
}else if(verticalSpeed > 3&&stateCode == 1){
outState = 2;
}
//else
//if(VerticalSpeed < 3 && stateCode == 2){
// stateCode = 2;
// //apogee but no code in CDR
//}
else if(verticalSpeed < 2 &&stateCode == 2){
outState = 3;
} else if((fabs(relativeAltitude - 450)< 10 || relativeAltitude<440 ) &&stateCode == 3){
outState = 4;
}else
//true should be replaced with seperation photocell is bright
if(true && stateCode == 4){
outState = 5;
}else if(relativeAltitude < 3 && fabs(verticalSpeed) < .7 && stateCode == 5 ){
outState == 6;
//activate Buzzer stop telemetry
}
return outState;
}
everything a good when state is 0 . when i send a command and states becomes 1. the after a few loops that 1 appears. the number in EEPROM becomes 8663 . is there a problem in addressing the EEPROM?