Why won't my if statment work...?

Hey,

I'm trying to use a digital input as a modifier. I have if statements setup like this...

button = digitalRead(digital);
if (button == HIGH){
math = (0.125);
}
if (button == LOW){
math = (1);
}

Then I have the variable math in a formula....

sensorval = analogRead(sensor); //Polls the sensor for data
boost = ((((((sensorval*0.0048828)/5.1)-.04)/.00369)*math)+1); //converts sensorval into a usable PSI value

Problem is that it appears I cannot use 0.125 in the "Math =" statment. I always get a return of "1" which means that math is coming out as 0.

What's going on here?

I always get a return of "1"

Return from what? I don't see a function definition.

Sorry,

I get boost = 1 when I print it to the LCD. Showing a 1 means that math = 0. But i never stated that math = 0.

I've never figured out decimals with the Arduino. All I know is that I can never get them to work properly ;D!

I think it might round it or something when it tries to display.

Well it's not rounding, since 'sensorval' is a input from an analog input.

But either way it does seem that arduino is taking my 0.125 and making it 0.

The point I was trying to make is that it is impossible to diagnose the problem from what you posted. If you post the entire Sketch, you will get better answers.

Please use code tags (hash button above the edit window) when posting large blocks of code.

But I'll take a guess at the problem. How is math defined?

//includes the liquid crystal library
#include <LiquidCrystal.h>

int sensor = 0; //Sets the sensor input to pin 0
int boost = 0; //sets the boost to 0
int sensorval = 0; //sets sensor value to 0
int digital = 9;
int math = 0;
int button = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //sets the pins for the LCD
 
void setup() {
  lcd.begin(16,2); //sets the LCD to 16x2 row/coloumns
  pinMode(digital, INPUT); 
  lcd.noCursor(); //disables the cursor
  lcd.setCursor(0,0); //sets cursor to position
   lcd.print("Digital Gauge By"); //prints data to screen
  lcd.setCursor(3,1); //moves cursor to position
  lcd.print("Bob Smith"); //prints data to screen
  delay(3000); //waits 2 seconds for start up screen
  lcd.clear(); //clears the LCD
  lcd.setCursor(5,0); // moves the cursor
  lcd.print("Visit:"); // prints information to the screen
  lcd.setCursor(1,1); // moves the cursor
  lcd.print("  ");
  delay(4000); // waits 4 seconds
  
  // this is static data, it is left on the screen to reduce CPU usage
  lcd.clear();  // clears the screen
   lcd.setCursor(1,0); //sets the cursor position
  lcd.print("VNT15:"); //prints data to screen
  lcd.setCursor(11,0); //sets cursor position
  lcd.print("PSI"); //prints data to screen
  lcd.setCursor(0,1); // moves the cursor for diagnostics
 // lcd.setCursor(2,1); // moves the cursor
 // lcd.print("Sensor:"); //used for diagnostic purposes, hows voltage
 //lcd.print("EGT:"); // prints static data
   //lcd.setCursor(11,1); // moves the cursor
//  lcd.setCursor(13,1); // sets cursor for diagnostics
  //lcd.print((char)223); // prints the degree symbol to the screen
  //lcd.setCursor(12,1); // moves the cursor
 //lcd.print("C"); // prints the celcius
// lcd.print("V"); //used for diagnostic purposes, hows voltage
}

