Hi,
can you please help me how to increase / decrease a menu item value NOT by just one, let's say by 100?
By one can be done with " value++" or "value--" but how chould I change this to change by 100?
How can I set limits for the value, not to change below 0 and upper than 5000 for example?
If a sample can be provided, that would be great!
Thanks for helping me!
value += increment;
And
value -= increment;
if (value < lowValue || value > highValue)
BulldogLowell:
value += increment;
And
value -= increment;
If you just change the sign of "increment", then value += increment;
is all you need.
thanks for the answers to all!
if I am right for example:
....
int increment = 100;
...
if ( action == decrease )
{
value -= increment;
if (value <= 0 || value >= 5000) increment = -increment;
}
else
{
value += increment;
if (value <= 0 || value >= 5000) increment = -increment;
}
...
Is it correct? I have found " increment = -increment;" in BulldogLowell's other answer in another post, but don't exactly understand.
BulldogLowell:
value += increment;
And
value -= increment;
if (value < lowValue || value > highValue)
I have found " increment = -increment;" in BulldogLowell's other answer in another post, but don't exactly understand.
Suppose increment is 5. It should be obvious that the statement is equivalent to increment = -5;, right?
If increment is -5, the statement is equivalent to increment = -(-5); which assigns +5 to increment.
So, whatever value was in increment is replaced by the same absolute value but with the opposite sign.
thank you Pauls! As reading your lines, it is clear ( and realised I should understood that earlier )
This is not correct in case, because I would like to have : in case reaching the limit ( 0 or 5000 for example) than it should not go under 0 or above 5000.
PaulS:
I have found " increment = -increment;" in BulldogLowell's other answer in another post, but don't exactly understand.
Suppose increment is 5. It should be obvious that the statement is equivalent to increment = -5;, right?
If increment is -5, the statement is equivalent to increment = -(-5); which assigns +5 to increment.
So, whatever value was in increment is replaced by the same absolute value but with the opposite sign.
vespapierre:
thanks for the answers to all!
if I am right for example:....
int increment = 100;
...
if ( action == decrease )
{
value -= increment;
if (value <= 0 || value >= 5000) increment = -increment;
}
else
{
value += increment;
if (value <= 0 || value >= 5000) increment = -increment;
}
...
Is it correct? I have found " increment = -increment;" in BulldogLowell's other answer in another post, but don't exactly understand. > BulldogLowell: > ``` > value += increment; > ``` > > > > And > > > > ``` > value -= increment; > ``` > > > > > > ``` > if (value < lowValue || value > highValue) > ```
a practical example...
const int myPWMpin = 9;
void setup()
{
pinMode(myPWMpin, OUTPUT);
}
void loop()
{
fadeMyLed();
}
void fadeMyLed()
{
static byte briteness = 0;
static int increment = 5;
static unsigned long lastUpdateTime;
if (millis() - lastUpdateTime > 10UL)
{
analogWrite(myPWMpin, briteness);
briteness += increment;
if (briteness <= 0 || briteness >= 255) increment = -increment;
lastUpdateTime = millis();
}
}
This is not correct in case, because I would like to have : in case reaching the limit ( 0 or 5000 for example) than it should not go under 0 or above 5000.
The change to increment needs to happen when the value gets to, or goes beyond, the end of the range. That is separate, though, from constrain()ing the value. (There's a hint for you). You can constrain the value using the constrain() function, or constrain it the same way. If it's less than the lower limit, set it to the lower limit. It it's greater than the upper limit, set it to the upper limit.
BulldogLowell,
thanks! I think in that case for example if you reach low limit, and you keep pressing decrease button it will start increase the value.
PaulS,
this is optimal
int increment = 100;
int value;
void setup()
value = constrain ( value, 0, 5000)
void menusomething()
if ( action == decrease )
{
value -= increment;
}
else
{
value += increment;
}
...
I think this is the one.
Thank you!
not there yet... constrain is a function, not a magic spell you cast over a variable:
value = constrain((value += increment), 0, 5000)
or you can do this:
value = min ((value+=increment), 5000);
with
value = max((value -= increment) , 0);
thanks for all the help!
It works, and it not works also increment is set to integer 100. Strange but it steps by 300.
It will be a problem somewhere else, but this is my fault.
what are you using to increment value?
it is doing only if i use constrain :
lowCO2 = constrain ((lowCO2 += co2increment), 0, 10000);
if i use this one, it is working ( but you know under 0 or above 10000 it begins from the first value....):
lowCO2 += co2increment;
if (lowCO2 <= 0 || lowCO2 >= 10000) co2increment = -co2increment;
lowCO2 and co2increment is also integer.
this is for setting values by keypad on LCD.
vespapierre:
thanks for all the help!
It works, and it not works alsoincrement is set to integer 100. Strange but it steps by 300.
It will be a problem somewhere else, but this is my fault.
I'd have to see your code, I guess
Thanks BulldogLowell,
attached. This is not too small, but you fill find it at the end. GoForSmoke helped me really really a lot in this.
So for now: co2 low and high values can be increased / decreased now by 300 instead of 100, and also therm and humid values can be increased / decreased by 2 ( it was 1, which was ok before constrain ).
smoke_version6_8.ino (29.7 KB)
vespapierre:
This is not too small, but you fill find it at the end. GoForSmoke helped me really really a lot in this.
So for now: co2 low and high values can be increased / decreased now by 300 instead of 100, and also therm and humid values can be increased / decreased by 2 ( it was 1, which was ok before constrain ).
you lost me on this... I don't understand " So for now: co2 low and high values can be increased / decreased now by 300 instead of 100, and also therm and humid values can be increased / decreased by 2 ( it was 1, which was ok before constrain )."
sorry for my english, trying to explain.
for example:
in case using: "lowCO2 -= co2increment;" value can be changed by 100 ( co2increment is 100 int ).
in case using: "lowCO2 = constrain ((lowCO2 -= co2increment), 0 ,10000);" value can be changed by 300...
also same for highCO2 variables.
in case using:"lowHumid--" value can be changed by 1.
in case using:"lowHumid = constrain((lowHumid--), 0, 100);" value can be changed by 2...
same for highHumid and lowTherm,highTerm variables.
I have tested this and get some unexpected (to me) as follows
void setup()
{
int x;
Serial.begin(115200);
x = 123;
x = constrain((x--), 0, 100);
Serial.println(x); //x = 100 as expected
x = 12;
x--; //decrement before constrain
x = constrain(x, 0, 100);
Serial.println(x); //x = 11 as expected
x = 12;
x = constrain((x--), 0, 100); //decrement in brackets
Serial.println(x); //x = 10 - why ?
x = 12;
x = constrain(x--, 0, 100);
Serial.println(x); //x = 10 - why ?
x = 12;
x = constrain(--x, 0, 100);
Serial.println(x); //x = 9 - why ?
}
void loop()
{
}
No doubt there is an explanation, but what is it ?
No doubt there is an explanation, but what is it ?
The usual - poor macro implementation#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
x = constrain((x--), 0, 100); //decrement in brackets
Serial.println(x); //x = 10 - why ?
Because constrain is not a function. It is a macro. In the macro, the argument is evaluated several times., causing it to be decremented several times.