analogIn analogOut problem

when i change the map function range to any other value why i still get the same value on my serial print like default range is 0 to 1023 & 0 to 255 but i change to any other value like 0 to 500 % 0 to 125 but in serial out i get the same default range while changing the pot

The circuit: * potentiometer connected to analog pin 0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground * LED connected from digital pin 9 to ground

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out)

void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }

void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);

// wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); }

Please can you explain what you mean, preferably with examples. And code tags.

Do you mean like this?

 outputValue = map(sensorValue, 0, 1023, 0, 125);

i want to know why map function not change the range. This is builtin example, i just want to know with the pot and led map 0 to 1023 and 0 to 255 as the range of analog in and out. so what if i want to change this range. if i change the value in map like map(sensorValue, 0, 500, 0 , 125); i still getting the output range 0 to 1023 on my serial port while changing the pot.

Because the pot still reads 1023 steps, the adc is 10-bits and that's a hardware thing.

But from 500 upwards, you're converting it to 125 as an output. edit... that seems not to be the case.

more edit.... you need constrain()


Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7,148850.0.html

You are reloading your sketch everytime you make a change?

Tom… :slight_smile:

MrRobot007: map(sensorValue, 0, 500, 0 , 125);

When the thing you're mapping falls outside the range you have specified, the map function will extrapolate.

So the following result in identical output:

map(someVal, 0, 500, 0, 125);
map(someVal, 0, 1000, 0, 250);

Thnx for replies, i understand map function extrapolate. Its not capable to modifies the limits.

MrRobot007: Thnx for replies, i understand map function extrapolate. Its not capable to modifies the limits.

There are no limits. If you want limits use constrain function before map.

It appears that you are doing

map(someVal, 0, 1023, 0, 255);

and expecting map() to modify the value in someVal. That is NOT what it does.

int mappedVal = map(someVal, 0, 1023, 0, 255);

WILL result in a different value in mappedVal from what is in someVal.