Problem with mapping analog value using map()

Hey guys,
I have a problem when using map() function to map 0 - 1023 to 0 - 5000. I have attached the code and print screen of serial monitor output indicating the error. For the love of god I don't know why this problem occurs. So can anyone help me. Please refer to the print screen of serial monitor for the problem.

Thanks

Serial Monitor Output.png

FSR_1_0.ino (539 Bytes)

For some reason when I changed the code 1 to code 2 i am able to eliminate the issue. So why does this happen?

CODE 1

int fsrPin = 0;
int samples[8];
int i;
int k;

void setup()
{
Serial.begin(9600);
pinMode(fsrPin, INPUT);
}

void loop()
{
for(i=0; i<=7; i++)
{
samples = analogRead(fsrPin); // sample pin 0, 8 times
_ samples[k] = map(samples*, 0, 1023, 0, 5000); // convert to 5V*_

* Serial.print("Analog Reading = ");*
_ Serial.print(samples*,DEC);
Serial.print(", Voltage in mV = ");
Serial.println(samples[k],DEC);
}*_

* Serial.println("---------------------------------------");*
* delay (5000);*
}
---------------------------------------------------------------------------------------------------
CODE 2
*int fsrPin = 0; *
int samples[8];
int i;
int k;
void setup()
{
* Serial.begin(9600);*
* pinMode(fsrPin, INPUT);*
}
void loop()
{
* for(i=0; i<=7; i++)*
* {*
_ samples = analogRead(fsrPin); // sample pin 0, 8 times_

* Serial.print("Analog Reading = ");*
_ Serial.print(samples*,DEC);*_

_ samples[k] = map(samples*, 0, 1023, 0, 5000); // convert to 5V*_

* //Serial.print("Analog Reading = ");*
_ //Serial.print(samples*,DEC);
Serial.print(", Voltage in mV = ");
Serial.println(samples[k],DEC);
}*_

* Serial.println("---------------------------------------");*
* delay (5000);[/size]*
}[/quote]

samples[k] = map(samples, 0, 1023, 0, 5000); // convert to 5V

Might well not be relevant, but k is an uninitialised int, that never gets a value.

Samples is a pointer.
A constant.
(k is initialised, it was set to zero because it is a global.)

(k is initialised, it was set to zero because it is a global.)

Didn't realise that.

When posting code, use "code" tags, not "quote" tags.

samples = analogRead()

Serial.print(samples,DEC);

samples is an array, so these lines doesn't make sense....

Perhaps you wanted to create a new variable to store the analogRead in, send to map(), and then store in samples*.*

Ok, next time will do, didnt realise there was a button for code, sorry. So what is the problem and what can I do to correct it?

What I want is analog read to read analog data and store in sample [j] and then map sample [j] to sample [k].

int fsrPin = 0;    
int samples[8];
int j; 
int k;

void setup()
{
  Serial.begin(9600);
  pinMode(fsrPin, INPUT);
}

void loop()
{
  for(j=0; j<=7; j++)
  {
    samples[j] = analogRead(fsrPin);                // sample pin 0, 8 times
    Serial.print("Analog Reading = ");
    Serial.print(samples[j],DEC);
    
    samples[k] = map(samples[j], 0, 1023, 0, 5000); // convert to 5V
    Serial.print(", Voltage in mV = ");
    Serial.println(samples[k],DEC);
    
    delay (3000);
  }
  
    Serial.println("---------------------------------------");

    delay (500);
}

k is still zero.

yaantey:
So what is the problem and what can I do to correct it?

Well, I could repeat myself...

Or here is the same advice I already gave in code (untested):

int fsrPin = 0;    
int samples[8]; 
// no need to globally define j and k, define them when you use them.

void setup()
{
  Serial.begin(9600);
  pinMode(fsrPin, INPUT);
}

void loop()
{
  for(int j=0; j<=7; j++)
  {
    int currentReading = analogRead(fsrPin);
    Serial.print("Analog Reading = ");
    Serial.print(currentReading,DEC);
    
    samples[j] = map(currentReading, 0, 1023, 0, 5000); // convert to 5V and save in the array
    Serial.print(", Voltage in mV = ");
    Serial.println(samples[j],DEC);
    
    delay (3000);
  }
  
    Serial.println("---------------------------------------");

    delay (500);
}

Edit: removed comment on the array deceleration. I misread the for-loop.

int samples[8]; // 8 is fine, but you only need to set this to 7.

I'd prefer 8, to match the for loop.