Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Allgorythm to store and lock a variable value

oversprayoverspray Member Posts: 14 ✭✭

Hi, I need an allgorythm to lock a variable value, so it remains constant through out the program once it runs.
I cant be a constant though, because it changes from 0 to the value obtained from a function that reads a sensor value. The first read value I want to store, but the following should be read on a different variable. So that I can compare the initial value to the present value commi9ng from the sensor. I thought about the following, but this routine doesnt give the desired rtesumlt since variable i is reset to 0 every time:

int i;
while (i<2)
{ InitAvg=Avg; // IntAvg should be locked, in a way that this condition exist only one time, after i reads 2 the while condition
i=2; // should stay wrong. Unfortunately i will reset to 0, when the program runs over it the next time.
}
Can someone give me an idea how to solve this?
Thanks a lot,
John.

Comments

  • SixtyHzSixtyHz USMember Posts: 13

    Your code looks strange, for one i is never set to a value before you check it. I'll assume you meant "int i = 0;"

    I'm also not clear on your question, do you want to save a value on your harddrive? or are you saying every time you run the function you want the value to not be reset?

    If you are trying to save a value to the harddrive, look up fopen etc...
    If you are trying to keep a value from changing when you call the function you can make that value a static "static int i = 0" this will not reset the value when you return to the function.

  • oversprayoverspray Member Posts: 14 ✭✭

    Hi, thanks for answering!

    I'll assume you meant "int i = 0; <

    Yes that is right, the book says that when registering a variable this way the compiler autmaticaly will set its value to 0. Actually I checked this, it does this right.
    I am programming an µc, there I want to stor the value in the Ram. Since its a constant value I guess it would be better to store it in the Rom area but registering it as a constant, only the developer can set its value, but in my case I want the sensor to determine it`s value. Only the very first reading should be taken, and stored.
    This value I need to later compare to the momentary values the sensor gives, to calculate the difference between starting pressure and the momentary one.

    Ill try this static registration, thanks, but I doubt itll solve my problem.
    I think I´ll have to find a way to call this function to deliver the reading only a single time, and don`t call it any more afterwards.
    Thanks,
    John

Sign In or Register to comment.