hello guys! what can i do to save first results from sensor in an array and and still show them ( the same first 5) even if the code runs again from beggining. I have an array that allready stores the resultes but when the code runes again he stores the resultes again and again. i want to save the first 5 forever so i can compare with the actual results
The EEPROM will store your values, check out the reference & playground:
http://playground.arduino.cc/Code/EEPROMWriteAnything
Note: there is no EEPROM on the Arduino DUE.
Also:
You can simply run a test sketch to gather the values then hard code them in your app, however the EEPROM will allow you to 'reset' your values in the code if you ever need that feature for say re-calibration.
thank you :). by the way. i dont wat to store it for ever. when i turn off the arduino it dosent matter. is just since the minute that i turn him on
Oh, ok.
Simply put your sample variables outside of the loop, loop function, or make the variables static-local and only initialize them:
loop(){
static int sample = get();
int wrong = get();
}
sample will only be set once, wrong will be set every loop.
Surely static variable persist their value between calls to the function in which they are declared but can be updated by subsequent calls to the function. If so, then sample will be updated on every pass through the loop() function.
A solution would be to flag the fact that the variable had been set and to check that flag before updating it again.
UKHeliBob:
Surely static variable persist their value between calls to the function in which they are declared but can be updated by subsequent calls to the function. If so, then sample will be updated on every pass through the loop() function.A solution would be to flag the fact that the variable had been set and to check that flag before updating it again.
In my example sample will not be updated every pass, it is only initialized once. If the variable is marked const then you can also guarantee it. No flag should be needed even for updating a non-const variable as being initialized suffices the 'is set' flag.
that's exactly what i was looking for static variable. im such a newbie..Thank you :).
What output would you expect from this ?
void setup()
{
Serial.begin(115200);
for (int i = 0; i < 3; i++)
{
aFunction();
}
}
void loop()
{
}
void aFunction()
{
static int test = 1;
Serial.println(test);
test++;
}
test is declared as static and initialized then updated on subsequent calls to the function. How does that differ from the solution given above that uses a static variable declared and initialised in the loop() function ?
well is it a tricky question? ![]()
1 2 3? im guessing. i just started on arduino so i really dont know for sure.
pYro_65:
In my example sample will not be updated every pass, it is only initialized once. If the variable is marked const then you can also guarantee it. No flag should be needed even for updating a non-const variable as being initialized suffices the 'is set' flag.
Sorry, but that isn't the case. A static variable is not just used once. All static means is that the variable will retain its value on subsequent calls to the function in which it's declared. It can be changed again, within the function, and that new value will be retained just like the first assignment was.
The right way to do it in this case is to use a flag (or some other means) to indicate that the variable has already been stored, and to assign to it only one time.
Can't you just assign the variables in the Setup function? (Assuming you initialized the variable as a global variable before Setup) That's kind of the point of Setup - to run stuff once on startup, before your main loop starts.
Sleepydoc:
Can't you just assign the variables in the Setup function? (Assuming you initialized the variable as a global variable before Setup) That's kind of the point of Setup - to run stuff once on startup, before your main loop starts.
Yes, but if they should be visible in loop(), they must be global.
lar3ry:
pYro_65:
In my example sample will not be updated every pass, it is only initialized once. If the variable is marked const then you can also guarantee it. No flag should be needed even for updating a non-const variable as being initialized suffices the 'is set' flag.Sorry, but that isn't the case. A static variable is not just used once. All static means is that the variable will retain its value on subsequent calls to the function in which it's declared. It can be changed again, within the function, and that new value will be retained just like the first assignment was.
The right way to do it in this case is to use a flag (or some other means) to indicate that the variable has already been stored, and to assign to it only one time.
Actually it is, I did not say static could not be modified, however I pointed out the magical keyword const that you seem to be forgetting.
And in my example, the static member does not change once initialized, which is also correct as initialization only occurs once. If you choose to assign to it later, that is your own choice.
If you want to argue such a moot point due to a failure to read my post correctly, then take a look at your own answer. Setting a flag does not prevent you from modifying the variable.
The OP stated it must be set once only, why does const not seem like the more appropriate answer?
The variable can't be const and global unless the data is available during initialization, therefore a const static-local is by far safer and more efficient than storing a flag and using a mutable variable that is never changed.
Your sample code did not specify const. If you want to argue that your sample code will not update the variable on every pass, I suggest you try it.
And in case you can't remember what you wrote in that same post...
loop(){
static int sample = get();
int wrong = get();
}
*sample* will only be set once, *wrong* will be set every loop.
Which is clearly wrong.
And yes, if the flag is used to bypass the writing of that variable, it will not be rewritten.
Please show me how, please, there is no code in that example that causes the variable to change value more than once, it is quite clear, and yes an initialization is guaranteed to run only once.
There is not really any argument here, if you want the variable to be modifiable there is still no need for a flag, I'll state once more for clarity, initialization occurs only once. It can stay in an always 'set' state, it would only be modified to reset it, which would still be in a 'set' state.
Is it not obvious, that if an object is initialized to a value, that you are purely just wasting cycles checking if it has been set. You can still modify the variable, your flag check is only worth something if you remember it.
Alternatively, a const gives you a compiler warning on modification, or you could simply not assign to it, that is the whole point of this thread isn't it?
Here take this as a proof of concept, the variable does not change every loop.
int get(){
return random( 1, 100 );
}
void setup() {
Serial.begin( 9600 );
}
void loop() {
static int sample = get();
Serial.println( sample );
delay( 500 );
}
It would appear that I owe you an apology, pYro_65.
I guess I have never used a static initializer for a variable when I wanted it to retain the initial value. I made an assumption, and I realize I should have actually tried it. Thank you for the education.
Apologies from me too. There is a subtlety going on here that I did not appreciate.
It seems that a static variable can be initialised as many times as you like in a function and will retain the first value it is given but its value can be updated subsequently and will be retained between calls to the function.
Have I got that right ?
UKHeliBob:
Apologies from me too. There is a subtlety going on here that I did not appreciate.It seems that a static variable can be initialised as many times as you like in a function and will retain the first value it is given but its value can be updated subsequently and will be retained between calls to the function.
Have I got that right ?
Not quite. A static variable in a function is initialized only once. It can be changed whenever you like and will retain its value between calls.
Another way to think about it is that it's just like a global variable, except it's only visible in the scope where it is declared.