1

Topic: Arduino statement constrain(ledPinVal, 0, 255); doesn't work on Remote

With the up/down button to increase/degrease a value within the limit 0-255, the Arduino statement

constrain(ledPinVal, 0, 255); doesn't limit the value to 255. It keeps going up >255 and <0 as well.

But this statement

if (ledPinVal <= 0 || ledPinVal >= 255) {
  ledPinVal=255;//works.

Same for

if (ledPinVal <= 0 || ledPinVal >= 255) {
  ledPinVal=0;//works

2

Re: Arduino statement constrain(ledPinVal, 0, 255); doesn't work on Remote

Hello

constrain doesn't modify the variable that you pass in it, instead it returns a new value, so must be used like this:

ledPinVal = constrain( ledPinVal, 0, 255 );

Or, using if statement:

if ( ledPinVal < 0 )
{
  ledPinVal = 0;
}
else if ( ledPinVal > 255 )
{
  ledPinVal = 255;
}

3

Re: Arduino statement constrain(ledPinVal, 0, 255); doesn't work on Remote

Thank you for your prompt reply. Yes, it works. Using constrain as suggested by you saved me a lot a coding. I love your RemoteXY. I have tried the MIT App inventor, Blynk and RoboRemo, but RemoteXY takes the GOLD. Thanks again from Sydney, Australia.