Long time constant ramp generator

This is my first posting.

I'm using an Arduino MEGA 2560 processor.

I've been trying to find a published sketch for a long time-constant up/down ramp generator. I'm trying to make a ramp generator that will ramp up or down to a variable analog voltage reference and hold it when it is triggered by external contact closures. The ramped output would drive a PWM output which would be externally amplified and used to drive a small DC motor.

Any help would be greatly appreciated.
Walt

waltsar:
I’ve been trying to find a published sketch for a long time-constant up/down ramp generator. I’m trying to make a ramp generator that will ramp up or down to a variable analog voltage reference and hold it when it is triggered by external contact closures. The ramped output would drive a PWM output which would be externally amplified and used to drive a small DC motor.

When you speak of “long time-constant” do you mean milliseconds, seconds, minutes, hours, days… ?
By “variable analog voltage reference” would I be right in guessing that you want to set the target point of the ramp with an analog input?
By “and hold it when it is triggered by an external contact closure” would I be correct in guessing that you want said target to be set by said analog input only when the contact goes from open to closed?
Such a program will be nearly trivial.

const byte InputPin = A0;
const byte ContactPin = 2;
const byte OutputPin = 3;
byte CurrentOutput = 0;
byte Target = 0;
unsigned long PreviousTime;
const unsigned long RampInterval = 3273;  // Milliseconds between steps (3.273 seconds)
void loop() {
    boolean contactState = digitalRead(ContactPin);
    static boolean previousContactState = HIGH;  // INPUT_PULLUP pins default to HIGH

    if (contactState != previousContactState) {
        previousContactState = contactState;
        if (contactState == LOW) { // Contact Closure
            Target = analogRead(InputPin) / 4;  // PWM value from 0 to 255
        }
    }
unsigned long currentTime = millis();
if (currentTime - PreviousTime >= RampInterval) {
    PreviousTime += RampInterval;
    if (Target < CurrentOutput)
        CurrentOutput--;
    if (Target > CurentOutput)
        CurrentOutput++;
    analogWrite(OutputPin, CurrentOutput);
    }
}

Thanks for the quick reply.

Supply voltage = 12VDC

long Time constant = Variable from 20sec to 200sec.

Variable Analog Input = 3 inputs; 0volts, 4 volts, 8 volts

One of three external contact closures would select which analog voltage input to use as the reference (and hold that value until the next contact closure selects another analog-in)

I hope that makes my question a little more understandable.

Walt

Variable Analog Input = 3 inputs; 0volts, 4 volts, 8 volts

You'll need a [u]voltage divider[/u] to scale-down the voltages, since the Arduino can be damaged by more than 5V (anything higher than Vcc).

And, your [u]if-statements[/u] should look for a range of voltages, since the voltage and/or voltage-measurement are analog an imperfect... for example, anything greater than 1.5V [u]and[/u] less than 3.5V can be treated as 3V, etc.

BTW - This is up to you, but your program doesn't have to convert to "volts", you can pre-calculate the range of ADC values representing each switch/voltage state. Then of course, put a comment in your code explaining what you're doing, as well explaining the need/use of a voltage divider.

waltsar:
Supply voltage = 12VDC
long Time constant = Variable from 20sec to 200sec.
Variable Analog Input = 3 inputs; 0volts, 4 volts, 8 volts
One of three external contact closures would select which analog voltage input to use as the reference (and hold that value until the next contact closure selects another analog-in)

I hope that makes my question a little more understandable.

OK... So if I understand the new parts of the specification you want your output voltage to ramp to 0V, 4V, or 8V based on which of three contacts closed most recently. You want your ramp time or ramp rate to be adjustable somehow.
Did you want to control ramp time or ramp rate? Ramp time would mean taking the same time from 8V to 0V as from 4V to 8V, for example. Ramp rate would mean taking twice as long from 0V to 8V (8V change) as from 8V to 4V (4V change). Selecting ramp time will produce funky results if the target voltage changes in the middle of a ramp: If you are going from 0V to 8V at a rate of 53 seconds per ramp and at 5V you change the target to 4V that would mean taking 53 seconds to go from 5V to 4V. I recommend you select ramp rate (volts per second or seconds per volt).
How do you want to input the desired "time constant"?
Since your power supply is 12V did you want your 4V to be 33% duty cycle and 8V to be 66% duty cycle or did you want to measure the output voltage and adjust for the load? To move to and maintain a target voltage you will probably need a PID control loop to control the duty cycle.