Hello! I am having some issues with pulling my data from the EEPROM correctly. I used a lot of existing forum posts to format the script I currently have but am still having some issues. I have a pump connected to a flow meter and am writing to the EEPROM the value of A0 and using that value to calculate voltage and flow rate (theFlow). The data prints fine through serial.print so something is happening between writing to the struct and pulling it through EEPROM. I have also tried pulling one variable off the eeprom and ot similar data, so I thin my issues lies in how I pull data rather than how I am writing it. Flow should be between 0-4095, voltage should be between 0-3.3V, and theFlow should be between 0-750 ccm (but probably around 200ccm). I am not sure if there is something wrong with the order I have the script it or if I am missing something pertinent to getting the correct output. In addition, it is difficult to tell if I am assigning the values made in the loop to the struct given the above issues, but am I assigning those values correctly?
Here is my code:
#include "Particle.h";
void clearEEPROM() {
for(int addr = 0; addr < 256; addr++) {
EEPROM.write(addr, 0);
}
}
SYSTEM_MODE(SEMI_AUTOMATIC);
size_t eeprom_size = EEPROM.length();
int pump = D0;
float flow = A0;
typedef struct {
float flow;
double voltage;
double theFlow;
} myObject;
void setup(){
Serial.begin(9600);
pinMode(pump, OUTPUT);
pinMode(flow, OUTPUT);
digitalWrite(pump, LOW);
delay(5000);
}
void loop() {
float flow = (float)analogRead(A0);
double voltage = (3.3*flow)/4095.0;
//below is order of 4
double theFlow = ((750*((voltage/3.3)-0.5))/0.4);
if (millis() <= 155000) {
digitalWrite(pump, HIGH);
}
else {
digitalWrite(pump, LOW);
}
for (int i = 0; i < eeprom_size; i++) {
myObject myObject;
EEPROM.get(i, myObject);
Serial.printlnf("i=%f, flow=%f voltage=%f theFlow=%f, sizeof(myObject)=%f",i, myObject.flow, myObject.voltage, myObject.theFlow, sizeof(myObject));
myObject.flow = flow;
myObject.voltage = voltage;
myObject.theFlow = theFlow;
EEPROM.put(i, myObject);
i += sizeof(myObject);
delay(2000);
Serial.println(" ");
}
}
Current data output:
i=0.000000, flow=23798114539864662754282872190323424436536735918671977172300818666169887935376021120766577815793566582932747423712689577171363140584772072552527651183525888.000000 voltage=-0.000000 theFlow=0.000000, sizeof(myObject)=1.745495
i=0.000000, flow=0.000000 voltage=0.000000 theFlow=0.000000, sizeof(myObject)=1.745495
i=0.000000, flow=23798115526964450517639850316289754158628405690900644617569708015680353919613529880060723516329585171216242998759146677823189386347834751461545304750292992.000000 voltage=0.000000 theFlow=0.000000, sizeof(myObject)=1.745495
i=-158456325028528675187087900672.000000, flow=1.587445 voltage=0.000000 theFlow=0.000000, sizeof(myObject)=1.745495
i=0.000000, flow=23798114539864763976750972493721429897567517633735449397811273424832234976827119813316397640041960681603234025281883831203172115637775808315288858574979072.000000 voltage=0.000000 theFlow=0.000000, sizeof(myObject)=1.745495
i=0.000000, flow=0.000000 voltage=0.000000 theFlow=0.000000, sizeof(myObject)=1.745495
i=0.000000, flow=0.000000 voltage=0.000000 theFlow=0.000000, sizeof(myObject)=1.745495
Any help would be greatly appreciated! Thank you!