Regarding the picture above,
I’ve change it according to @ScruffR suggestion.
As we can see in the picture EEPROM.get only get the first summation right. But then, the summation process got a negative sign number.
I guess, this is because the address. Means that, the space is not enough.
Maybe…
Do you have any suggestion and opinion regarding this matter?
I have only outlined what needs to be done. Can you show the entire code after you applied the principles illustrated above to the rest of your code too?
#define LM35 A1
#define READSAMPLES 100
// this constant won't change:
const int buttonMeasure = D5; // the pin that the pushbutton is attached to
const int buttonReset = D6; // the pin that the pushbutton is attached to
const int ledPin = D7; // the pin that the LED is attached to
// Variables will change:
int BStateMeasure = 0; // variable for reading the pushbutton status
int BStateReset = 0; // variable for reading the pushbutton status
float bt, sum, t;
int sample[READSAMPLES];
int EEPROM_START;
int EEPROM_END;
int address;
int count;
void setup() {
// initialize the button pin as a input:
pinMode(buttonMeasure, INPUT);
pinMode(buttonReset, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
char msgMeasure[64];
char msgReset[64];
// read the pushbutton input pin:
BStateMeasure = digitalRead(buttonMeasure);
BStateReset = digitalRead(buttonReset);
EEPROM_START = 0;
EEPROM_END = count;
//Particle.publish("EEPROM_ENDe: ", String::format("{\"number =\": %d}", count));
if (BStateMeasure == HIGH) {
t = writeTemp();
EEPROM.put(address, t); //write value to current address counter address
count++;
snprintf(msgMeasure, sizeof(msgMeasure)
, "{\"temp\":%0.2f"
",\"address\":%d"
",\"Contcount\":%d"
"}"
, t
, address
, count);
Particle.publish("ON1", msgMeasure, PRIVATE);
address += sizeof(t);
delay(1000);
//Particle.publish("Button press count:", String::format("{\"count =\": %d}", buttonPushCounter));
}
else if (BStateReset == HIGH) {
float mean;
for (int i = EEPROM_START; i < EEPROM_END; i++)
{
sum = sum + EEPROM.get(i, t);
Particle.publish("Summation:", String::format("{\"sum =\": %d}", sum));
delay(2000);
}
mean = sum / count;
snprintf(msgReset, sizeof(msgReset)
, "{\"sum\":%0.2f"
",\"mean\":%0.2f"
"}"
, sum
, mean);
Particle.publish("ON2", msgReset, PRIVATE);
//Particle.publish("ON2", msg, PRIVATE);
delay(1000);
//Particle.publish("Button press count:", String::format("{\"count =\": %d}", ButtonPushCounter));
}
delay(2000);
}
float writeTemp()
{
int j;
float average = 0;
//Particle.publish("Data count entering: ", String::format("{\"Data No. =\": %d}", buttonPushCounter));
for (j = 0; j < READSAMPLES; j++)
{
average += analogRead(LM35); //read sensor value
delay(50);
}
average /= READSAMPLES;
float mv = ( average / 4096.0) * 3300;
float cel = ( mv / 10 );
/*if(address == EEPROM.length()) //check if address counter has reached the end of EEPROM
{
address = 0; //if yes: reset address counter
}*/
return cel;
}
Below is the other picture of the result, just now…
Above picture shows that, no change occur after I corrected the code.
for (int i = EEPROM_START; i < EEPROM_END; i ++)
{
sum = sum + EEPROM.get(i + 4, t);
Particle.publish("Summation:", String::format("{\"sum =\": %d}", sum));
delay(2000);
}
if,
i = 0, address = 4
i = 1, address = 5
Therefore, above is incorrect.
Let the sum as below:
for (int i = EEPROM_START; i < EEPROM_END; i + 4)
{
sum = sum + EEPROM.get(i, t);
Particle.publish("Summation:", String::format("{\"sum =\": %d}", sum));
delay(2000);
}
if,
i = 0, address = 0
i = 4, address = 4
i = 8, address = 8
Seems correct. However, still not solve.
I guess you need to step back a bit and get a feeling for what you are doing.
You had this
Where the idea was right, but the factor was wrong.
Then you got
Where the "factor" was right, but the operation wrong.
Hence this was wrong too (as you correctly noticed)
But now you have introduced a new issue by doing this
The width of your steps ("factor") is correct now, but how many steps are you making now? (and you are never really changingi - the console log should give you a clue what I mean with that).
To me it looks you are just trying out combinations without first contemplating what all the effects of a given change would be.
The above picture shows that the result of sum and mean is correct by using the code improved by @ScruffR. But I’m want to know why the operation of sum has negative sign.