map function not working

hey all,

im trying to get my project to read a value on A0 via a potentiometer. then take that reading and output it to a percentage.

my pot minimum reading is .46v but floats around .50v when circuit voltage (5v) is fully applied
my pot maximum reading is 4.62v but floats around 4.75v when circuit voltage (5v) is fully applied

i need to out put a percentage based on its position... so at .46v = 0% thru to 4.75v = 100%

i tried the map() function.. it didnt work.. my map() example code was

int pMin = .46;
int pMax = 4.75;

val = analogRead(A0); // reads A0 pin for data
percent = map(val, pMin, pMax, 0, 100.0);

When i fought it for hours and googled the snot out of it, i came up with nothing so i removed it entirely..attached is my original code without map() function... can someone help me out here?

Also, can the code be cleaned up to gain more memory?

sketch_may11a.ino (2.69 KB)

map function does not take decimal (float) values.

https://www.arduino.cc/en/pmwiki.php?n=Reference/Map

I tried the way the example code was written.. it didnt work either...

(deleted)

09ZX-6R:
Also, can the code be cleaned up to gain more memory?

With a simple sketch like yours, I wouldn't worry too much about the dynamic memory. The compiler will give you a warning, but you have plenty of memory left, and are not doing anything, such as using Strings, that can take an unpredictable amount of memory or accumulate over time.

The SH1106 display alone takes 1350 bytes of memory.
Serial adds another 175 bytes, for a total of 1525.

You don't appear to ever use Serial, so you can remove the Serial.begin() statement.

You can save a small amount of memory by changing the print statements to store the literal text in program memory using the F() macro, as an example:

display.println("TPD");

//would change to 

display.println(F("TPS"));

The variable var isn't needed, you are only using it for temporary storage of the sensor value inside each of the functions, and only referencing it once, so just do the analogRead in the equation itself.

void Percent() {
  int val = analogRead(SensorPin); 
  percent = (val / 1023.0) * 100;
}

//remove the variable var and use analogRead directly in the equation

void Percent() {
  percent = (analogRead(SensorPin) / 1023.0) * 100; 
}

It gets a bit more involved, but you can change the arithmetic to eliminate the floating point numbers and use only integers, but since a float and a long integer are both 4 bytes in length that may not save you much.

09ZX-6R:

int pMin = .46;

int pMax = 4.75;

Nope.
int is only for integers (whole numbers, no decimals). For decimals, you will need to use float.

val = analogRead(A0); // reads A0 pin for data

percent = map(val, pMin, pMax, 0, 100.0);

Um... nope.
analogRead() gives you a number from 0 to 1023.
For low voltage (zero volts), it should give you 0.
For "maximum" high voltage (presumably, this would be 5 volts), it should give you 1023.

When i fought it for hours and googled the snot out of it, i came up with nothing so i removed it entirely..

I admit, it can be hard to find the information you need just by googling.
Part of the reason for that is, it is hard to find answers if you don't even know what questions to ask!

It seems that, rather than looking for help with map(), you should have been looking for help with analogRead(), and also for help with data types (such as int, long, float, etc.)

so i changed the lines and it saved me alot more than i expected! Thanks! however the macro function didnt work

david_2018:
With a simple sketch like yours, I wouldn't worry too much about the dynamic memory. The compiler will give you a warning, but you have plenty of memory left, and are not doing anything, such as using Strings, that can take an unpredictable amount of memory or accumulate over time.

The SH1106 display alone takes 1350 bytes of memory.
Serial adds another 175 bytes, for a total of 1525.

You don't appear to ever use Serial, so you can remove the Serial.begin() statement.

You can save a small amount of memory by changing the print statements to store the literal text in program memory using the F() macro, as an example:

display.println("TPD");

//would change to

display.println(F("TPS"));




The variable var isn't needed, you are only using it for temporary storage of the sensor value inside each of the functions, and only referencing it once, so just do the analogRead in the equation itself.



void Percent() {
  int val = analogRead(SensorPin);
  percent = (val / 1023.0) * 100;
}

//remove the variable var and use analogRead directly in the equation

void Percent() {
  percent = (analogRead(SensorPin) / 1023.0) * 100;
}





It gets a bit more involved, but you can change the arithmetic to eliminate the floating point numbers and use only integers, but since a float and a long integer are both 4 bytes in length that may not save you much.

worked !! thanks

took a little bit of math to figure out the volts to bytes conversion but it worked none the less...

spycatcher2k:

int pMin = .46;

int pMax = 4.75;

val = analogRead(A0); // reads A0 pin for data
percent = map(val, pMin, pMax, 0, 100.0);





analogRead does not output a float (you have cast pMin & pMax as INT - wrong), it outouts a value between 0 and 1023,

find your MINIMUM reading, and MAXIMUM reading, replace the pMin & pMax with these values!