wrist holding code

ok, so my robot has a motorized wrist which allows us to dump forwards and backwards with our claw, however were coming across a problem where the wrist falls down while holding problems, after adding a holding value, it doesnt hold still. not knowing pid can anyone help us with code to hold it in place?

Learn PID… it’s a lot less complicated that it seems. For now just a P control loop should work, but you might oscillate a little without the I and D terms.

int target = 100; //value the sensor reads when you have the claw at the desired holding value

int kp = 1; //tune this until you get minimal oscillation

int error = SensorValue[sensor] - target; //calculate the error between where you are now and where you need to be

motor[clawHinge] = error*kp; //set the motor to the error multiplied by a constant

If you do oscillate a bunch with the code that Michael has written, you’re gonna want to lower your kP value. If you’re not oscillating with the kP of 1, I would keep increasing it until you do. Once you get to that point, lower it until the oscillation stops. The bigger your kP, the faster you will reach the set value.

Thanks guys