Hi there,
I need to assign my calculated value to a constant value. But is it possible to make this change permanently in code script so when arduino resets this change will be kept.
Thanks in advance
Hi there,
I need to assign my calculated value to a constant value. But is it possible to make this change permanently in code script so when arduino resets this change will be kept.
Thanks in advance
Welcome to the forum
Why did you start a topic in the Uncategorised category of the forum when its description is
DO NOT CREATE TOPICS IN THIS CATEGORY
Your topic has been moved to the Programming category
As to your question, save the calculated value to EEPROM and load it again each time the Arduino is reset
I am new to the forum sorry for the mistake.
Thanks for your answer.
No problem but please be more careful in future
Good luck with your project and if you have problems saving and loading the value please continue in this topic
It depends on what you mean exactly.
If you have calculated a value before you start the compilation process, you can simply add it to your sketch as a constant, e.g.,
int const myValue {31415}; // Calculated somehow.
It is also possible to calculate quite some some constant values at compile time, but this is a rather advanced topic.
Finally, if the value needs to be calculated at run time, EEPROM can be used in many cases to store this value. This of course would not be a real constant.
Yep, a C++ compiler will calculate values of constants at compile-time whenever possible, e.g.:
const int a = 100 * 42;
const float b = cos(42);
// etc
Those will be calculated at compile time so you won't have the overhead of calculating them at run-time (you won't find any trace of the cos()
function in the compiled code, unless you use it elsewhere in your code).
I have a loadcell but in case of calibration constant needed to be updated. I will add a calibration mode on my code. When user calibrates the loadcell and different calibration factor calculated, i need to user to be able to continue with same calibration factor, if the power cut off or code is reseted by a reset buton.
Then storing the calibration value in EEPROM seems to be the way to go.
In your calibration routine, you can use EEPROM.put()
so store the calibration value, and in setup()
you can use EEPROM.get()
to retrieve this value. E.g.,
#include <EEPROM.h>
int const calibrationAddress {0}; // Where to store the calibration value.
int calibrationValue {};
void calibration() {
// ...
EEPROM.put(calibrationAddress, calibrationValue);
}
void setup() {
EEPROM.get(calibrationAddress, calibrationValue);
// ...
}
void loop() {
// ...
}
I think i got it. Thanks. That was the solution i need.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.