user41512: 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

user10794: 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--;


user41617: 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


41512: <p>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.</p>

<p>How would I go about achieving something to this effect?</p>

<p>Liam</p>
 10794: <p>For adding:</p>

<pre><code>total_value = total_value + 1;
if (total_value &gt; 5)
  total_value = 5;
</code></pre>

<p>And for subtracting:</p>

<pre class="lang-c++ prettyprint-override"><code>total_value = total_value - 1;
if (total_value &lt; -5)
  total_value = -5;
</code></pre>

<p>You may find a C or C++ tutorial very helpful.</p>

<hr>

<p>Another method for adding:</p>

<pre><code>if (total_value &lt; 5)
  total_value++;
</code></pre>

<p>And for subtracting:</p>

<pre class="lang-c++ prettyprint-override"><code>if (total_value &gt; -5)
  total_value--;
</code></pre>
 41617: <p>You are probably looking for the <a href="https://www.arduino.cc/reference/en/language/functions/math/constrain/" rel="nofollow noreferrer">constrain()</a> macro (an existing implantation of clamp logic, credit goes to @Edgar Bonet)</p>

<pre><code>value = constrain(value + input, -5, 5);
</code></pre>

<hr>

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

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

<p>and use like variable of its argument type</p>

<pre><code>BoundValue&lt;int&gt; val(-5, 5);
val += 6;//val = 5
</code></pre>
