OK, assuming you're dividing the voltage by 3 to make it a maximum of 5V, you can do something like the following. You don't really need floating point functions like floor and ceil, and should avoid floating point if it's not too hard to think about. You can just do it with integers. Modulus (% operator) returns the remainder after division, so a (x % 10) = the bottom digit of x.
Use the map() function to map your analogRead value into a good range. Since it looks like you want 2 digits of precision, and 15V is your max, then you'll want the normal analogRead value (which goes from 0...1023) mapped to 0...1500. That means 0 = 0V and 1500 = 15.00V.
Try something like this:
char voltageString[6];
// Do these two first since they don't change:
voltageString[5] = 0; // Null termination
voltageString[2] = ','; // Decimal separator
int value = analogRead(5);
// Make the value be in between 0 and 1500
int mappedValue = map( value, 0, 1023, 0, 1500 );
// Working backwards, take the bottom digit and shift
voltageString[4] = mappedValue % 10; // Take the bottom digit
mappedValue /= 10; // Shift by one digit (this is a short way to write mappedValue = mappedValue / 10;)
voltageString[3] = mappedValue % 10; // Take the bottom digit
mappedValue /= 10; // Shift by one digit
// Decimal was already written above, skip voltageString[2]
voltageString[1] = mappedValue % 10; // Take the bottom digit
mappedValue /= 10; // Shift by one digit
voltageString[0] = mappedValue % 10; // Take the bottom digit
thanks, oh yes, i never used map commad before. Think this will be the solution for the current function, low voltage at 0 amp is 2,5 volt and 1 amp 2,67volt.
I found that using the integer 'whole' and modulus %, this code seams to do the job:
Here comes a piece of code for FLOOR function. CEILING function can also be derived by a little tweak in the code. The variable you want to find its floor value is defined is "variable".
///////////////////FLOOR function//////////////////////
int temp_counter = 0;
while (temp_counter+1<=variable)
{
temp_counter++;
if (temp_counter+1>=variable)
{
variable=temp_counter;
}
Thanks for reply. The second tip is cool, and basically I tried it before but in another way, didn;t work.
But how did you come up with the first one? All my Arduino programming knowledge is coming from the Arduino Programming Book, which I'm sure this one is not in it!! IS there another advanced book or so out there on Arduino Programming> Where did you get the command from??
That's not true, the second has no cast in it. It uses the normal integer initialisation, but explicitly. While your (PaulS) conversion uses the operator=() function of the int class, mine just uses the constructor and should herefore be slightly faster (because your way first creates and int and then assigns a new value). Very likely this will be optimised away by the compiler and not matter, but if you mean for something to happen, you should program it that way and not count on the compiler to fix things.
Furthermore I believe compiler warnings should be paid very careful attention to, but that's just my conviction.
In reply to Shanejet3, the first one with the usage of static_cast() is the standard C++ cast from one type to another. It replaces the C typecasting. If you want further information, you can find it here, section 3.5. It has nothing to do with Arduino programminging per se, it's standard C++.
The Arduino hardware is not able to compile anything.
Most C and C++ functions can be used in Arduino code. There are, obviously, some restrictions. fopen(), fclose(), seekg(). tellg(), etc. rely on a file system that the Arduino does not support.
The sprintf() implementation does not handle floating point numbers.
Do you know how the int "function" is implemented?
Implementation of the int contructor is not in the C++ specification and does not really matter either. All this code does is request the compiler to create an anonymous variable of type int from the float. It will never represent a typecast because it does not ask the compiller to reinterpret the memory block of the float; it uses its value (type + bit information) to intialise an integer, whereas a cast would ignore type and just take the floats memory as-is. There is a fundamental difference. If you're interested, you could also read the link I gave Shanejet3.
What is Arduino hardware able to compile? I mean how far can I go on with programming standard C/C++ ??
First off a little correction: the Arduino boards do not actually compile the software, they just run the machine code the compiler produces. As for how far you can go, I believe some things are not included in the Arduino compiler (such as the operators new and delete, most of the STL etc), but this does not mean one could not write classes with similar functionalty. The biggest limitation for code as I see it, is the space and memory constrainst inherent to microchip programming.
I'm not that familliar with the Arduino compiler though, maybe someone else can be more exact.
Fundamental types, like 'int' do not have constructors in C++.
I'm afraid that you are also mistaken about how casts work in C and C++.
If you cast a float to an int, for example:
int i;
float f;
f = 10.0;
i = (int)f;
then the bit representation of the float does NOT get transferred to the int. The whole (i.e., non-fractional) portion of the floating point number is converted to an integer and the results assigned to 'i'. In fact, as Paul S pointed out, assignment from a float to an integer has an implicit type conversion, so the cast serves only to document that the programmer intends for a conversion to take place, and to quiet compiler warnings about the possible loss of precision.
Whether or not to use casts in a case where there is implicit conversion is strictly a matter of programming style, since the generated code will be exactly the same in either case. Personally, I prefer the use of the casts so there is no question about my intent.
As to the notation:i = (int)f;versusi = int(f);they are semantically identical. The second form is called the "functional" form of a type cast. It is not invoking a constructor for a class, it is just doing a plain old C-style type cast. The functional form is just a little bit of "syntactical sugar" as K & R used to call it, added to the C++ language specification.