Temp Sens LM35 + 4066 Switch

Hi
I am trying to program the arduino so that from a single analogue input i get a direct reading from the LM35 and from a second analog input i get another reading from the same sensor but passing through the 4066.

N.B. The 4066 IC is a QUAD BILATERAL SWITCH FOR TRANSMISSION OR MULTIPLEXING OF ANALOG OR DIGITAL SIGNALS. I am going to use this so that the reading i am getting it passes through a different gain. And due to a control switch included in the 4066 i could bypass to whatever gain circuit i want.

In the case i am going to post (note this is just testing) i should get the same reading since i am not bypassing the 4066 reading through no gains.
Due to propagation delay of the 4066 IC i do not get the same reading so i made a simple calculation with the following algorithm.

original signal = read from sensor;
4066 signal = 4066 signal - (4066 signal - original signal);

So example with this algorithm: if i get 40 on the original and 30 on the 4066 i make 40 - 30 to get the difference then subtract again from the 4066 so that i get the two signals to 30.

I programmed this algorithm to be printed on an LCD display and even get a value on the serial monitor.

The code is the following:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


float tempS;
float tempC;
float diff;

int ic = 0;
int lm = 1;
int a=0;

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  lcd.setCursor(0,0);
  lcd.print("Sens:");
  lcd.setCursor(0,1);
  lcd.print("4066:");
}
void loop ()
{
  a++;
  tempS = analogRead(lm);
  tempS = ((5.0*tempC*100.0)/1024.0);
  Serial.print(a);
  Serial.print(".");
  Serial.print("\t");
  Serial.print("Sensor: ");
  Serial.print(tempS);
  Serial.print("\t");
  lcd.setCursor (7, 0);
  lcd.print(tempS);
  tempC = analogRead(ic);
  tempC = ((5.0*tempC*100.0)/1024.0);
  diff = tempC -tempS;
  tempC = tempC-diff;
  Serial.print("HCT4066: ");
  Serial.print(tempC);
  Serial.print("\t");
  lcd.setCursor(7, 1);
  lcd.print(tempC);
  Serial.print("Diff:  ");
  Serial.print(diff);
  Serial.print("\n"); 
  delay(1000);
}

Strange is that on the serial monitor i get:

1.	Sensor: 0.00	HCT4066: 0.00	Diff:  40.04
2.	Sensor: 0.00	HCT4066: 0.00	Diff:  40.04

and on the LCD display i get:

Sens: 00.00
4066: 00.00

Can you please help me out why is this happening, as you can see my understanding of the problem is correct so i get the same signal, so why is it showing only 0??

More over since the difference is nearly always the same i got the 40.04 and subtracted this from the 4066 signal but strange again the sensor signal decreased drastically (which it shouldn't since i am not doing any calculation) and the difference has changed.

Can you please help me figure out this problem?

Thanks & Regards
Combinu

Combinu:
Hi

Hi

Combinu:
...please help me figure out...

For one thing, what's up with this?

  tempS = analogRead(lm);
  tempS = ((5.0*tempC*100.0)/1024.0); // Huh? Why are you using tempC here?

For another thing, what's up with this?

  diff = tempC -tempS;
  tempC = tempC-diff;  // Just too confusing, and makes the printed value of tempC not equal to the temperature

Huh???

This leaves the value of diff equal to the difference between the value of tempC and the (incorrect) value of tempS. It leaves the value of tempC equal to the (incorrect) value of tempS.

My recommendation for increasing the possibility of remaining sane while debugging: Use tempC to hold the value of the temperature from its calculation. Use tempS to hold the value of its calculation. Use diff to hold the difference between the calculated temperature values. If you need some other values, use different variable names for them.

How is the '4066 connected? What are its Vcc and Gnd voltage values? And, by the way, what specific chip are you using? CD4066? 74HC4066? What?

How about making a program that just prints out the integer values of the ADC readings for the two channels (no calculations) and tell us what the results are?

Regards,

Dave

Footnote:
I don't see where propagation delay through the '4066' could affect the readings since you are (apparently) leaving the '4066 enabled for this test, and the voltage from the sensor won't be changing enough to matter. Putting some kind of fudge factor to try to get the "right" answer from incorrect calculations is an exercise in futility, I'm thinking.