Today, while trying to debug the Example 1 from the Experimenters kit, I thought I might be having battery issues. So I wanted to see what the battery level was.
Being new to RobotC, I figured this might be a fun little mini-project (and I do mean mini) to tackle.
So, first thing I did, was start checking the functions and such in RobotC. I found a neat value you can read.
nImmediateBatteryLevel
It returns an integer of the battery level. If your battery level was 7.499, then this value returns 7499.
So I copied a line of code to display this value on the NXT, like this below:
nxtDisplayTextLine(1, "%d", nImmediateBatteryLevel);
Perfect! But wasn’t quite satisfied. I wanted to display this on the NXT with the decimal. And a bit of text so I knew what the value was.
Well, with a bit of math, you can divide a integer by a factor of 10 to get a decimal. Since I wanted 3 decimal places, I divided by 1000. In order to do the division, you need to convert (or cast) the integer to a float. So that’s where the (float) code comes in. I also found you can include plain text in the nxtDisplayTextLine() function, and it will display that.
So here is what I came up with.
nxtDisplayTextLine(1, "Battery: %.3f", (float)nImmediateBatteryLevel / 1000);
That code puts the word "Battery: 7.499" on Line 1 of the NXT screen. The 7.499 is the actual battery value. The %.3f means I want a value with 3 decimal places, of type Float.
For those of you with a C background, this is common knowledge. For those with a Basic background, type conversions are usually automatically done for you. In RobotC, you have to be explicit about it.
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment