Variable Setpoint on PID Control DC Motor Position

Hello. Im working with the PID library to control a dc motor position, the idea of the program is to make an oscillatory movement with the motor, declaring two setpoints. For example if we put setpoint1=15 and setpoint2=45, the motor will go to 15, then 45, then back to 15, then 45 and so on.
The problem is that the motor go to the first setpoint and it stops there ignoring setpoint2. On the code its only for the first setpoint, i cant get it right. Im hoping someone can help me.

P.S. Sorry for the bad english.

pidpos4.ino (5.63 KB)

In your code your PID defintion refers to setpoint

PID myPID(&input, &output, &setpoint, kp, ki, kd, DIRECT);

and I can't see anywhere in your program where you have setpoint = setpoint2

A better approach might be to use setpoint as the variable for use by the PID function and have two other variables setpoint1 and setpoint2 either of which can be used to set the value in setpoint


Thanks for the answer!
I know, in this code I didn't put any of the many tries. I can't find where should I put the new value for the setpoint. It always get ignored.

I can't find where should I put the new value for the setpoint. It always get ignored.

If you don't post the code that shows what you tried I cannot help you correct it.


A PID requires a feedback loop, usually referred to as a process value. What feedback are you getting from a DC motor? What kind of control to expect to get from a PID? Normally you might control RPM with some kind of tachometer or encoder attached but it sound more like your talking about a servo motor in which case a PID has no place in the equation.

I'm using a quadrature encoder with it attached to the motor. I get it to work alredy. Thanks!