Decimal Points with Map – Nigel Moody

This question comes from Nigel Moody. It comes from this lesson:

The Question

“Hello Micheal,
Enjoying your course, and finding I am taking it in a little slower now, but I am persevering.
I have a pressure sensor from work I have been trying to test. I have been fairly successful with the readings but i am stuck getting a final reading in Bar. The sensor works on 4-20mA, I have managed to get readings from that to 0 – 5 volts.
Read the sensor value from 0 – 1023
Read the mA from 0 – 20.
I appear to be stuck getting a decimal reading in Bar, so in short 4mA should be 0 Bar.
Using the map function I can get the conversion but I cannot get it to read a decimal, it will only jump up in whole increments. I have searched and tried different variation but cannot get the result I am after.
Would be pleased for a little help or a nudge in the correct direction.”

/*
   To test a 4-20mA pressure sensor.
   voltage range 8-30 volt dc
   2 wire operation
*/

//set analogPin input pin
const int analogPin = A0;
//
int sensorValue = 0;
float bar = 0.0;

void setup() {
  // put your setup code here, to run once:
  //Start serial monitor baud rate.
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  //variable sensorValue with analogRead
  sensorValue = analogRead(analogPin);
  //convert 0 - 1023 to 5 volts and make a float to get a decimal
  //output.
  float voltage = sensorValue * (5.0 / 1023.0);
  // Serial print voltage output 0 - 5 volts
  Serial.print("Voltage ");
  Serial.println(voltage);
  //
  // Serial print senosorValue 0 - 1023
  Serial.print("sensorValue ");
  Serial.println(sensorValue);
  //
  //Serial print mA output with conversion in decimal
  float mA = sensorValue * (20.0 / 1023.0);
  Serial.print("mA ");
  Serial.println(mA);

  // bar = map (mA,4,20,0,6);
  bar = map (voltage, 1.0, 5.0, 0.0, 6.0);
 // bar = map (voltage, 1, 5, 0, 6);
  // float bar = map (mA, 0.0, 20.0, -1.0, 6.0);
  Serial.print("BAR ");
  Serial.println(bar);
/*
 * Problem getting a decimal output for the BAR reading.
 * The sensor reads from 4-20mA = 0-6Bar if the reading
 * is less than 4mA then it brings up a fail to say the 
 * wire could be broken, I will attempt that code when 
 * I have the correct output.
 */


  delay(500);

}

 

Your Challenge

Take a look at the map function documentation, is it possible to get decimal number back from the map() function? (i.e. decimal numbers are stored with the data type “float”)
 
BONUS Challenge: Could write your own map function using the code provided on the map() documentation page?

Solution

Here are my initial thoughts:

Nigel came back with a solution, here are some more thoughts:

Your Thoughts

What do you think? Did you try writing your own Map function?

4 Comments

  1. Robert Hatcher on March 12, 2020 at 8:52 am

    Yep happy to say I eventually got it sussed yippee!:
    The Sensor Value is: 1023 The Voltage is: 5.0 The Current is 20mA The Pressure is 6Bar
    The Sens…
    etc.

    • Michael Cheich on March 12, 2020 at 9:56 am

      Sweet! Nice work Robert!

  2. Robert Hatcher on March 12, 2020 at 8:53 am

    Forgot:
    Thanks, Michael and Nigel!

  3. Alan Feller on February 4, 2022 at 11:52 am

    This was an extremely helpful problem, debug, and solution. I am relatively new to programming but it’s pretty obvious that mapping or “re-scaling” is a necessary tool for reading and controlling inputs and outputs in the real world. I have NO DOUBT I will be referencing both Nigel and Michael’s solutions in the future for my projects. Thank you both.
    Alan

Leave a Comment