# problem in linear interpolation program

Page 1 of 1

## 7 Replies - 834 Views - Last Post: 28 October 2013 - 09:30 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=332583&amp;s=8eea848fa550e0a13a6b47edb25d5a34&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 inf2013

Reputation: 0
• Posts: 4
• Joined: 27-October 13

# problem in linear interpolation program

Posted 27 October 2013 - 01:38 PM

i use a code in order to make a linear interpolation program. the code don't work correctly.

the code is :

```

#include <arduino.h>
#include <LiquidCrystal.h>
#include <sstream>
#include <string>
#include <iostream>
#include <math.h>

/*****************************************************************************************************/

#define CALIBARAION_SAMPLE_TIMES  50     //define how many samples you are going to take in the calibration phase
#define CALIBRATION_SAMPLE_INTERVAL 5 //define the time interal(in milisecond) between each samples in the
//cablibration phase
#define READ_SAMPLE_INTERVAL  50      //define how many samples you are going to take in normal operation
#define READ_SAMPLE_TIMES     5      //define the time interal(in milisecond) between each samples in normal operation

#define RO_CLEAN_AIR_FACTOR 60.0  //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,//which is derived from the chart in datasheet
#define VCC 4.49
#define Rl 7.42 // Load resistance 1 kohm

float nodepoints[3][2]= {

{
1,42 }
, {
3,20.9 }
, {
5,15.05 }

};

char messages[6][16] = {
"Heating sensor", "64 seconds", "Autozero", "Ready!", "% Vol= ","Rs= "};

float x;
float y;
float calibration_result;
float logRs;
float x0[5];
float L[5]={0};
float x_initial;
float Rs;
float Ro;

float input;

LiquidCrystal lcd(12, 11,5, 4, 3, 2); // Wiring microcontroller - LCD:
const int pushbutton = 8; // Pushbutton (normally open). Stops initial heating time when it is pressed. .
const int MQ3_analogPin = A0; // Reads sensor voltage as a float int the interval (0-1) corresponding to (0 - 3.3V).

void text_screen( char messages[],  int colum, int row) {
lcd.setCursor( colum, row);
lcd.print(messages);
}

void setup() {

lcd.begin(16,2);
Serial.begin(9600);

lcd.clear();
text_screen(messages[0], 0, 0);
text_screen(messages[1], 0, 1);
delay(2000);
Serial.print("Heating ...");
Serial.print("   ");
Serial.print("  \n");

for (int j = 0; j<4; j++)
{
lcd.clear();  // Heating sensor 4x16 = 64 seconds
text_screen(messages[0], 0, 0);

for (int i = 0; i<16; i++){
if (pushbutton == 1){   // Pressing pushbutton stops initial heating and enters in measuring mode.
break;
}

lcd.setCursor(i, 1);
lcd.write(62);
delay(1000);
}
}

lcd.clear();
text_screen(messages[2], 0, 0);
delay(1000);

Serial.print("Calcul Ro...\n");
Ro = MQCalibration(MQ3_analogPin);//Calibrating the sensor. Please make sure the sensor is in clean air
//when you perform the calibration

Serial.print("Ro calculated...\n");
Serial.print("Ro=");
Serial.print(Ro);
Serial.print("kohm");
Serial.print("\n");

lcd.clear();
text_screen(messages[3], 0, 0);
delay(2000);

lcd.clear();
text_screen(messages[3], 0, 0);
delay(2000);

}

void loop()  {

Rs = Rs_sensor((MQ3_analogPin));
logRs = log(Rs);

Serial.print("\n");

Serial.print("Rs=");
Serial.print(Rs_sensor((MQ3_analogPin)));
Serial.print("   ");
Serial.print("\n");
Serial.print("log(Rs)= ");

// Moyenne flottante

float val= 0.00;
for (int i = 0; i<10; i++){

val +=(Rs_sensor((MQ3_analogPin)));
delayMicroseconds(100);

}

val = val/10;

Serial.print(log(Rs_sensor((MQ3_analogPin))));
Serial.print("   ");
Serial.print("\n");

// Resultat courbe de calibrage

if ( val > 0){
float result= reMap(nodepoints,val);
calibration_result = result;
Serial.print("calibration result= ");
Serial.print(result);
Serial.println("  ");
Serial.print("\n");
}

delayMicroseconds(100);

// Alrame de teneur faible

if (calibration_result < 0.30){
}

else {
}

Serial.print("val= ");
Serial.print(val,3);
Serial.print("  ");
Serial.print("\n");

// Float to string conversion.
char buffer[16];      // you need some space
char buffer2[16];
dtostrf(reMap(nodepoints,val),16,2, buffer);
dtostrf(Rs,16,2, buffer2);

lcd.clear();

for (int i=0; i<strlen(buffer); i++)
{

lcd.setCursor(i,0);
lcd.write(buffer[i]);

text_screen(messages[4], 1, 0);

lcd.setCursor(i,8);
lcd.write(buffer2[i]);

text_screen(messages[5], 1, 1);
}

delay(2000);

}

{

}

/*******************Calcul de Rs**********************
connection : VCC -ETOH sensor - 1k resistor - GND
Measuring voltage Vs between ETOH and ETOH sensor and 1 K resistor
Rs = Rl *(VCC/Vs-1)
*/
float Rs_sensor( float MQ3_analogPin)

{

return Rs;

}

/*******************Resistance calculation*********************/
{
}

/**********************Calibration Mesure**************************/

//float Rs0=

/*int i = 0;

Serial.print("enter number ");
Serial.print(i + 1);
Serial.println(" ?");

float f = Serial.parseFloat();
/*

/***************************** MQCalibration ****************************************

************************************************************************************/
float MQCalibration(int MQ3_analogPin){
int i;
float Rval=0;

for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) {         //take multiple samples
delay(CALIBRATION_SAMPLE_INTERVAL);
}
Rval = Rval/CALIBARAION_SAMPLE_TIMES;                   //calculate the average value

Rval = Rval/RO_CLEAN_AIR_FACTOR;                        //divided by RO_CLEAN_AIR_FACTOR yields the Ro
//according to the chart in the datasheet

return Rval;
}

/*******************************************fonction de calcul teneur en alcool*****************************************/

float reMap(float pts[3][2], float input) {
int rr;
float bb,mm;

for (int nn=0; nn < 2; nn++) {
float minsensor = min (1024,pts [1][nn]);
float maxsensor = max (0, pts [1][nn]);

if (input >= minsensor && input <= maxsensor)
{
/*if (input >= pts[nn][0] && input <= pts[nn+1][0])

mm = (log(pts[nn][1])-log(pts[nn+1][1]))/(log(pts[nn][0])-log(pts[nn+1][0]));
mm = (log((input/pts[nn][1])))/mm;
mm = mm + log(pts[nn][0]);
mm = pow(10,mm);
rr= mm;
*/
for (int nn=0; nn < 2; nn++)
mm = (pts[nn+1][1]-pts[nn][1])/(pts[0][nn+1]-pts[0][nn]);
mm = (input-pts[nn][1])/mm;
mm = mm + pts [0][nn];
mm = rr;
}

else if (input< minsensor)

{
mm = 0;
mm = rr;
}

else if (input > maxsensor)

{
mm = 100;
mm = rr;
}
return(rr);

}

}

```

