Problem Statement:
I am not getting expected results when writing an unsigned long integer to EEPROM beginning at offset = 106.
I have checked and double-checked my code to ensure I am calling the method with the correct variable types.
All the code is listed below
Ideas for why it is happening:
- Am I “stepping” on previously written values with each successive write?
a. The answer is No
i. when I modify my sketch to only write to the first eeAddress, I get the following:
BEGIN TimeOn ******************
Method: writeTimeOn eeAddress = 106
Method: writeTimeOn newEEPROMValue = 4000
0 = 119968
END TimeOn ******************
2. Do I need to include the EEPROM library in both my personal library I created AND my sketch?
a. No, when I include EEPROM in both places, I get the same results
3. is there a mismatch in my variable types that I am using to call the method?
a. No. The function in my sketch calls the method in my library with the same variable types.
My sketch uses: rdaq.writeTimeOn(i, jUL);, where I is uint16_t, and jUL is uint32_t
My method is: uint32_t DQM::writeTimeOn(uint16_t dex, uint32_t newEEPROMValue)
Facts:
- The sketch compiles without error when the library is included
- The writeTimeOn method (" uint32_t DQM::writeTimeOn(uint16_t dex, uint32_t newEEPROMValue) " is definitely being called
a. Offset to the TimeOnX values is 106
b. The correct value for newEEPROMValue is being sent (e.g., 4000, etc)
c. the eeprom address is being incremented correctly for the desired TimeOnX value (e.g., when i=0, eeAddress is 104, when i=1, eeAddress = 110, etc…) - The main program calls the “writeTimeOn” method using “rdaq.writeTimeOn(i, jUL)”
a. in this call,
i. “i” is uint16_t
ii. “jUL” is uint32_t - The variable types in the “writeTimeOn” method match the variable types in the call “writeTimeOn”
- Something (though it is not the correct value) is definitely written to EEPROM
a. when I set jUL = 4000 and run the sketch, the output is this:
BEGIN TimeOn ******************
BEGIN TimeOn ******************
Method: writeTimeOn eeAddress = 106
Method: writeTimeOn newEEPROMValue = 4000
0 = 119968 (-- expected 4000)
Method: writeTimeOn eeAddress = 110
Method: writeTimeOn newEEPROMValue = 4001
1 = 30113 (-- expected 4001)
Method: writeTimeOn eeAddress = 114
Method: writeTimeOn newEEPROMValue = 4002
2 = 30114
Method: writeTimeOn eeAddress = 118
Method: writeTimeOn newEEPROMValue = 4003
3 = 30115
Method: writeTimeOn eeAddress = 122
Method: writeTimeOn newEEPROMValue = 4004
4 = 30116
Method: writeTimeOn eeAddress = 126
Method: writeTimeOn newEEPROMValue = 4005
5 = 30117
Method: writeTimeOn eeAddress = 130
Method: writeTimeOn newEEPROMValue = 4006
6 = 30118
Method: writeTimeOn eeAddress = 134
Method: writeTimeOn newEEPROMValue = 4007
7 = 30119
Method: writeTimeOn eeAddress = 138
Method: writeTimeOn newEEPROMValue = 4008
8 = 30120
Method: writeTimeOn eeAddress = 142
Method: writeTimeOn newEEPROMValue = 4009
9 = 30121
Method: writeTimeOn eeAddress = 146
Method: writeTimeOn newEEPROMValue = 4010
10 = 30122
Method: writeTimeOn eeAddress = 150
Method: writeTimeOn newEEPROMValue = 4011
11 = 30123
Method: writeTimeOn eeAddress = 154
Method: writeTimeOn newEEPROMValue = 4012
12 = 149932 (-- !!! expected 4012, why did this go to 149932 from 3023???)
Method: writeTimeOn eeAddress = 158
Method: writeTimeOn newEEPROMValue = 4013
13 = 149933
Method: writeTimeOn eeAddress = 162
Method: writeTimeOn newEEPROMValue = 4014
14 = 149934
Method: writeTimeOn eeAddress = 166
Method: writeTimeOn newEEPROMValue = 4015
15 = 149935
END TimeOn ******************
- This output shows
a. the new eeAddress is generated correctly
b. the value for newEEPROMValue passed to the method is as expected
c. when value of “i” =0. the method returns 119968
i. the expected value is 4000
d. for each value of 0< “i” < 12, the returned value is 3011x, where x increments by 1 each time
i. the expected value is 400x, where x increments by 1 each time (e.g., 4001, 4002, 4003, etc)
e. for each value of 12 < “i”, the returned value is 114993y, where y increments by 1 each time
i. the expected value is 400x, where x increments by 1 each time (e.g., 4024 4025, 4026, etc)
Assumptions:
- the variable types in the calling function must match the variable types in the class method (this is verified, see Facts, note 4)
- the
Theory:
- Something is causing each write to EEPROM to “step” on the preceding value (DONE, see question 1.a.i)
a. No, see question 1.a - Maybe I need to include the EEPROM libray in both my sketch and library.
- The correct offset to the first TimeOnX value is not 106
Actions:
- Find the answer to question 1 by modifying my test program to write only to the first eeAddress? (DONE, see answer to Question 1.a.i
My sketch:
#include <DQM.h>
#define RD 1
#define WR 0
//#include <EEPROM.h>
DQM rdaq;
void printInt(uint8_t, uint16_t);
void printUL(uint8_t, uint32_t);
uint16_t theIntValue;
uint16_t jInt =300;
uint32_t theULValue;
uint32_t jUL =4000;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(F("BEGIN TimeOn ******************"));
for (uint16_t i=0; i<16; i++) {
rdaq.writeTimeOn(i, jUL);
theULValue = rdaq.getTimeOn(i);
printUL(i, theULValue);
jUL++;
}
Serial.println(F("END TimeOn ******************"));
/*
Serial.println(F("BEGIN Analog offsets ******************"));
for (uint16_t i=0; i<16; i++) {
theIntValue = rdaq.getAnalogOffset(i);
printInt( i,theIntValue);
//Serial.println(jInt);
rdaq.writeAnalogOffset(i,jInt);
jInt++;
theIntValue = rdaq.getAnalogOffset(i);
printInt(i, theIntValue);
}
Serial.println(F("END Analog offsets ******************"));
*/
while (true) {
delay(500);
}
}
void printInt(uint16_t i, uint16_t theValue) {
Serial.print (i);
Serial.print (" = ");
Serial.println(theValue);
}
void printUL(uint16_t i, uint32_t theValue) {
Serial.print (i);
Serial.print (" = ");
Serial.println(theValue);
}
My Library:
.h
/*
DQM.h
RDAQ library for using EEPROM
*/
#ifndef DQM _h
#define DQM _h
#include "EEPROM.h"
#include "Arduino.h"
class DQM
{
public:
DQM();
void timeOn();
uint16_t getAnalogOffset(uint16_t );
uint16_t writeAnalogOffset(uint16_t , uint16_t );
uint32_t getTimeOn(uint16_t );
uint32_t writeTimeOn(uint16_t , uint32_t );
private:
uint16_t _offset; // 21;
uint32_t _timeon; // 106;
};
#endif
.cpp
/*
DQM.cpp - Library for flashing DQM code.
Created by Chuck Elliott Nov. 5, 2018
*/
#include "Arduino.h"
#include "DQM.h"
DQM::DQM()
{
_offset = 21; //int16_t
_timeon = 106; //uint32_t
}
void DQM::timeOn()
{
delay(1000);
}
uint16_t DQM::getAnalogOffset(uint16_t dex) {
uint16_t eeAddress = _offset + (dex * 2);
uint16_t valueAtOffset = 0;
return EEPROM.get( eeAddress, valueAtOffset );
//return valueAtOffset;
}
uint16_t DQM::writeAnalogOffset(uint16_t dex, uint16_t newEEPROMValue) {
uint16_t eeAddress = _offset + (dex * 2);
EEPROM.update(eeAddress, newEEPROMValue);
delay (500);
uint16_t value = 0;
return EEPROM.get(eeAddress, value);
}
uint32_t DQM::getTimeOn(uint16_t dex) {
dex = _timeon + (dex * 4);
uint32_t valueAtOffset;
return EEPROM.get( dex, valueAtOffset );
//return valueAtOffset;
}
uint32_t DQM::writeTimeOn(uint16_t dex, uint32_t newEEPROMValue) {
uint16_t eeAddress = _timeon + (dex * 4);
uint32_t valueAtOffset = 0;
Serial.print("Method: writeTimeOn eeAddress = ");
Serial.println(eeAddress);
Serial.print("Method: writeTimeOn newEEPROMValue = ");
Serial.println(newEEPROMValue);
EEPROM.update( eeAddress, newEEPROMValue );
delay (500);
uint32_t value = 0;
return EEPROM.get(eeAddress, value);
}
keyword.txt
DQM KEYWORD1
getAnalogOffset KEYWORD2
writeAnalogOffset KEYWORD2
getTimeOn KEYWORD2
writeTimeOn KEYWORD2
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.