I am wondering if there is any quick way to define the number of places shown after a decimal point. I am making a sensor array for my car and the first one is a boost/vac sensor. I am getting the pressure to display fine, but I only need 1 place after the decimal and not 10. Is there a way to specify this in the code without having to use a mod(%) calculation?
For printing data, you can use Print::print(float, int), where the second parameter is the number of digits after the decimal.
This is usually seen in code as Serial.print(value, 2); or LCD.print(value, 3);
For actual computational simplicity, you're stuck with % - it's the most efficient operator that will do what you want. For a single decimal digit of accuracy, just multiply everything by ten, and divide by ten + decimial point + mod ten to get a fixed-point number.
Aeturnalus:
divide by ten + decimial point + mod ten to get a fixed-point number
Or just manipulate the text to insert a decimal where needed. Maybe something like this:
void encode_voltage(void)
{
long voltage;
char buf[8];
/* The AtoD reading is 0V..5V yielding readings of 0..1023
* Voltage is computed from reading by dividing by 204.6.
* We want to do this calculation using integer fixed point
* Arithmetic to calculate volts x 100. This will be reading
* divided by 2.046. To retain accuracy we first multiply
* by 64K, then divide by 65536 * 2.046 -- 134087. Next we
* add the voltage offset provided by the 10V zener diode,
* which is 9.87V or 987. The final fixed point integer
* value is converted to two decimal point fraction by
* inserting a decimal point.
*/
voltage = analogRead(ANL_VOLTAGE_PIN);
voltage <<= 16;
voltage /= 134087;
voltage += 987;
sprintf(buf, "%5ld", voltage);
buf[6] = 0; buf[5] = buf[4]; buf[4] = buf[3];
buf[3] = '.';
memcpy(voltage_message + 1, buf + 1, 5);
frame_send((uint8_t *)voltage_message , VMSG_LENGTH);
}