Go Down

Topic: Quick question about map( (Read 805 times) previous topic - next topic


Can the map function use data types in it. For example

if (buttonState == LOW){
int num = analogread(A0);

int math = (10 numbers added together/10);
val = map(math, num, 27000, 0, 10000);

I have tried but am not having luck. Figured i would turn to the forums.


The parameters of 'map' are of type 'long'.

Smaller data-types will be 'implicitly' converted.


OH so i would have to convert my "int num" to long?


No, as 'int' is a smaller type the compiler will do the conversion for you.


Code: [Select]
val = map(math, num, 27000, 0, 10000);

This says take math, which should be in the range of num [which can range from 0 to 1024]  to  27000, and scale/offset it so it fits inside 0 to 10,000.

so, if map is num, return 0 and if map is 27000, return 10000.

Is that what you want?


Can the map function use data types in it?
( ? added )

Not what OP asked. Apples and oranges.

The parameters of 'map' are of type 'long'.

Smaller data-types will be 'implicitly' converted.


Yes keith that is what i want exactly! What am i doing wrong


Does anyone know what i am doing wrong?


Does anyone know what i am doing wrong?
If what Kieth said is what you want then you're doing nothing wrong. If it is doing something other than what you want then why don't you enlighten us as to what you want it to do vs what it is doing now.
If at first you don't succeed, up - home - sudo - enter.


Delta I am trying to use a button to take a analog read if pressed. That analog read then needs to go into my map function as num. it will act as a starting point for my readings. Hope this helps


Oct 19, 2016, 05:01 am Last Edit: Oct 19, 2016, 05:07 am by outsider
I think what you are asking is "can variables be used instead of constants", is that correct?


Yes that is correct outsider


Yes it can. So you do nothing wrong. if that's indeed what you want.

val = map(math, num, 27000, 0, 10000);

Some math = num it gives you 0
if math = 27000 it gives you 10000

But keep in mind, if math goes below num or over 27000 is also goes under 0 or over 10000. If you are not sure math never goes below num / over 27000 and you want to limit the output to 0 to 10000 you have to limit the input (or the output) as well.

Code: [Select]

val = map(math, num, 27000, 0, 10000);
if(val < 0){
  val = 0;
else if(val > 27000){
  val = 27000;

Or a nice wrapper function
Code: [Select]

//only map it if it's in range. Otherwise clamp it.
long mapClamped(long x, long in_min, long in_max, long out_min, long out_max){
  if(x < in_min){
    return out_min;
  else if(x > in_max){
    return out_max;
  return map(x, in_min, in_max, out_min, out_max);
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake


What is happening that you consider wrong Can you give us some numbers?


tatic long currentSample; // current sensor sample.
  static long currentValue;  // current sensor value.
  int num;
  num = 0;
  currentValue = 0;

  if (buttonState == LOW) {
    int num = ads1115.readADC_SingleEnded(0);
    digitalWrite(inPin, HIGH); // connect internal pull-up
  //THIS IS For readings
  for (int i = 0; i < 10; i++) {

    // current value works as a sum counter.
    currentSample = ads1115.readADC_SingleEnded(0);

    // add sample to the sum counter.
    currentValue += currentSample;

  int math = currentValue / 10;
  val = map(math, num, 26689, 0, 20000); //might need to change 26689 to minus offset

  lcd.setCursor(0, 0);
  lcd.setCursor(0, 1);

Here is my code.
So on the top line of my lcd (0,0) i get a number of about 4000. On my botton line i get what my adc is reading which is about 5350. Something is not right. When i click the button i need my top line to go to zero.

Go Up