-1

I want to create an integer whose total value cannot go beyond a range of numbers when added to and subtracted from. For example, if the "total_value" started at 0 (of a total possible range between -5 and 5), and then, using a button that added 1 to the total value, I got to 5, if I was to press it again it would not go to six but either reset to 5 or block another input of +1.

How would I go about achieving something to this effect?

Liam

closed as off-topic by gre_gor, user31481, Michel Keijzers, KIIV, MatsK Jan 23 '18 at 15:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – gre_gor, Community, Michel Keijzers, KIIV, MatsK
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This is about Arduino, not a programming school. – user31481 Jan 23 '18 at 8:59
2

You are probably looking for the constrain() macro (an existing implantation of clamp logic, credit goes to @Edgar Bonet)

value = constrain(value + input, -5, 5);

If you get tired of writing constrain() you can even wrap it in a new type (which might be over-engeneering), producing something like following(code not tested)

template<typename T>
struct BoundValue {
    T value;
    const T vmin, vmax;
    //similar for other operators +,/,-=,...
    decltype(*this)& operator+=(T operand) { value = constrain(value + operand, vmin, vmax); return *this; }
    operator T() const { return value; }
    BoundValue(const T& vmin, const T& vmax) : vmin(vmin), vmax(vmax) {}
    BoundValue& operator=(const T& other) { value = constrain(other, vmin, vmax); return *this; }
};

and use like variable of its argument type

BoundValue<int> val(-5, 5);
val += 6;//val = 5
  • 1
    No need to write clamp(): you can use constrain(). – Edgar Bonet Jan 23 '18 at 8:52
  • Total overkill. – user31481 Jan 23 '18 at 8:55
  • @EdgarBonet good catch, kind of unusual name choice by Arduino though(at least for my computer graphics background) - no wonder I had trouble to finding it. – wondra Jan 23 '18 at 9:17
  • @LookAlterno kind of agree, there are people who love shiny code though (myself included), its interesting for fun projects. For serious ones not so much due to the other issue being its super-easy to introduce errors when not being careful with c++ constructors/operator overloading. The clamp/constraint is definitely the way to go though. – wondra Jan 23 '18 at 9:30
  • @wondra. You have to give an answer at the poster's level. If he can't deal with a normal boundary check, an answer with Template, references, operator overload and whatever is not going to work for him. NickGammon gave an answer more appropiate to the poster's habilities. – user31481 Jan 23 '18 at 9:41
1

For adding:

total_value = total_value + 1;
if (total_value > 5)
  total_value = 5;

And for subtracting:

total_value = total_value - 1;
if (total_value < -5)
  total_value = -5;

You may find a C or C++ tutorial very helpful.


Another method for adding:

if (total_value < 5)
  total_value++;

And for subtracting:

if (total_value > -5)
  total_value--;
  • Thank you for your response, I did try the first method before I posted but I must have mucked something up because it didn't work. – Liam Jan 23 '18 at 4:56
  • if (++total_value > 5) total_value = 5 and if (--total_value < -5 ) total_value = -5. The use of prefix operators can achieve the same effect and make the code more concise. – AJD Jan 23 '18 at 5:37
  • @AJD. Nick's way is safer. Using ++/-- inside expressions is going to give headaches sooner or later. – user31481 Jan 23 '18 at 8:59
0

You can not create a number with arbitrary upper and lower limits. It is common in the C language to create an integer comprised of 16 bits. Such an integer has a minimum of:

-(2^15) = -32768

...and maximum of:

(2^15) - 1 = +32767 

Instead, after pressing the positive button, consider testing if "total_value" is 5 and skipping adding 1 to "total_value" if TRUE. Conversely, after pressing the negative button, consider testing if "total_value" is -5 and skipping subtracting 1 from "total_value" if TRUE.

Not the answer you're looking for? Browse other questions tagged or ask your own question.