Pages: [1]   Go Down
Author Topic: Sensor Values  (Read 279 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?


Code:
{
    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.
Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is that if your value is > 850 all the conditions will be true. You may change your code this way:
Code:
// 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:
Code:
// shorter, but depends on the right ordering of the ifs
if (sensorValue > 850) {
//...
} else if (sensorValue > 845) {
//...
} else if (sensorValue > 840) {
//...
}
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1287
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried to use map()

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

Code:
percentage = (percentage/10) * 10;
or
precentage -= (percentage % 10);
to eliminate the last digit.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Pages: [1]   Go Up
Jump to: