Motion Tracking Woes

I’m working on a project that tracks motion using two photocells. The way it’s supposed to work is as follows:
-sensors are rotated on a servo at 0, 18, 36, 54, 72, 90, 108, 126, 144, 162, and 180 degrees.
-at each stop, the right sensor will take a reading and store it.
-after this, the servo moves back to 0 and repeats the process for the left sensor.
-in the loop, the arduino checks each sensor one at a time.
-if the sensor’s current reading doesn’t match the calibrated reading taken at that angle, the servo begins moving in the direction of the sensor anomaly (if the left senses an incorrect value, it moves left).
-when the other sensor also senses a value not equal to the calibrated value for the current angle, the servo stops moving
-loop

Instead, this is what happens:
-setup works the way it’s intended.
-in the loop, the servo sits completely still. Waving my hand in front of one of the sensors or walking in front of it does nothing.

The weird thing is, I had gotten it to work last night. I unfortunately made the mistake of tweaking it a bit before figuring out what I did to make it work. Soon enough I had no idea what I had done to make it work. My excuse of a code is as follows:

#include <Servo.h>
int sensorPin[2] = { 2, 3};
int sensorVal[2];
int CalValA[10];
int CalValB[10];
int pos;
Servo X;

void setup() {
  Serial.begin(9600);
  X.attach(9);
  {
    for (int i=0; i<10; i++)
    {
      pos=18*i;
      X.write(pos);
      delay(150);
      Serial.print("X.write(pos); for value ");
      Serial.println(i);
      CalValA[i]= analogRead(sensorPin[0]);
      Serial.println(CalValA[i]);
    }
    X.write(0);
  }
  {
    for (int i=0; i<10; i++)
    {
      pos=18*i;
      X.write(pos);
      delay(150);
      Serial.print("X.write(pos); for value ");
      Serial.println(i);
      CalValB[i]= analogRead(sensorPin[1]);
      Serial.println(CalValB[i]);
    }
  }
  pos=90;
  X.write(pos);
  Serial.println("End of SETUP");
  delay(300);
}
void loop()
{
  sensorVal[0]=analogRead(sensorPin[0]);
  Serial.println(sensorVal[0]);
  if(sensorVal[0] - CalValA[pos/18] !=0)
  {
    while(CalValB[pos/18]==analogRead(sensorPin[1]))
    {
      X.write(pos);
      delay(15);
      pos=pos+18;
      if(pos==0)
      {
        pos=90;
        X.write(pos);
        delay(15);
      }
    }
  }
  sensorVal[1]=analogRead(sensorPin[1]);
  Serial.println(sensorVal[1]);
  if(sensorVal[1] - CalValB[pos/18] !=0)
  {
    while(CalValA[pos/18]==analogRead(sensorPin[0]))
    {
      X.write(pos);
      delay(15);
      pos=pos-18;
      if(pos==0)
      {
        pos=90;
        X.write(pos);
        delay(15);
      }
    }
  }
}

UPDATE: I’ve removed the for loops involving “j” and moved the “pos=pos+18;” to beneath the “X.write(pos);”

Any help would be much appreciated. I apologize for my novice in programming.
Thank you,
The (ever-so ironically named) Engy

You need to put each { on a new line, and then use Tools + Auto Format to fix up that code. It is nearly impossible to follow with the curly braces all over the place.

Then, you need to explain why you are using a for loop to repeat code in setup, when the loop only executes two passes, and you execute different code on each pass.

Dump the for loop, and just do each block of code.

You do the same thing in loop().

   while(CalValB[pos/18]==analogRead(sensorPin[0])){
     pos=pos+18;
     X.write(pos);
     delay(15);
    if(pos==0){

Since the first thing that you do in the while loop is increment pos by 18, how is it ever going to equal 0?

PaulS: You need to put each { on a new line, and then use Tools + Auto Format to fix up that code. It is nearly impossible to follow with the curly braces all over the place.

Then, you need to explain why you are using a for loop to repeat code in setup, when the loop only executes two passes, and you execute different code on each pass.

Dump the for loop, and just do each block of code.

You do the same thing in loop().

   while(CalValB[pos/18]==analogRead(sensorPin[0])){
     pos=pos+18;
     X.write(pos);
     delay(15);
    if(pos==0){

Since the first thing that you do in the while loop is increment pos by 18, how is it ever going to equal 0?

Right. Hahaha. These are things I by all means should've noticed but didn't. I've fixed that. It should be easier to read now.