Keeping Track of Previous State in Line Follower

Hello,
I'm working on a line follower for this map, that uses a TCRT5000 5 IR sensor array -

I'm not able to traverse the broad white strip and the black square after it, so I tried keeping track of the previous state of sensors and using it as a condition.
Eg-
If the robot went from a white strip to a case where the leftmost sensor detects black, it means the robot is on the broad strip but misaligned to the left. So, the robot turns to the right slightly to correct itself.

The problem is that this is not working, which I think is because the robot's momentum resulting in a previous State that is the same as current state, since the program loops quickly.
Here's how I coded the previous state tracker:

int previousState[5] = {0,0,0,0,0};
void setup() {.....}
void loop() {
  //Reading Sensor Values
  int s1 = digitalRead(ir1);  //Left Most Sensor
  int s2 = digitalRead(ir2);  //Left Sensor
  int s3 = digitalRead(ir3);  //Middle Sensor
  int s4 = digitalRead(ir4);  //Right Sensor
  int s5 = digitalRead(ir5);  //Right Most Sensor
  // -------- (Other code here)

  int currentState[5] = {s1, s2, s3, s4, s5};

  for (int i = 0; i < 5; i++)
  {
    previousState[i] = currentState[i];
  }
}

Any idea how I can navigate those parts of the map, by correcting this program or using a different approach?

Thanks

You may want to have a state variable saying if you can trust the current reading and if not a couple variables that hold the last trusted reading and time of this reading.

You update the last known good reading only if the current reading is good.

An idea to explore is to keep going as if there was no change for some time if the reading is not OK and hoping that it will not be too long until you get a good reading again

It will pose an issue for the big square so you need a bit more intelligence for that situation (you’ll traverse the square and see nothing until you hit a full signal on the 5 sensors again, so that might be your cue to turn and react to the readings))

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.