# Help with PID Line follower

So i've been working on a line follower using basic if statements to control it, but researching more i've learned that PID is problaly the most efficient way of making my robot follow a line, but i'm realy lost with the programing as of yet so i need some much needed help

first a lil about my line follower, my line follower is built to follow a black line on a white surface using 6 IF emmiter/detector sensors laid out so that the two inner sensors should be on the line if its going perfectly straight and then the other 4 sensors, 2 on each side, are stagered outward, so that the line will always be on atleast 1 or 2 of the sensors.

my first question is i've been looking at other line follower code using PID and they all are reading the sensors using anolgue, how ever i am using digital, LOW = black line and HIGH = White surface. so is it possible to make PID work using digital inputs ?

The idea behind PID is to control something based on input from something else. For instance, you might vary the amount of heat applied to a pot of soup based on the temperature of the soup in the pot. You’d turn down the heat as a the soup neared a specific temperature, so as to not overshoot the desired temperature by too much. As the soup cooled, you’d add heat. A little cooling, a little heat. Drop an ice cube in, you’d add a lot of heat, initially, tapering off as the temperature neared the setpoint.

So, what do you want to control, based on on/off input?

yes i understand the concept of PID, however when i start looking at the code of other line followers using PID i get a lil overwhelmed. but from my understanding they use various equation based on the feedback givin by the sensors so that the program knows where the line is, and then by knowing the amount of error, can then adjust the speed of the motors so that its straight, and i think part of it has to do with time i think, which involves a lil tweaking most likely. but my line sensors are all digitalread and my motors are analogWrite. other line followers that iv'e seen on the web use analog read for the sensors.

however beacause there my sensors are all set up so atleast 1 sensor is always on i can assig each sensor to equal a different value so i could make an equation so that the program could figure out how much error there is and gradualy increase the speed on one side and decrese on the other side so that it my line follower is straight on the line.

cause right now my line follower can normaly keep straight on a straight line withought jerking too much, but on turns its back and forth. so if my line follower could transition into the curve i could reach higher speeds.

The line followers that use analog sensors don't see a sharp edge for the line, so they are using PID to keep the robot following the brightest/darkest portion of the line.

You could do something like what they are doing, by assigning weights to the values received from the 3 sensors on each side. The closest to the center has the lowest weight. The other sensors have progressively higher weights.

Read each sensor, multiply be it's weight, and add the values, to form a left and right value.

As the left or right value increases, the robot is farther from the desired path, so it needs to turn faster to get back to the desired path. The direction to turn is easily determined.

Well ya i tried something like that originally giving each of my motor a max speed and then subtracting that speed by the weight of each sensor, but that has more or less the same jerking effect.

what i want is to more transition more smoothly,
so what im thinking now is if i constrain a variable so that is can only equal 0 to 255, and then give each sensor a weight to decrement the speed by. that way the longer my line follower isn’t going straight the more it will slow down the other motor. and if it hits the next sensor it will slow even faster.

so i have 6 sensors 2 of them are ment to stay on the line when it goes straight so if it goes off the line and hits the 1st sensor it decrements the speed of one motor by 1, if it hits the secound it decrements it by 2, until the 2 middle sensor are both on, when there both on reset the speed to full,.

just hook up the doohickie up to the thingamajig