void loop() { // starts the boost sensor loop
   button = digitalRead(digital);
   if (button == HIGH){
     math = (0.125);
   }
   if (button == LOW){
     math = (1);
   }
  
  sensorval = analogRead(sensor); //Polls the sensor for data
  boost = ((((((sensorval*0.0048828)/5.1)-.04)/.00369)*math)+1);  //converts sensorval into a usable PSI value
 
if (boost < 10){ //This removes the extra places when boost falls below 10
lcd.setCursor(9,0); // moves cursor
lcd.print("  "); // removes the extra decimal place
}

  lcd.setCursor(8,0); //sets the cursor position
  lcd.print(boost); //prints boost data
  //lcd.setCursor(11,1);
  //lcd.print("  ");
  
 // lcd.setCursor(8,1); // this is a diagnostic line to show the input from the sensor
 // lcd.print(sensorval*0.004883);
  
  //lcd.setCursor(7,1); // moves the cursor
  //lcd.print("XXX"); //prints the value of the EGT
  
  delay(400);//slows the refresh rate of the loop, reduces ghosting

All I'm trying to do is have a button to swtich between units, instead of PSI, Kpa.

int math = 0;

If you set the variable 'math' to be an integer, it is pointless to write something like this:

math = 0.125;

Integer numbers don't have a fractional part per definition.

Please explain more by pointless.

I've set it to nothing and still get the same result.

There are many places in your code where you use floating point numbers to calculate stuff, and the result gets stored into an integer. Is that intentional?

The effect is a bit like pushing a square cake into a round form, you lose the important edges with the chocolate frosting.

Yes, they are.

I think you're missing the point :wink:

http://arduino.cc/en/Reference/Float

Guess what? It won't work that way. Generally when a float is cast to an int, it rounds to the next nearest whole integer - in this case, zero (0). You need to declare those variables as floats if you wish to perform floating point math with them. Your only other option is to use fixed-point math, which can be done with integers (and is quite fast), but the discussion on how to implement those on an Arduino (unless someone has written a fixed point math library? A quick google didn't turn up anything) would take up more room than we have here.

As good a place to start as any:

Also:

http://fabiensanglard.net/doomIphone/DoingItFast.htm

Also as a quick point. Don't you want:

   button = digitalRead(digital);
   if (button == HIGH){
     math = (0.125);
   }
   else {
     math = (1);
   }

I've tired using float and get some wonky numbers, like -9. Which is impossible since this sensor cannot read vacuum. :-/

I've tried the else statement, and yes it would work....if I could get the 0.125 in correctly. I've also tried putting math = 1/8; and I get the -9 value.

Just to be clear, this program operates 100% correctly without the if/math statements. This is nothing more than an addition to try to add a feature.

Alright I'm trying float again, and I seem to be close to an answer...

int sensor = 0; //Sets the sensor input to pin 0
int boost = 0; //sets the boost to 0
int sensorval = 0; //sets sensor value to 0
int digital = 9;
int button = 0;
float math;
 button = digitalRead(digital);
  if (button == HIGH){
     math = (float)1/8;
   }
   else{
     math = (float)1;
   }
  
  sensorval = analogRead(sensor); //Polls the sensor for data
  boost = ((((((sensorval*0.0048828)/5.1)-.04)/.00369)*(float)math)+1);  //converts sensorval into a usable PSI value

I do get a return that seems somewhat correct. But now my range is reduced. Instead of going from 0-37 PSI, I now only can read up to 32 PSI. I also still get -9 on math = 1. That might be an error in my calculation though.

Forgive the multiple posts....

I found why I get -9, so it is my formula. I'll have to tweak it or have other values equal something else all together.

But what I don't get is...

I'm now printing math to the display. Instead of being 0.125 it's only showing as 0.12. I can only assume this is why I lost some range in my values. (0-37) Why is my float only showing as 0.12?

By default, Arduino prints floating point using two decimal places. Arduino version 0018 adds the capability to specify the number of digits after the decimal point:
Serial.print( floatValue, digits);

Okay perfect, I'm using 17 still. Just upgraded and it works nicely.

I thank you ALL! :smiley:

In order to do math with fractional numbers, it would be very helpful to consistently use "float" variables (and constants.) I am surprised that your current calculation of "boost" works correctly. However, assuming that it DOES, how about the following:

int math; // leave math as in int...

   if (button == HIGH){
     math = 8;  //divisor!
   }
   if (button == LOW){
     math = (1);
   }
  
  sensorval = analogRead(sensor); //Polls the sensor for data
  boost = ((((((sensorval*0.0048828)/5.1)-.04)/.00369)/math)+1);  //converts sensorval into a usable PSI value