variable +/- a percentage?

I am still quite new to programming.

I have a variable I am using called setPoint. however I want this set point to be a range of numbers rather than a rigid number. so is there a way to set a variable to +/- 10 % or a range of numbers..

I know this is not correct syntax but i am trying to convey and idea..the result i am looking for is

int setPoint = 700 +/-%

i dont know how to accomplish this

i appreciate the help.

is there a way to set a variable to +/- 10 % or a range of numbers..

No. A variable can only have one value at a point in time. You can, however, test whether a variable is within a given range of values.

What is it that you are trying to do ?

thanks UKHeliBob

I am controlling high power leds. I am trying to add a temperature control loop. the loop is fast and depending on conditions can cause unwanted flicker. I know there are several ways to do this.

so.. I have a simple temp sensor resistor divider that i am reading and testing against.

i am thinking to a set point that is +/- 10 % or like you said testing to see if the read from the sensor is within +/- % of the setPoint.

i am guessing that is the way to go?

how do i test if a number is between this and this? in my case 694 and 707 ?

I know this is simple for some but im new and simple minded.

thanks for the help

You can have two variables, one to hold the setpoint and another to hold the percentage or fractional range, or use them to store the upper and lower limit.

how do i test if a number is between this and this? in my case 694 and 707 ?
I know this is simple for some but im new and simple minded.

Something like this

if (x >= 694 && x <= 707) {
   //   within setPoint acceptable range
   ...
} else {
   // outside range
   ...
}

Or better with non hard coded values within the business logic - define that at the top of your program

const unsigned int setPoint = 700;
const float acceptableRange = 0.1; // +/- 10%
const unsigned int minPoint = (1.0 - acceptableRange) * setPoint ;
const unsigned int maxPoint = (1.0 + acceptableRange) * setPoint ;

....

if (x >= minPoint && x <= maxPoint) {
   //   within setPoint acceptable range
   ...
} else {
   // outside range
   ...
}

And best (all is relative :slight_smile: ) is to go with a function

const unsigned int setPoint = 700;
const float acceptableRange = 0.1; // +/- 10%
const unsigned int minPoint = (1.0 - acceptableRange) * setPoint ;
const unsigned int maxPoint = (1.0 + acceptableRange) * setPoint ;

boolean isInRange(unsigned int val)
{
   return (val >= minPoint && val <= maxPoint);
} 

...

   if (isInRange(x)) {
      //   within setPoint acceptable range
      ...
   } else {
      // outside range
     ...
   }

Examine the following code to see if it fits your requirement:

struct iint {
    int value;
    int range; //in percentage
    bool operator== (const int r) {
        int percent_value = value * range / 100;
        return (r >= (value - percent_value)) && (r <= (value + percent_value));
    }
};

void setup()
{
    Serial.begin(9600);

    iint n;
    n.value = 700;
    n.range = 10;

    if (n == 629) { //will print not equal since 700 +- 10% = [630 - 770]
        Serial.println("equals");
    } else {
        Serial.println("not equals");
    }
}

Note: beware of integer math!

something like this?

int minSet = 964; 
int maxSet = 707;

void loop(){
 
 int sensVal = analogRead(0);
     
     if ((sensVal >= minSet) && (sensVal <= maxSet)){

     }


}

arduino_new:
Note: beware of integer math!

Yes - this calculation will kill you pretty quickly on a UNO or similar 16 bit for a int architecture if value is above 3277 and range is 10 for example.... value * range / 100
I would writevalue * range / 100.0 to be on the safer side and tell the compiler to perform the math using float instead of int and cast the result

(that being said nice usage of OOP - but probably over the head of newbies)

wow..lots of answers.. thanks

you guys rock.

I had been looking for a way to make a variable = a number +/- a percentage. once UKHeliBob
replied and said nope variables can only hold one value. I considered using an array.. like

is a value = a number in this array.

then thought more about the and thought about testing for between this and this and by the time i had figured out that is probably the way to go,, all these answers..

you guys are awesome thanks