Cannot figure out how to simply program Lego Mindstorm NXT robot to follow another moving robot that has a bright LED light attached to it.
Currently my programme looks like this:
If I were you, I'd make it this way:
The leader robot has a very bright light shining backwards, but not in a tight beam, rather a wide cone, so it can be seen from the back easily.
The follower robot has the light sensor facing forward, with a cowl around and in front of the sensor in order to narrow its sensing angle. In other words, the sensor is modified in such a way that it only picks up anything if the light source is directly in front of it, not to the sides. This will help filtering out background "noises" (ambient light and other light sources).
Then the follower robot runs this pseudocode:
while true // enter stopping condition here if required while light is visible // current light value is over a threshold - experiment with this value go forward some amount // if light is not visible the while loop exits and we need to find the light store current light level to variable FRONT rotate 10 degrees to the right // experiment with various values, maybe less is enough, maybe more is needed store current light level to variable RIGHT if RIGHT < FRONT // the light probably moved to the left rotate 20 degrees to the left // twice the amount in the previous turn // else the light probably moved to the right, where we are currently headed // so there is no need to rotate further, just continue with the next iteration, // checking if we see the light now
This way the follower will go after the leader while it is headed in the right way, but when the follower loses the leader, it stops and turns around to look for the light.
This method however has a drawback. If the follower is slower than the leader, it will be left behind during the "stop and look around" phases, but if the follower is faster, it will run into the leader from the back.
You could remedy this by giving the leader a bumper on the back connected to a push button (pressure sensor), and program the leader to only move forward when something (probably the follower) has pushed into its back bumper.
Let me know if you need additional help.