I have been trying to save data from an altimeter to an EEPROM over I2C. While my code runs without any errors in the IDE, I have been unable to save the data to the EEPROM. I believe the problem may originate from my attempts to store float values as bytes. I first convert them to integers, and then to bytes, but my understanding is that one integer takes two bytes to store correctly. Any insight into how to correctly store and transmit these values would be appreciated. The links to both parts on Sparkfun can be found below.
Altimeter: https://www.sparkfun.com/products/11084
EEPROM: I2C EEPROM - 256k Bit (24LC256) - COM-00525 - SparkFun Electronics
Here is the complete code I am using to write the data to the EEPROM:
#include <Wire.h>
#include "SparkFunMPL3115A2.h"
MPL3115A2 altimeter;
float startAlt;
float alts [5] = {};
byte altitudeData[180] = {};
int second = 0;
float sum = 0;
float timecheck;
#define EEPROM_ADR 0x64
const int maxEepromWrite = 16;
long currentSpot = 0;
byte tempStore[maxEepromWrite];
byte counter = 0;
void writeEEPROM (long address) {
Wire.beginTransmission(EEPROM_ADR);
Wire.write((int)(address >> 8));
Wire.write((int)(address & 0xFF));
for (byte x = 0; x < maxEepromWrite; x++)
Wire.write(tempStore[x]);
Wire.endTransmission();
}
void setup() {
Serial.begin(9600);
Wire.begin();
altimeter.begin();
altimeter.setModeAltimeter();
altimeter.setOversampleRate(7);
altimeter.enableEventFlags();
startAlt = altimeter.readAltitudeFt();
startAlt = startAlt - 5;
long startTime = millis();
while (1) {
float altitude = altimeter.readAltitudeFt();
sum = sum - alts[5];
alts[5] = alts[4];
alts[4] = alts[3];
alts[3] = alts[2];
alts[2] = alts[1];
alts[1] = altitude;
for (int i = 1; i <= 5; i++) {
sum = sum + alts[i];
}
float altAvg = sum / 5;
float above = altAvg - startAlt;
long currentTime = millis();
long elapsed = currentTime - startTime;
if (second == 180) {
altimeter.setModeStandby();
break;
}
if ((elapsed % 1000) <= 10 and (elapsed % 1000) >= 0) {
int altitudeInt = (int)altitude;
altitudeData[second] = (byte)altitudeInt;
Serial.print("Saving data for second ");
Serial.println(second);
second += 1;
}
}
int arrpos = 0;
for(int a = 0; a <= 180; a++) {
tempStore[arrpos] = altitudeData[a];
if(arrpos == maxEepromWrite) {
writeEEPROM(currentSpot);
currentSpot += maxEepromWrite;
arrpos = 0;
}
}
Serial.println("Write complete");
}
void loop() {
}
Any help you can offer would be greatly appreciated.