Is This A Good Question/Topic? 0

## Replies To: problem in linear interpolation program

### #2 jimblumberg

Reputation: 4736
• Posts: 14,768
• Joined: 25-December 09

## Re: problem in linear interpolation program

Posted 27 October 2013 - 02:34 PM

So what exactly is wrong with your code? You need to ask specific questions about the code you provided.

Jim

### #3 inf2013

Reputation: 0
• Posts: 4
• Joined: 27-October 13

## Re: problem in linear interpolation program

Posted 27 October 2013 - 02:57 PM

jimblumberg, on 27 October 2013 - 02:34 PM, said:

So what exactly is wrong with your code? You need to ask specific questions about the code you provided.

Jim

the problem is the linear interpolation function it don't run

### #4 jimblumberg

Reputation: 4736
• Posts: 14,768
• Joined: 25-December 09

## Re: problem in linear interpolation program

Posted 27 October 2013 - 03:30 PM

What function is your linear interpolation function?

Why exactly doesn't it run?

Jim

### #5 inf2013

Reputation: 0
• Posts: 4
• Joined: 27-October 13

## Re: problem in linear interpolation program

Posted 27 October 2013 - 03:57 PM

the function don't run correctly

```
float nodepoints[3][2]= {

{
1,42 }
, {
3,20.9 }
, {
5,15.05 }

void setup() {
Serial.begin(9600);
pinMode(PIN,INPUT);
lcd.begin(16,2);
lcd.print("Alcohol Sensor");
}

void loop() {

for (int i=0; i<10;i++)

{
int x = 0;
val += x;
delay(10);
val /= 10;

}

Serial.print("val= ");
Serial.println(val);
Serial.print("  ");

if (val > 0) {

int result= reMap(nodepoints,val);

Serial.print(result);
Serial.println("  ");
}

lcd.setCursor(0,1);
lcd.print(val);

delay(500);
}

if ( val > 0){
float result= reMap(nodepoints,val);
calibration_result = result;
Serial.print("calibration result= ");
Serial.print(result);
Serial.println("  ");
Serial.print("\n");
}

float reMap(float pts[3][2], float input) {
int rr;
float bb,mm;

for (int nn=0; nn < 2; nn++) {
float minsensor = min (1024,pts [1][nn]);
float maxsensor = max (0, pts [1][nn]);

if (input >= minsensor && input <= maxsensor)
{

for (int nn=0; nn < 2; nn++)
mm = (pts[nn+1][1]-pts[nn][1])/(pts[0][nn+1]-pts[0][nn]);
mm = (input-pts[nn][1])/mm;
mm = mm + pts [0][nn];
rr = mm;
}

else if (input< minsensor)

{
mm = 0;
mm = rr;
}

else if (input > maxsensor)

{
mm = 100;
mm = rr;
}
return(rr);

}

}

```

