Sensor Values

Hello All

Im working with a stretch sensor here and im trying to attach a percentage value to the analog value. I have done this (only part of the code). the problem im having is it will print all of the values 10%-100% rather than only printing the current value. I know that i need to create a range or some type only i forget what that is called in computer language... basically i need to create

if(sensorValue >800 but below 804) than print out 0%, and so forth for the rest 10%-100%. i need to put this into all of my if statements so they are no longer true.

any thoughts on the best way to do this?

 {
    if(sensorValue>800){ //You can change the number here to adjust the threshold.
      Serial.print("Straight - 0%");
    }

    //10 

    if(sensorValue>805){ //You can change the number here to adjust the threshold.
      Serial.print("Straight - 10%");
    }

    //20 

    if(sensorValue>810){ //You can change the number here to adjust the threshold.
      Serial.print("20%");
    }   

    //30  

    if(sensorValue>815){ //You can change the number here to adjust the threshold.
      Serial.print("30%");
    }

    //40 

    if(sensorValue>820){ //You can change the number here to adjust the threshold.
      Serial.print("40%");
    }

    //50 

    if(sensorValue>825){ //You can change the number here to adjust the threshold.
      Serial.print("50%");
    }   

    //60 

    if(sensorValue>830){ //You can change the number here to adjust the threshold.
      Serial.print("60%");
    }
    //70  

    if(sensorValue>835){ //You can change the number here to adjust the threshold.
      Serial.print("70%");

    }   

    //80  

    if(sensorValue>840){ //You can change the number here to adjust the threshold.
      Serial.print("80%");
    }

    //90 

    if(sensorValue>845){ //You can change the number here to adjust the threshold.
      Serial.print("90%");
    }

    //100  

    if(sensorValue>850){ //You can change the number here to adjust the threshold.
      Serial.print("100%");
    }

Thank you for your time.

The problem is that if your value is > 850 all the conditions will be true. You may change your code this way:

// longer, boring but unambiguous
if (sensorValue > 800 && sensorValue <= 805) { //You can change the number here to adjust the threshold.
  Serial.print("Straight - 0%");
}
else if (sensorValue > 805 && sensorValue <= 810) {
//
}

or this way:

// shorter, but depends on the right ordering of the ifs
if (sensorValue > 850) {
//...
} else if (sensorValue > 845) {
//...
} else if (sensorValue > 840) {
//...
}

Have you tried to use map()

percentage = map(variable, 800, 850, 0, 100);

of course, this assumes a linear relationship (ie, each value read from the analog port is 2% of full scale).

If you want to keep it a multiple of 10% then you could use

percentage = (percentage/10) * 10;
or
precentage -= (percentage % 10);

to eliminate the last digit.