Using Map function with a potentiometer

I've been trying to read an output from the potentiometer and map the values from the base values of 0 - 1023 to values of 1 - 24 (I need it for a 24 hour time thing). I've got the code displaying to the serial monitor, but it's only display the base values, not the mapped ones. Is anyone able to help?

void setup() {
//the setup routine runs once when you press reset

Serial.begin(9600); //initialize serial communication at 9600 bits per second
}

void loop() {
//the loop routine runs over and over again forever

int sensorValue = analogRead(A0); //read the input on analog pin 0

map(sensorValue, 0, 1023, 1, 24);

Serial.println(sensorValue); //print out the value you read

delay(10); //delay in between reads for stability
}

Potentiometer_Value_Test.ino (505 Bytes)

You did not post properly. As a result, even if I could have helped I cannot now because I am unable to view your code. Please read three of the four locked posts at the top of this forum, and anything to which they link.

Many helpers with mobile devices cannot see the code that you posted as an .ino file. Sad but true.

map(sensorValue, 0, 1023, 1, 24);

You use the map function correctly but you don't use the mapped value.

map(sensorValue, 0, 1023, 1, 24);

This uses the value of sensorValue but it does not change its value

Try

byte mappedValue = map(sensorValue, 0, 1023, 1, 24);
Serial.println(mappedValue);

Hey, if you've yet to figure out your map() function problem with potentiometers (speed pot), see if this helps...

The first two numbers state the potential lo/hi SCOPE of the speed pot...from 0 to 1,023.
Think of the second number as how many times we're dividing-up the speed pot's sweep...1,024 times starting at zero, just like 1,024 potential points of value we can read.

The third and fourth numbers are the RANGE (point A to point B) we want the SCOPE reMAPped to, but instead of starting at zero and going to 1,023, we start at point A and stop at point B.

e.g., if we used the third value as 200 and the fourth value as 823, we're telling the speed pot we're not interested in it's potential values dropping so low or raising so high, so, we trimmed the lower 200 values off of the bottom-end and trimmed the upper 200 values off of the top-end of the speed pot.

Are ya' with me on this? The upper and lower 200 values on the speed pot were removed; eliminated from potential. No more zero, no more top speed.

This is valuable when dealing with things like dc motors that are temperamental and can't rotate too slowly. Because we've trimmed the bottom 200 values off of the speed pot (values 0 to 199), the speed pot can no longer go to zero; the potentiometer now starts at the remapped value of 200, so the motor cannot go too slow. Likewise, we trimmed 200 values (values 824 to 1,023) off of the top-end of the potentiometer, so we can also control the top speed of the motor in this manner.

So, if you state "1, 24" as the remapping, you can see where your troubles lie. I'm not sure exactly what it is your project curtails, but I hope this may shed some light on the map() function and it's arguments.

best of luck.

1 Like