map question

Hello all, I'm just starting to learn the map (is function the correct word?). From the Map reference:

  int val = analogRead(0);
  val = map(val, 0, 1023, 0, 255);
  analogWrite(9, val);

Does this mean if I did a map like the following:

 {int volt = analogRead(0); //input range of 0 to 5vdc on pin 0
volt = map(volt = map(volt, 0, 1023, 0, 5); // will this break the 5 volts into 1024 different parts?
  lcd.print(volt, 2); //then print the voltage to two decimals using 0018 IDE?

Will that work to read voltage and print out? Is this a better way than the calculations of pin0 input * 5.0/1023 to get the voltage? OR is it better to do the calculations?

OR am I trying to use map for something other than it's intended use?

Thanks to a great group for all the help I've received.

Ken H>

The map function performs integer arithmetic. If you’re happy having the value (in the range 0 to 1023) mapped to 0, 1, 2, 3, 4, or 5, the map function will be perfect for you.

Oh, I didn’t think so. So, you will need to perform your own (floating point) mapping.

You could "map" to the range 0..500, and then divide by 100.0.

Thank you for the response Paul - I was sorta thinking it was integer is why I mentioned the 2 decimals.....

If the result were mapped to volt = map(volt, 0, 1023, 0 500) //this would now set to 1, 2, 3,.....234, 235,.... 498, 499, 500?

I could then do a volt = volt/100 to get the 2 decimals?

Does this give any better results than normal calculations? OR just a different way of getting the same thing? The result is still going to depend on the voltage.. is that "rail voltage" when the default 5.0vdc is used?

Thank you for the help Paul... and others who have provided this boy so much help. Ken H>

The map function is using integer arithmetic. Mapping to 0...500 will result in less loss of precision, but there will still be losses.

Multiplying by 5.0, then dividing by 1023 will give more precise answers. The trade-off is time to calculate. If time isn't a factor, but accuracy is, then do the math yourself.

If time is a factor, and accuracy isn't as important, use the map function.

If both are critical, use different hardware.

Thank you Paul, that was what I was looking for - which is the most accurate. Sounds like doing the math is. I suspected so, but just wished to check with the experts.

AWOL, you must have posted your response while I was typing my response - thank you.

Very seldom do any of my simple programs worry about speed or size - not yet anyway. Heck, I'm using resonators rather than crystal in most of my stuff 'cause timing is not an issue.

Again, thanks to all for the help - I do think this is the BEST board I've been on for getting knowledge without so much "static" in the responses. There are a couple of my ham radio boards could emulate this site.

73 de Ken H.

Ken H.