Go Down

Topic: Sensor Values (Read 313 times) previous topic - next topic

mvaughan19

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: [Select]
{
    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.

spatula

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

// 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: [Select]

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

marco_c

Have you tried to use map()

Code: [Select]
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: [Select]
percentage = (percentage/10) * 10;
or
precentage -= (percentage % 10);

to eliminate the last digit.
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy