Hi guys! Anyone of you know how to write the code for a wall following quadcopter using PID method? I am using the MB1010 LV-MaxSonar-EZ1. A simple sketch of it will do Have you got a program that reads distance from the wall using the sonar device ?

Have you got a program that controls the quadcopter ?

yea i got it but the problem now for me is to write the code for a wall following quadcopter using PID method.

Do you understand the basis of how a PID works ?
Basically you will be setting a target distance, reading the actual distance then passing both into the PID which will return an error value proportional to the difference. You then use the error value to adjust the quadcopter motors to move the quad closer to or further away from the wall and keep doing the measure/PID/adjust.

What the PID does that simple measurement and adjustment cannot do is to make the error value proportional to the difference between the target and actual distances, hence the P in PID, so that corrections get smaller as the target distance is approached.

Have you downloaded and installed a PID library and looked at/run any examples ?

i got it

What is "it" ?

This function is inside the void loop:

if(SONAR == 1)
{
moveLeft();
}

This function is outside the void loop:

void moveLeft()
{
prevErrLeft = currErrorLeft;
currErrorLeft = setpoint - distance1;
op2 = kp * currErrorLeft;
velocityLeft = (currErrorLeft - prevErrLeft)/ T;
dFactorLeft = D * velocityLeft;
roll = roll + op2; + dFactorLeft;

}

It seems that my quadcopter can only roll right and not able to roll left. I want it to stay in the setpoint position.

It's no good posting snippets of your code. Please post it all.