Author Options:

Arduino Help (again) - For some reason map() is messing up my analogRead value Answered

Hi again.

I am still messing around with this Arduino and have hit another barrier. I have given up on the last idea. This time I am trying to make 5 LED's light up as I turn the potentiometer. I assigned all the pins to what they were (names and pinMode() ). I then, just to see if I was actually getting the wanted value, I made it write to the serial monitor. Before using map(), when turned fully one direction it read 0 and when I turned it to the opposite side, it read 1023 (which is correct). I tried mapping it and then read the value. It would start off at 400 (with no interaction with the potentiometer) and drop all the way down to around 330 (when it was mapped with a toLow value of 1 and toHigh value of 5 (because I have 5 LED's)). Here is the code...

(Any help would be greatly appreciated, Thanks)

int ledPin1 = 0;
int ledPin2 = 2; int ledPin3 = 4; int ledPin4 = 6; int ledPin5 = 8;

int potPin = 5;

void setup() { // put your setup code here, to run once:

pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); pinMode(ledPin5, OUTPUT);

pinMode(potPin, INPUT);

Serial.begin(9600); }

void loop() { // put your main code here, to run repeatedly: int potPin1 = map(potPin, 0, 1023, 1, 5);

int potPinVal = analogRead(potPin1);

Serial.println(potPinVal); delay(1);




10 months ago

It would be nice to close your other question if you abandonedit already ;)

Looks like you fell for the same culprit in the map function as I did some years ago.
Depending on the IDE you use the map function the code will use an updated version.
The documenttion for this is hard to find as most examples online IMHO just confuse here.
Currently you assign a value to "potPin".
The range you use is from 0 to 1023 for the input and you want to convert this to 1 - 5 as the value for potPin.

In many cass it is ctually suficient to limit the input range to 255 instead of using higher value.
But that should only matter once you need to save on ram nd timing things.
I hope I am guessing correctly that you don't want to install another IDE until you find one that works with your code...
So keep your map function somewhere in the back of the head!
Once you start using a diferent version of the IDE you might need his way instead of the one that should work for your IDE.
To make it work for you it should suffice to change the placement of your values.
The map function you use should work with (x, low input, low output, hig input, high output)
So map(potPin, 0, 1, 1023, 5)
But you can always do it by using standard math:
potPin=(input value X max output)/max input

From what I see however you try to make it too simple and this can result in a wrong interpretation of values.
If you work with analog or decimal input values than it makes sense to seperate all by using indepent names for them
So what ou read from the potentiometer should be assigned to lets say "potPin".
Once you do math with it a good practice is to use another variable for this task.
For example by assining "potVal" the same number as "potPin".
So far so good :)
But have you considered to use "potVal" for the mapping instead of "potPin"? ;)
You also strt the mapping before actally having read any input values.
Another good pactise is to define "sane" values for your variables in the setup part.
Until real values are read these dummies provide the base for otherwise impossible calculations ;)

In case just changing the order of your values in the map function works you might want to consider updating to the latest version of the IDE.
Over time you might collect the most common versions, especially when you start using code from the web that constantly gives you error messages.
Some code and functions only work with a specific IDE versio, so if code is confirmed to complie correctly but on your end and without modifications it fails: try a different IDE version for it ;)


Reply 10 months ago

Yes sorry. Tried to delete it earlier but wouldn't work! Will try again now