Linear equation substitution

Hello guys,
I have used the map function in the arduino code. ```
float val = (float)(map(samplse,0,1023,-3500,3500)/100.0);

`0	 -35
2.61 0
5	35
`
 Here I have taken calibration values from the sensor and plotted a graph. First when I use the power graph, I got **NAN x^NAN**. But when I use the calibration values with linear graph, I got **13.9909711599448x - 35.4904301757266**.

My question is why in power graph, I am getting NAN. Then If I want to add the calibration values of linear graph in the arduino, how will I implement?

Hi,
Can you post your complete code please?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

Here I have taken calibration values from the sensor and plotted a graph. First when I use the power graph, I got NAN x^NAN. But when I use the calibration values with linear graph, I got 13.9909711599448x - 35.4904301757266.

My question is why in power graph, I am getting NAN. Then If I want to add the calibration values of linear graph in the arduino, how will I implement?

not clear what you mean by power vs linear graph?
is the issue with your graphing tool or the arduino code?

when i do your mapping , i get the following values which look correct

    0 -350.00
  100 -281.60
  200 -213.20
  300 -144.80
  400  -76.30
  500   -7.90
  600   60.50
  700  128.90
  800  197.40
  900  265.80
 1000  334.20

NaN, Not a Number. Using a Uno/Mega? Most likely the NaN is being caused by a overflow.

You may, also want to normalize your data.

I used a 32 bit Linear Regression formula that was prone to overflows and gave NaN’s. I switched to a 64 bit LR.

Hi,
Using

float val = (float)(map(samplse,0,1023,-3500,3500)/100.0);

I get;

samplse = 0 val = -35.00
samplse = 100 val = -28.16
samplse = 200 val = -21.32
samplse = 300 val = -14.48
samplse = 400 val = -7.63
samplse = 500 val = -0.79
samplse = 600 val = 6.05
samplse = 700 val = 12.89
samplse = 800 val = 19.74
samplse = 900 val = 26.58
samplse = 1000 val = 33.42

Tom… :grinning: :+1: :coffee: :australia:

int samplse;
int ledPin = 13;
bool LEDState = false;
void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  for (long i = 0 ; i < 1024; i=i+100)
  {
    samplse = i;
    float val = (float)(map(samplse, 0, 1023, -3500, 3500) / 100.0);
    Serial.print("samplse  =\t");
    Serial.print(samplse);
    Serial.print("\t val  =\t");
    Serial.println(val);
    delay(250);
    digitalWrite(ledPin, LEDState);
    LEDState = !LEDState;
  }
}

Hi,
Your equation from the map parameters transforms to this graph.

y = mx +c
m = gradient
c = y value when x = 0

m = rise /run = (3500 - -3500)/ 1023 = 7000 /1023 = 6.842
c = -3500

y = (6.842 * x) - 3500
Which is the resultant equation used in the map function.

you divide b y 100 so

y = (( 6.842 * x ) - 3500)/100

This produces.

samplse = 0 val = -35.00
samplse = 100 val = -28.16
samplse = 200 val = -21.32
samplse = 300 val = -14.47
samplse = 400 val = -7.63
samplse = 500 val = -0.79
samplse = 600 val = 6.05
samplse = 700 val = 12.89
samplse = 800 val = 19.74
samplse = 900 val = 26.58
samplse = 1000 val = 33.42

Tom… :grinning: :+1: :coffee: :australia:

int samplse;
int ledPin = 13;
bool LEDState = false;
void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  for (long i = 0 ; i < 1024; i = i + 100)
  {
    samplse = i;
    float val = (( 6.842 * (float)samplse) - 3500.0) / 100.0;
    Serial.print("samplse  =\t");
    Serial.print(i);
    Serial.print("\t val  =\t");
    Serial.println(val);
    delay(250);
    digitalWrite(ledPin, LEDState);
    LEDState = !LEDState;
  }
}

Hi friends, I am using HG-C 1100 sensor and this sensor measures the range from -35mm to 35mm. I have doubt in using the formula float val = (float)(map(samplse,0,1023,-3500,3500)/100.0);. I have to take the readings from the sensor and should display it in the serial monitor. The values also should be accurate as in the sensor. Can anyone explain me how to take the formula and calibrate the same?
Thanks in advance.HG-C_Series.pdf (3.0 MB)

Hi,
Have you read post #6, I show you the basics of calculating the equation, and have provided it for you.

Tom… :grinning: :+1: :coffee: :australia:

Hi,

How are you reading the value from the HG-C device?
Can you post a circuit of your project?
Can you post the code you are using?
What model Arduino are you using?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

why?

what values do you get that make you doubt?

is it the sensor values or the map() results?

I have doubt with my formula itself.Please guide me?
The sensor is connected with the analog pin of arduino only.

Hi,
If you have doubts, then.

Build it.
Code it.
Run it.
Check its accuracy in the REAL WORLD by experiment.

Adapt and try my code.

Tom… :grinning: :+1: :coffee: :australia:

both Tom and I have shown that the map equation works. it translates values in the range from 0-1023 to proportional values in the range from -35 to 35 (yes I scaled by 10 instead of 100)

what are you expecting the results to be?

1 Like

Hi.
Please post your code and we can advise you.

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

I have tried your code but there is no accuracy. Whatever the values coming from the sensor should display in the serial monitor using calibration

int samplse=0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);                 // initialize serial communication at 9600 bits per second
}

void loop() {
  // put your main code here, to run repeatedly:
  int samplse = analogRead(0);             // read the input on analog pin 0
  
  float val = (float)(map(samplse,0,1023,-35000,35000)/1000.0);  // Map an analog value to 10 bits(0 to 1023) and the target range
  //val = constrain(val, 0, 1023);
  Serial.print("digital value = ");
  Serial.println(val);                 // print digital value on serial monitor
  float voltage = (float)((samplse*5.0)/1023);
  Serial.print("Voltage:");
  Serial.println(voltage);
 delay(3000);                         // delay in between reads for stability
}```
The values I am getting from the sensor should reflect in the code with more accuracy.

After scaling of 100 also, there is no accuracy. Can you tell me how to take calibration?

I am telling the calibration means taking the samples of voltage and the sensor readings and use excel for getting the equation,

Is my calibration correct or what can i do?

you need to explain what you mean by no accuracy?

can you provide the raw data, in text not an image?

Accuracy in sensor when I use this map function float val = (float)(map(samplse,0,1023,-35000,35000)/1000.0); Serial.println(val); , I am getting the the value for example 31.56 in the sensor but in the serial monitor I am getting as 33.90. There is a lot more difference in the values. What can I do to get the accurate sensor values in the serial monitor?
How will I calibrate?