Interesting that you have 4 sensors. Usually there is an odd number of sensors for line-following robot. 4 will work, but might be harder to program.
3 sensors is easiest to program. If either outside sensor sees the line, it is time to adjust to re-center the line. Middle sensor just confirms that you are still on the line & haven’t lost it.
With 4 sensors, you have to decide how you want it to work. If only outside sensor sees the line, you probably want to make a hard turn to re-center the line. If only one inside sensor sees the line, you might want to start a gradual turn. If both inside sensors see the line, go straight.
With 4 sensors, there will be lots of combinations that don’t make sense. You can’t have line under both outside sensors, but not the inside sensors. You can’t have line under ever other sensor. Depending on how sensitive your sensors are, how close together they are and how thick your line is, you probably won’t have 3 sensors seeing the line; you might not even ever have 2 sensors seeing the line (which would make it hard to know when to go straight).
Your code converting the 4 sensors to a byte as if they were binary bits is not “wrong”, but it probably doesn’t help. Most of the numbers are invalid (like 3 sensors seeing the line), and the numbers are kind of meaningless; 1 means there is no line to follow, 2 means the line is to the right, 9 means the line is to the left, 7 means it is in the middle.
There are several options.
- One is you could code a number like you did, but make it more useful. For example, you could code far left sensor only as -1, left-middle as -0.5, both middle as 0, right-middle as 0.5, and right-only as 1. This would give you a direction to go, but it is not immediately obvious what happens if there is no line.
- Same as above, but use other ranges of numbers. They could ranges like above that can be multiplied with a max speed to get current speed, or they could just be coded values (0=hard left, 1=soft left, 3=straight, etc.); your code above could be used for this.
- You could use the right 2 sensors to control the right motor & the left 2 sensors to control the left motor. If the outside sensor sees the motor, stop (or slow) that motor to re-center the line. With 4 sensors, I’m not sure what you want to do for the inside sensors (and you may need to compare to other inside sensor to see if you are starting to drift off the line). Again, you will need extra code to stop if you have lost the line.
- I’m sure there are more options, but these are the most obvious.
In all cases, you probably also want code to stop the robot if all sensors think they see the line. If you are following a black line on white background, lifting the robot will mean that all sensors “see” the line because none of them are getting reflection from white background.
If you are using regular motors (not stepper motors), they will coast when you tell them to stop, so it is very possible to over-run the line (especially if you are trying to go fast), so the robot will no longer see the line. You probably want to stop when this happens so the robot does not just run away.