This is my map function; val=map(40,103,0,0,6477) The return value should be 3961 from the arduino help section showing the result for the mathematically inclined.
I am getting val=0. Anybody know what is wrong here? In the calculation there is a negative number in both the numerator and denominator but these cancel out to give a positive answer with my scientific calculator anyway.
Please post an example sketch that demonstrates the issue.
The below does print 3961; tested on a Nano.
void setup()
{
Serial.begin(115200);
int val = map(40, 103, 0, 0, 6477);
Serial.println(val);
}
void loop()
{
// put your main code here, to run repeatedly:
}
map() is using the ratio of of 40:103 in the "103..0" range, so too must the ratio be taken from the range of the "0..6477" scale.
40:103 = 0.388349514563 // the ratio, taken from the input "range"
therefore;
6477-(0.388349514563*6477) = 3961.x// the ratio taken from the output range
Here is my complete code. Something incorrect here as I get val=0. I am using nano.
type or paste code here
int ld;
int az1;
int sr;
int ss;
int val;
int x;
void setup() {
Serial.begin(115200);
}
void loop() {
ld=212;
lt=10.5;
az1=40;
sr=103;
ss=109;
ldg=(ld/360*22000);
x=ldg/2;
if (lt<12.00){
val=map(az1,sr,0,0,x);
}
if (lt>=12.00){
val=map(az1,0,ss,x,ldg);
}
Serial.print("val=");
Serial.println(val);
}
```
sorry I have missed things in the posted code. 'lt' was declared a float. 'ldg' an integer.
Print the value of ldg.
ldg=(ld/360*22000);
a7
is zero as a result of the integer division.
Hello petercl14
Give the variables comprehensible names and no cryptic abbreviations.
What´s the task of the sketch in real life?
you are right jremington. I have now fixed it by declaring LD a FLOAT. now getting the correct values from the map function.
So you can now mark @jremington's reply as the solution and the others helped you a
ld = ((ld * 22000) / 360);
Did you?
int ld;
//...
ld = 212;
//...
ld = ((ld * 22000) / 360);
Result is 30, equal to
(int) (22000 * 212 mod 65536) / 360
a7