Notifications
Clear all

Arduino map() function

5 Posts
4 Users
0 Likes
2,826 Views
Sumanta
(@sumanta)
Member
Joined: 3 years ago
Posts: 197
Topic starter  

Can anyone please explain me the syntax and usage of the map() function of Arduino?

How to use it with a potentiometer?


   
Quote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1085
 

Actually, they explain it quite well on the Arduino website, and the first example they give is exactly what you need to control a potentiometer. 

The map function just takes a range of values and maps it to another range of values.  You specify the low and high points of both the input and output values.

So this statement will map the analog input A0 (which is 10-bit so it ranges from 0-1023) to a range of 0-255, so you can drive a PWM or servo output:

val = map(analogRead(A0), 0, 1023, 0, 255);

You'll find int mentioned in many of my videos and articles if you need other examples

😎

Bill

 

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
Sumanta
(@sumanta)
Member
Joined: 3 years ago
Posts: 197
Topic starter  

Thank you for clearing this. 😀 


   
ReplyQuote
Photo Bud
(@photo-bud)
Member
Joined: 5 years ago
Posts: 89
 

How fortuitous! I was just in the midst of looking for documentation on map() for a Uno/TFTLCD project I'm working on and needed to know what it was doing so I could understand the map() for translating coordinates from a design basis to the the actual width/height of the LCD screen!

Thanks to both the op and Bill!

Photo Bud (aka John)
The Old Curmudgeon!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@dronebot-workshop

Posted by: @dronebot-workshop

Actually, they explain it quite well on the Arduino website,

Hi Bill, I posted the following to "kevin333", just a couple of day's ago:

In your Arduino IDE:

Help -> Reference

Cheers.

...and as part of that thread, I also put forward a question to you 😉


   
ReplyQuote