I have a temperature and light logging program, which I want to store collected data on an eeprom. My C knowledge is minimal and I was wondering what the best way to do this is!
the data I plan to store :-
Samples UL
Temp Total UL
Light Total UL
Min Temp I
Max Temp I
Min Light I
Max Light I
This will be in 2 tables, the first will contain data for a day period (15 minute intervals currently), the second will contain a record for each day of the year.
Each record update will require the data reading and totals/max/min updating, since the EEPROM routines use bytes I was wondering what the best way to do this is?
Create a structure and use sizeof and a byte pointer to read each byte / write it, I have no problem working out where to write it (assembler experience) just not sure of the best and most efficient way to do this.
I am more concerned with the mechanics as I am not to sure with pointers, structures etc in C, and without being able to trace the program and watch variables it makes it difficult!
I have a 24LC256, with 32k memory (infact I have 5), and yes your calculations are correct! I planned this on a spreadsheet, so I should have plenty of room, (and more as I plan on capturing more statistics, once I get the base ones done)
except 366 days in a year!
The problem is I'm not sure of the best way to read bytes into the structure and write them back out to the EEprom using C, Ideally having a function :-
WriteStrucute(*structure,Address) ;
would solve all the problems!
Also although it does not say it explicitly it says the chip is good for 1,000,000 writes I assume this is per cell not total!
Ok still trying here to at least get the basis here is my code sofar, still struggling with the pointers to the log entry!
(note I am editing in Codeblocks, once I get the code working I will be using it in the IDE)
struct Log_Entry{
unsigned long SamplesTaken ;
unsigned long LightTotal ;
unsigned long TempTotal ;
int MinLight ;
int MaxLight ;
int MinTemp ;
int MaxTemp ;
};
struct Log_Entry myLog ;
int WriteStructure(Log_Entry &structure,unsigned long Address) ;
int main() {
}
int WriteStructure(Log_Entry &structure,unsigned long Address) {
}
I am getting the following errors :-
C:\Users\Daz\MyProgramming\CProgramming\TestSyntax.c|14|error: expected ';', ',' or ')' before '&' token|
C:\Users\Daz\MyProgramming\CProgramming\TestSyntax.c|22|error: expected ';', ',' or ')' before '&' token|
||=== Build finished: 2 errors, 0 warnings ===|
I have been googling structures but not much help on calling and reading bytes from them yet!
Ok, can anybody take a look a this and see if it makes sense, I think I got it it at least compiles, I just dont want to take my logger off line to test it just yet!
Also if anybody has any suggestions on cleaning the code up it would be appreciated!
#include <Wire.h>
struct {
unsigned long SamplesTaken ;
unsigned long LightTotal ;
unsigned long TempTotal ;
int MinLight ;
int MaxLight ;
int MinTemp ;
int MaxTemp ;
}
Log_Entry ;
struct Log_Entry myLog ;
void setup() {
}
void loop() {
}
// *****************************************************************************
// Read Structure from EEPROM
// *****************************************************************************
int WriteStructure(struct Log_Entry structure,unsigned int Address) {
union {
struct Log_Entry structure ;
byte bytes[sizeof(Log_Entry)] ;
}
Log ;
for (int j =0 ; j<sizeof(Log_Entry); j++) {
writeEEPROM(0x50,Address+j,Log.bytes[j]) ;
}
}
// *****************************************************************************
// Write Structure to EEPROM
// *****************************************************************************
int ReadStructure(struct Log_Entry structure,unsigned int Address) {
union {
struct Log_Entry structure ;
byte bytes[sizeof(Log_Entry)] ;
}
Log ;
for (int j =0 ; j<sizeof(Log_Entry); j++) {
Log.bytes[j] = readEEPROM(0x50,Address+j) ;
}
}
// *****************************************************************************
// Write Byte to EEPROM
// *****************************************************************************
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.send(data);
Wire.endTransmission();
delay(5);
}
// *****************************************************************************
// Read Byte from EEPROM
// *****************************************************************************
byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.receive();
return rdata;
}
Normally, the IDE creates function prototypes for you.
You can't call a function unless the compiler knows what the signature is for a function. You don't normally need to define the prototype (for the Arduino) because the IDE does that for you.
There is a bug in the IDE, though, that prevents it from creating prototypes for functions with references (the & indicates a reference). The EEPROM_writeAnything and EEPROM_readAnything functions use references.
If having them on top bothers you, put them in another file, with a .h extension, and #include that file in the sketch.