Maybe some of great people here have come across to the issue i'm facing right now. I am ramping up a variable (setpoint) in my program from 0.0 - till 3.3 using millis(). When the variable is at 3.30, i want to ramp it down again to 0.01. But i fail. Any ideas or tipps? to achieve that would be highly appreciated.
double setpoint_value_Ramp_Hold_Dump()
{
unsigned long start_millis; unsigned long current_millis; const unsigned long hold_1=1000;
current_millis=millis();
if(current_millis-start_millis>=hold_1 && setpoint<=3.279)
{
setpoint=setpoint+0.1;
start_millis=current_millis;
}
}

Your code cannot work, setpoint is not declared. When you post examples, please make sure that they actually work.
That said; What you are trying to do cannot be done in a method with local variables.
David_ZI:
When the variable is at 3.30, i want to ramp it down again to 0.01.
Probably with something like this
define a variable
float setPointAdj = 0.1
change your code roughly like this
setPoint = setPoint + setPointAdj;
if (setPoint >= 3.3) {
setPointAdj = -0.1
}
...R
Or a boolean, like:
boolean RampingUp = true;
double setpoint_value_Ramp_Hold_Dump()
{
static unsigned long start_millis = 0;
unsigned long current_millis;
const unsigned long hold_1 = 1000;
current_millis = millis();
if (current_millis - start_millis >= hold_1)
{
startMillis += hold_1;
if (RampingUp)
{
setpoint += 0.1;
if (setpoint > 3.3)
{
setpoint = 3.3;
RampingUp = false;
}
}
else // not RampingUp
{
setpoint -= 0.1;
if (setpoint < 0.0)
{
setpoint = 0.0;
RampingUp = true;
}
}
}
What you are trying to do cannot be done in a method with local variables.
Oh yes it can if the variables are declared static so that they retain their value between calls to the function.
UKHeliBob:
Oh yes it can if the variables are declared static so that they retain their value between calls to the function.
Yes, but that was not what he did.
Danois90:
Yes, but that was not what he did.
Exactly. He did not declare the variables as static, but what he was trying to do was perfectly possible to do using local variables
I have added the code block JohnWasser posted above and it does exactly what I was aiming for. Thanks guys for nice assistance, and Danois90, I know that variables where not declared as static, but that was secondary thing, because my aim was to figure out, the ways how one can dump the value of the variable which it was holding in it, and all other posters got the point I was aiming for. Danois90 you were discussing cooking fish without having it in your kitchen.
by the way, what I could use to tell the same setpoint variable in the program above, to stay at one value for 1 second and then ramp down to zero again?. As I know the delay(time) function HALTS the program at that point, which means that it will not work. Are there other ways to tell variables to stay at certain value for a certain time?
:-\ :-\
The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.
Have a look at Using millis() for timing. A beginners guide if you need more explanation.
...R
David_ZI:
by the way, what I could use to tell the same setpoint variable in the program above, to stay at one value for 1 second and then ramp down to zero again?.
it ALREADY stays at 3.3 for one second because 'hold-1' is set to 1000 milliseconds (1 second). It holds for 1 second at EVERY value.
It holds for 1 second at EVERY value.
what I should do to make the setpoint not hold at every value for a second, but say, start from 0 Volts, ramp to 3.0 Volts with maximum speed, wait there for 10 milliseconds, and then ramp down again to zero. As of in the picture posted.

That graph is more like:
setpoint = 0.04;
delay(2);
setpoint = 1.75;
delayMicroseconds(8500); // 8.5 milliseconds
setpoint = 0.08;
Nothing on that graph looks anything like a 'ramp'. It sounds like you are confused about what it is you are trying to accomplish. Perhaps you should start with what the goal is and not jump into designing code until the specifications are clear.
i doubt that the strong oscillations measured on my setup could be as a result of the stepwise behavior of the setpoint variable, or at least what is happening inside the fucntion ramp_hold_dump(). or maybe event loop() aka void loop().
I'm very much confused by the labeling of your graphs.
Graph "No1":
The X axis runs from under 55 to over 70 and is labeled "run time (milliseconds)". It looks like you may have meant "(seconds)".
The "setpoint/programmed voltage" line has some major spikes and appears to step about once per "millisecond" (second?).
The blue line is not labeled at all. There is an axis labeled "PID/output (a.u.)" What is an "a.u." in this context?!?
Graph"No3":
the X axis runs from about 5000 to about 7750, is labeled "run time (milliseconds)"?!?
The "XI-390 sensor signal (Volts)" rises from 0.0V at 5000 mS to level out around 1.75V at about 6500 mS.
The "setpoint/programmed voltage" is slamming between 0V and 3.3V about 24 times per second.
The "PID/output" seems to be tracking the sensor signal but ranges from 395000 to 422000.
The graphs are not going to help without at least a better description of where the data comes from.
It appears that you are using a PID loop to get a specific ramp out of your sensor. I suspect you are changing the setpoint too infrequently so the jumps in Setpoint cause a spike in Output. If it were me, I'd change the Setpoint as frequently as possible.
Hi John, thanks for your feedback.
In Graph "No1" the X-Axis scale is wrong, Please see the updated Graph "No1". The Setpoint/programmed Voltage and what it does comes from the function we discussed earlier here "Ramp_Hold_Dump()" function. Also the "PID/Output" blue curve is the PID output, calculated and plotted but in arbitrary units, because I don't know what kind of measurement unit it has. So I wrote on graph a.u.= arbitrary units.
double setpoint_value_Ramp_Hold_Dump()
{
static unsigned long start_millis = 0;
unsigned long current_millis;
const unsigned long hold_1 = 1;
current_millis = millis();
if (current_millis - start_millis >= hold_1)
{
start_millis += hold_1;
if(RampingUp)
{
setpoint += 0.01;
if (setpoint > 3.279)
{
setpoint = 3.279;
RampingUp = false;
}
}
else // not RampingUp
{
setpoint -= 0.01;
if(setpoint < 0.0)
{
setpoint = 0.0;
RampingUp = true;
}
}
}
}
With Respect to "Graph No3"
The X axis runs from 5000 till 7000 and is in milliseconds, it is results of the millis() function printed in serial and always starts from 5000, no idea why.
The data comes from the Due, which reads out the analog signal from a sensor, then compares with the setpoint, which in our case is the setpoint_value_Ramp_Hold_Dump() function, and then calculates the amount of voltage it needs to output on both DAC's, which are connected to sensor as super tiny heaters.
Have you decided what you want the signal voltage curve to be?
yes, the signal voltage curve should be/look as a rectangle/square function, starting from 0.0 Volts, then 3.279 Volts, staying at 3,279 for 10 µicroseconds, then going down to 0.0 Volts. Also it is important to do it as fast as possible. One more question: is there a chance to achieve a simple PID cycle rates of 1-20 microseconds? on Arduino Due.