### #6 jimblumberg

Reputation: 4736
• Posts: 14,768
• Joined: 25-December 09

## Re: problem in linear interpolation program

Posted 27 October 2013 - 04:05 PM

What function doesn't run correctly?

What is that function supposed to do?

What is that function actually doing?

Does your code compile without errors or warnings? If not post the complete error/warning messages exactly as they appear in your development environment.

You are using nonstandard headers that most people here won't have so we can't compile your code so you must be our eyes and tell us exactly what is happening.

Jim

### #7 inf2013

Reputation: 0
• Posts: 4
• Joined: 27-October 13

## Re: problem in linear interpolation program

Posted 27 October 2013 - 05:14 PM

ok,

the code must normally use the interval to give the concentartion vs resistance gives by the sensor.

if for example sensor return 42 the result must be 1 if the sensor return 20.9 the result must be 3 now if the sensor return 30 the code muust do a linear interpolation and use the interval{1, 42} and {3, 20.9} the result is a linear interpolation between those points. unfortonately the result is 0.00

the sensor fonction is an decreasing function more the concentartion is high less is the sensor resistance (response).

i use an arduino code

### #8 Skydiver

• Code herder

Reputation: 4373
• Posts: 14,115
• Joined: 05-May 12

## Re: problem in linear interpolation program

Posted 28 October 2013 - 09:30 PM

You have a few issues, but start off with the fact that you are mixing up rows and columns. The first index is for rows, and the second index is for columns, yet on line 71 and 72 you are always accessing the second row, when I think you intent was to access the second column.

I think that you would have an easier time writing and understanding your code if instead of:
```float nodepoints[3][2]=
{
{ 1, 42 },
{ 3, 20.9 },
{ 5, 15.05 }
};

```

you used
```struct NodePoint
{
float result;
float sensor;
};

NodePoint nodepoints[3]=
{
{ 1, 42 },
{ 3, 20.9 },
{ 5, 15.05 }
};

```