Strange Problem, varible not subtracting?

Ok, this is one of those stupid simple problems that we don’t get it. In this post is FIVE screen shots of our code and a zip file of the project in easyC along with the screen shots again.

What we are trying to do is subtract two varibles, then assign it to another varible in easyC. Sounds simple right?

In screen shot 1 you see the varible we are having problems with:

enc_Right_Difference = 10-5

Then in the terminal window, you see enc_Right_Difference = 5. This is correct. No problems here.

In screen shot 2 you see the varible enc_Right_Difference = Count minus 100. And the terminal window reports a 100 difference between the two varible. No problems here.

In screen shot 3 you see the varible enc_Right_Difference = 100 - Old_Count. And the terminal window reports a 100…uh oh problems here…But wait…Shouldn’t enc_Right_Old_Count get updated by enc_Right_Count?? Two lines down. Hmm…It’s not updating varible enc_Right_Old_Count so then now enc_Right_Difference is ALWAYS equal to 100. No good.

In screen shot 4 you see enc_Right_Old_Count is now assigned to a constant 50, and the terminal screen shows that indeed that varible is 50, BUT WAIT…just above back up two line for varible enc_Right_Difference = 100 - enc_Right_Old_Count. The controller KNOWS THAT enc_Right_Old_Count is equal to 50, but why isn’t it SUBTRACTING???

Are we losing our minds?

In screen shot 5 is our orginal code before doing all this research debugging to a terminal window…

Why won’t in screen shot 4 it subtract? It’s like that varible gets blown away.

Is it because when it returns a varible at the bottom is loses it’s orginal value? Just trying to help our kids understand it and it stumped me too…













Here is the project file ZIP incase you want to open in easyC.

mess.zip (578 KB)


mess.zip (578 KB)

I see 3 lines involved, minus comments:

enc_Right_Difference = 100 - Enc_Right_Count_Old;
enc_Right_Old_Count = 50;
Print enc_Right_Difference

So… basically this is saying

enc_Right Difference = 100 - enc_Right_Count_Old_Which_Isn’t_Set_To_Anything_Yet
Set enc_Right_Count_Old to 50
print enc_Right_Difference

I don’t really see anything that suprises me. I think you wanted to do:

Set enc_Right_Count_Old to 50
enc_Right Difference = 100 - enc_Right_Count_Old
print enc_Right_Difference (which will be 50)

You need to set your variable enc_Right_Count_Old = 50 **before **you want to use math involving it, or it will just use zero (it doesn’t know any better!). Move it to the line above the subtraction, it should work as expected.

To better visualize this, you can put print statements right before you do the math and right after the math, and you can see what the variables are set equal to. You happened to put them in places that give you (seemingly) unexpected results.

Hope this helps,

Matt

Hi Matt,

Thanks for the time in checking this out.

We don’t want to set that varible first, or before the math. It is true that the first loop through the CPU it will be ZERO. But then it gets a value (50) at the bottom on the first loop. Shouldn’t the CPU remember this value, so in loop pass #2, now that varible is set to 50 and it should correctly excute the subtract math?

We only want to update this varible at the end because we are doing a velocity measurement sampling the encoder counts.

I posted Screen 1 through 4 to show you what we are trying to do our testing, basiclly to eliminate “Have you tried this? Have you tried that?” Sort of thing…

Screen shot #5 is what we want, but we found out the problem in screen shot 3 and 4. By the varible updating but not being able to get subtracted above it.

This is a special case, because its a seperate function. Each time you call this function, everything starts from scratch, assuming you are not sending the value of enc_Right_Old_Count to this function each time (which is appears you aren’t). I notice that you are only returning the value of enc_Right_Distance, so the primary loop that is calling this function has no recollection of what might have been previously use internally with this function (read: it doesn’t know what the value of enc_Right_Old_Count might have been).

If you want to reuse the value of enc_Right_Old_Count, one way is to return this value from your function and then send it back as an arguement each time its called in your main loop.

This is where… my syntax experience with C gets foggy. If you want to return and reuse both parameters, I think you’ll actually need to return them as an array, since you can’t return more than 1 variable from a function. You’ll need to make a 2 element array, return them and if you wish for cosmetic reasons, split them up.

Some brief example code inside the function right_velocity would be:

long velocity_right(enc_Right_Old_Count)
{
long distance_and_old_count[2]; // declares this array
 
** MATH **
 
distance_and_old_count[0] = enc_Right_Difference;
distance_and_old_count[1] = enc_Right_Old_Count;
 
return distance_and_old_count;
}

The [2] specifies the size, but the array actually starts at element [0]. That’s what makes C a little different than some other languages.

In the main outter loop, you would use something like the following to send the en_Right_Old_Count value to the function:

distance_and_old_count = right_velocity(enc_Right_Old_Count);
enc_Right_Difference = distance_and_old_count[0];
enc_Right_Old_Count = distance_and_old_count[1];

Matt

Use global variables if you want variables to be persistent through many function calls.

What Matt Adams said is correct.

Here’s what’s going on. In your “right_velocity” function the variables are all implicitly declared as auto storage class.

long enc_Right_Old_Count = 0;

What this means is that the variables are allocated as the function is entered and deleted when it exits. This is the default in C to save space - temporary variables in one function can use the space of variables in another function. And in your case you initialize them to 0, so that subtraction of 100-enc_Right_Old_Count will always be 100 since enc_Right_Old_Count is initialized to zero each time the function is entered.

Fortunately the fix is easy - you need to make “enc_Right_Old_Count” not get reinitialized each time. You can do this by declaring the variable as a global in the main function. That way it will only be initialized once when the program starts and not every time the function is declared. To do this, go to the main function, and double click on the “Globals” block and add it there.

Be sure to remove it from the right_velocity funtion otherwise the local declaration will override the global for that function and you’ll still get the same results.

There might be better ways of doing this without global variables if you were hand coding in C, but this will solve the problem easily in EasyC.

Hope this helps.

Thanks again guys. I dunno how we got through last year. We must have used all globals and didn’t know it. I don’t remember this “rule” when programming in C. But now I will never forget and neither will my programmers. BTY, it works like a champ now using globals. Looks too messy to try and do with an array in EasyC, or even doing your own structures…It doesn’t look like EasyC support structures, but I did see something in the help on Arrays, we need to read up on that…

Again, thanks a bunch.

Arrays work fine in EasyC. The problem is the complier limits the program to 250 “char” slots total in the program. Int is limited to 120.