2 IR sensor readings

Hello, I am trying to take readings of two sensors at the same time, The weird thing that one of the sensor is reading for the two variables and the mistake seems to be in the code

  • I attached a picture of the readings from only one sensor !!
  • the other sensor take only one readings but it's not accurate !!

This is the code I wrote :

int a,b,c,x,y,z,reading;
float varVolt = 3.308077; // variance determined using excel and reading samples of raw sensor data
float varProcess = 1e-2;
float Pc = 0.0;
float G = 0.0;
float P = 1.0;
float Xp = 0.0;
float Zp = 0.0;
float Xe = 0.0;
float Xz= 0.0;
void setup()
{
Serial.begin(9600);
pinMode(6,OUTPUT);
pinMode(5,OUTPUT);

}

void loop() {
digitalWrite(6,HIGH); // Turning ON LED
delayMicroseconds(500); //wait
a=analogRead(A3); //take reading from photodiode(pin A3) :noise+signal
digitalWrite(6,LOW); //turn Off LED
delayMicroseconds(500); //wait
b=analogRead(A3); // again take reading from photodiode :noise
c=a-b; //taking differnce:[ (noise+signal)-(noise)] just signal

digitalWrite(5,HIGH); // Turning ON LED
delayMicroseconds(500); //wait
x=analogRead(A2); //take reading from photodiode(pin A3) :noise+signal
digitalWrite(5,LOW); //turn Off LED
delayMicroseconds(500); //wait
y=analogRead(A2); // again take reading from photodiode :noise
z=x-y; //taking differnce:[ (noise+signal)-(noise)] just signal

Pc = P + varProcess;
G = Pc/(Pc + varVolt); // kalman gain
P = (1-G)Pc;
Xp = Xe;
Zp = Xp;
Xe = G
(c-Zp)+Xp;
Xz = G*(z-Zp)+Xp;

Serial.print(-Xe);
Serial.print("\t");
Serial.println(-Xz);
}

Which Arduino, and sensors (link to datasheet).

Post a diagram, showing how you have connected the sensors.
And if just photo diodes, the value of the pull up or pull down resistor.
What are the average values returned from the analogue readings.

Most Arduinos only have one A/D that is switched between analogue inputs with a multiplexer.
Crosstalk between analogue inputs is common, and worse if input impedance is >10k.
Leo..

The IR sensor is a TCRT500, I am taking the reading of two sensors from pin A2 and A3, and I am making a kalman filter algorithm and I used the same for both reading except the last one I made two separate equations and then I did serial print for each.

The Arduino I am using is a Nano Arduino.

The issue as stated both equations are taking readings from only one sensor, The other sensor is giving reading too but it's not accurate