(Solved)Map function apparently not working

SOLVED

okay, basically I have a analog temperature sensor I have it attached to my Arduino MEGA 2560 on analog pin A14

if I run the default example code of AnalogReadSerial (of course changing the analog pin)

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 
 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A14);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

I get a value of 156 in the Serial monitor. (yes this value changed with temperature as it is suppose to)

when I run this code

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  int sensorValue = analogRead(A14);
  sensorValue =map(sensorValue, 0, 1023, 0, 5);
  Serial.println(sensorValue);
  delay(1);      
}

I just get a big fat 0 in the serial monitor.

Can someone tell me what it going on here? I have been staring at this code for quite sometime and it looks good to me.

Thank you for you help.

Also I am using Arduino 1.0.5 it is set for "Arduino mega 2560 or mega ADK"

The problem might be due to the analog input being a low value that maps to between 0 and 1, which might display as a 0 due to int only being whole numbers. Just a thought.

I think you might be right. How can I make it return a decimal?

Couldn't you just do:

float sensorValue=analogRead(A14)/1023.0f;

JarkkoL:
Couldn't you just do:

float sensorValue=analogRead(A14)/1023.0f;

I think you mean:

float sensorValue=analogRead(A14)/204.6;

He is trying to map onto the range 0-5. :slight_smile:

But your point is valid. For something like this, why use map() at all when a simple division will suffice?

For something like this, why use map() at all when a simple division will suffice?

Especially since map() is an integer function.

He is trying to map onto the range 0-5. :slight_smile:

You are right of course, or better yet:

float sensorValue=analogRead(A14)*5.0f/1023.0f;

for sake of readibility :slight_smile: Compiler will optimize that to single multiplication anyway if performance is a concern.