Go Down

### Topic: Quick question about map( (Read 1 time)previous topic - next topic

#### Pepsicoke

##### Oct 19, 2016, 01:31 am
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

#### lloyddean

#1
##### Oct 19, 2016, 01:39 am
The parameters of 'map' are of type 'long'.

Smaller data-types will be 'implicitly' converted.

#### Pepsicoke

#2
##### Oct 19, 2016, 01:42 am
OH so i would have to convert my "int num" to long?

#### lloyddean

#3
##### Oct 19, 2016, 01:44 am
No, as 'int' is a smaller type the compiler will do the conversion for you.

#### KeithRB

#4
##### Oct 19, 2016, 01:45 am
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?

#### julyjim

#5
##### Oct 19, 2016, 01:46 am
Can the map function use data types in it?

Not what OP asked. Apples and oranges.

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

Smaller data-types will be 'implicitly' converted.
Yes.

#### Pepsicoke

#6
##### Oct 19, 2016, 01:50 am
Yes keith that is what i want exactly! What am i doing wrong

#### Pepsicoke

#7
##### Oct 19, 2016, 02:03 am
Does anyone know what i am doing wrong?

#### Delta_G

#8
##### Oct 19, 2016, 02:08 am
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.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

#### Pepsicoke

#9
##### Oct 19, 2016, 02:59 am
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

#### outsider

#10
##### Oct 19, 2016, 05:01 amLast 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?
https://www.arduino.cc/en/Reference/Map

#### Pepsicoke

#11
##### Oct 19, 2016, 01:32 pm
Yes that is correct outsider

#### septillion

#12
##### Oct 19, 2016, 01:58 pm
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.

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

#### KeithRB

#13
##### Oct 19, 2016, 06:52 pm
What is happening that you consider wrong Can you give us some numbers?

#### Pepsicoke

#14
##### Oct 19, 2016, 06:59 pm
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.

Go Up