Map and write in one step.

Most code for reading an analogue value, mapping and writing to a servo do something like:

mappedvalue = map(analogValue, 0, 1023, 0, 179);


Is there any reason why I can't do this in one step such as:

servo1.write(map(analogValue, 0, 1023, 0, 179));

Just seems more compact.
Any comments/warnings?

There is no reason why you cannot do it all on one line.

The reason I would not do it is because I would not be able to print the intermediate values for debugging purposes.


servo1.write(map(analogValue, 0, 1024, 0, 180));
servo1.write(map(analogValue, 0, 1024, 0, 181));


Ops sorry about the (lack of) code tags:

for (i = 0; i < all_answers; ++i)
    printf("Many Thanks");

Since both ranges start at 0, why not:servo1.write((analogValue * 180UL) / 1024);

and why not getting rid also of the analogValue and dump the call to analogRead() right in there if you want to write long lines :slight_smile:

it all depends if you need the value later on.

if mappedvalue is a byte and you write

analogValue = analogRead(myPin);
mappedvalue = map(analogValue, 0, 1023, 0, 180); // only 1023 will lead to 180
//mappedvalue = map(analogValue, 0, 1024, 0, 181); // 1019 and above will lead to 180 

the optimizer will likely keep mappedvalue in a register and not read from memory to call write() on the Servo