# Using PID to vary time delay.

I am using two ultrasonic sensors to control manual item entry into a processing area with a stop/go gate.

One on the input increments a counter towards a preset maximum. The other sensor on the exit gate decrements the counter.

I would like to control the flow by speeding up or slowing down the entry gate according to the distance from the max setting.The idea being to stop "bunching" within the process area.

I have done this by using percentages (0 -30%= min delay, 31-60%= 50% of max delay, and 61- 99% =max delay). This works after a fashion but has its shortcomings.

I have heard of PID loop control but my maths knowledge, and advanced programming skills are sadly lacking.

Any assistance would be greatly appreciated.

A PID controller is a feedback loop controlled mechanism where you can adjust an input to the system.

You continuously calculate an error value as the difference between a desired set point and a measured process variable.

The maths involve applying a dynamically calculated value on the input based on Proportional, Integral, and Derivative terms.

So the first thing to think about is to define what input can you play with (assume the gate delay), what is the process variable you are interested in measuring (population inside ?) and what is the desired set point (max capacity ?)

The P in PID stands for proportional control, and is the most important parameter. It may be all you need.

Your process suggests that gate delay should be inversely proportional to remaining slots, so you could try something like this (adjust Kp, the floating point control parameter, to optimize):

``````gate_delay = Kp/(max_count - current_count);
``````

Alternatively, try the following, which will behave very differently:

``````gate_delay=Kp*current_count;
``````

Of course some controls on the input and output are necessary: you can't divide by zero, and the gate delay probably has a reasonable minimum and maximum value. Arduino has a constrain() function for that.

I would argue that you do not need PID control. It is more typically used for a process that can very quickly and needs to react quickly. What you describe does not sound like such a system.

PID works very well for systems where the operating point changes slowly.

BrianA89:
I have done this by using percentages (0 -30%= min delay, 31-60%= 50% of max delay, and 61- 99% =max delay). This works after a fashion but has its shortcomings.

-- ? --

BrianA89:
I am using two ultrasonic sensors to control manual item entry into a processing area with a stop/go gate.

What you've described sounds like "automated item entry". What part of you system is manual?

jremington:
PID works very well for systems where the operating point changes slowly.

I did not say it did not work. I stated is was not needed. It often adds complexity for little/no benefit, especially on slow changing input.

For something like a slow temperature ramp with a bake/soak phase, PID is the solution of choice.