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

if (buttonState == LOW){
}

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.
Thanks

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.

``````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?

Not what OP asked. Apples and oranges.

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

Smaller data-types will be 'implicitly' converted.

Yes.

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

Does anyone know what i am doing wrong?

Pepsicoke:
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.

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

I think what you are asking is "can variables be used instead of constants", is that correct?
https://www.arduino.cc/en/Reference/Map

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);

So:
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.

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

Or a nice wrapper function

``````//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);
}
``````

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) {
digitalWrite(inPin, HIGH); // connect internal pull-up
}
for (int i = 0; i < 10; i++) {

// current value works as a sum counter.

// add sample to the sum counter.
currentValue += currentSample;
delay(100);
}

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

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(val);
lcd.setCursor(0, 1);
lcd.print(math);
delay(500);

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.

When i click the button i need my top line to go to zero.

That will happen only is val goes to 0, and that will happen only if math == num. And, since you reset num to 0 every time, then val will be 0 only if math == 0.

Ok let me try to understand this. When you say im putting num to 0 every time do you mean.

static long currentSample; // current sensor sample.
static long currentValue; // current sensor value.
int num;
num = 0; <======(This part)
currentValue = 0;

I just took that part out and still getting 4009 on my top line and bottom line is reading correct at 5349.

Could this clarify the map function ?
You are dealing with three entities
range - independent value specifying the input "range" - for a sake of argument 0 thru 1023
input range of interest - related to "range" - lower and upper limit
output range of interest - related to "input range of interest" - lower and upper limit

In this example, modified / taken form Arduino doc
the "range" 0 thru 1023 is directly mapped to output 0 thru 255.

If you change the int range = 400 your output will no longer start from 0 but from a point where 400 maps to output range.

You can modify both input and output range of interest also. It gets messy but still follows the basic map function - maps the "range" according to "input / output ranges" relations.

I am not clear why you have to change both "range" and the lower value of the "input range" (same time). .

``````void setup() {
Serial.begin(115200);
for (int i = 0; i < 255; i++)
Serial.println(""); // cleanup monitor screen
int input, output;
for (int range = 0; range < 1023; range += 100)
{
output = map(range, 0, 1023, 0, 255);
Serial.print("Input   ");
Serial.println(range);
Serial.print("Output  ");
Serial.println(output);
// put your setup code here, to run once:
}
}

void loop() {
// put your main code here, to run repeatedly:
}
``````

Ok let's try piece by piece. Math is ran by my for statement. This gets me 10 readings an average of my 1 readings. What I'm trying to do is when I press a button it takes a analogread (I have ads1115 analog to digital converter) and puts it in place of num on the map function. This will in turn make my readings turn to zero if math and num are the same in turns of adc readings. Now the 0-20000 is the min and max that I want it to be mapped/converted to. With how my reading me are coming out right now I get 4000 for my top reading(if everything was working right it should be zero) and I get my analog to digital converter number on the bottom row of about 5349 (which is right) My whole reason for going this route was I was told I could. Is this not going to be possible? I feel like it should be but feelings are not always right lol.

Math is ran by my for statement.

It is NOT "ran" by your for statement. It is VALUED by function calls in the for statement.

What I'm trying to do is when I press a button it takes a analogread

The snippet you posted does not show any switches. Buttons are for shirts.

and puts it in place of num on the map function.

Not in the snippet you posted.

My whole reason for going this route was I was told I could. Is this not going to be possible?

Instead of telling HOW you think it should be done, tell us what you are trying to accomplish. If you just want to print 0 on the top line of the LCD, that is super simple.

``````  lcd.clear();
lcd.setCursor(0, 0);
lcd.print("0");
``````

But WHY do you want to do that? If you think that your code is supposed to use one reading as a baseline, and then compare the average of 10 readings to that baseline, you are going about it all wrong.