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);
}
