Go Down

### Topic: How would I make this if statement? (if somehting is within x amount?) (Read 9003 times)previous topic - next topic

#### XOIIO

##### Jun 21, 2013, 03:49 pm
I was just wondering how I would make an if statement like this, for measuring temperature, I want an indicator when it has stabilized, so if the temperature is withing + or - .20, then the if statement would continue. How would I do this? I know that I'll need to set a variable that changes so basically it would be if current temp is within plus or minus .20 of the last temp then it would do whatever I needed.

#### AWOL

#1
##### Jun 21, 2013, 04:08 pm
Think about the arithmetic you have to perform to make this decision yourself.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### XOIIO

#2
##### Jun 21, 2013, 04:13 pm

Think about the arithmetic you have to perform to make this decision yourself.

well I know how to do it with more than one line, I was just hoping there was a way to do it all in one if statement, instead of two like this.

if (celsius == lasttemp + .20){
if (celsius == lasttemp - .20){
digitalWrite(13, HIGH);
else {
digitalWrite(13, LOW);
}
}
}

#### JB_AU

#3
##### Jun 21, 2013, 04:15 pm
Amateurs built the ark, Professionals built the Titanic

#### XOIIO

#4
##### Jun 21, 2013, 04:16 pm
whoops, that should actually be this.

if (celsius == lasttemp + .20) {
if (celsius == lasttemp - .20) {
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
}

I'll check out boolean

#### Arrch

#5
##### Jun 21, 2013, 04:17 pm
Code: [Select]
`  if (celsius == lasttemp + .20){    if (celsius == lasttemp - .20){`

How can celcius be equal to two different values at the same time?

I think you're looking for something like this:

Code: [Select]
`if ( (celcius >= lasttemp - 0.20) && (celcius <= lasttemp + 0.20) )`

#### AWOL

#6
##### Jun 21, 2013, 04:17 pm
You could use abs
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### XOIIO

#7
##### Jun 21, 2013, 04:22 pm

Code: [Select]
`  if (celsius == lasttemp + .20){    if (celsius == lasttemp - .20){`

How can celcius be equal to two different values at the same time?

I think you're looking for something like this:

Code: [Select]
`if ( (celcius >= lasttemp - 0.20) && (celcius <= lasttemp + 0.20) )`

Ahh, that does make more sense, I guess that's what you get for staying upt oo long, thank you.

Also, I didn't know about that && thing, that's gonna be usefull.

#### AWOL

#8
##### Jun 21, 2013, 04:23 pm
Quote
Also, I didn't know about that && thing, that's gonna be usefull.

So is abs.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### XOIIO

#9
##### Jun 21, 2013, 04:28 pm
hmm, now I've got to work on scripting this right. If I have this section at the start of the script then it doesn't turn the LED on. If I have it at the end the LED stays on permanently. hmmm

if ( (celsius >= lasttemp - 0.2) && (celsius <= lasttemp + 0.2) ) {
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}

#### Arrch

#10
##### Jun 21, 2013, 04:33 pm

hmm, now I've got to work on scripting this right. If I have this section at the start of the script then it doesn't turn the LED on. If I have it at the end the LED stays on permanently. hmmm

Start of what? End of What? All you've posted is snippets.

#### XOIIO

#11
##### Jun 21, 2013, 04:38 pm

hmm, now I've got to work on scripting this right. If I have this section at the start of the script then it doesn't turn the LED on. If I have it at the end the LED stays on permanently. hmmm

Start of what? End of What? All you've posted is snippets.

ahh true. Here's the whole thing.

Code: [Select]
`#include <OneWire.h>#include <LiquidCrystal.h>OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)LiquidCrystal lcd(6, 7, 5, 4, 3, 2);float lasttemp;void setup(void) {  pinMode(13, OUTPUT);  Serial.begin(9600);  lcd.begin(8, 2);  lcd.setCursor(0,0);  lcd.print("STARTING");  lcd.setCursor(0, 1);  lcd.print("STARTING");  delay(1000);  lcd.clear();}void loop(void) {  byte i;  byte present = 0;  byte type_s;  byte data[12];  byte addr[8];  float celsius, fahrenheit;  if ( !ds.search(addr)) {    Serial.println("No more addresses.");    Serial.println();    ds.reset_search();    delay(250);    return;  }  Serial.print("ROM =");  for( i = 0; i < 8; i++) {    Serial.write(' ');    Serial.print(addr[i], HEX);  }  if (OneWire::crc8(addr, 7) != addr[7]) {    Serial.println("CRC is not valid!");    return;  }  Serial.println();  // the first ROM byte indicates which chip  switch (addr[0]) {  case 0x10:    Serial.println("  Chip = DS18S20");  // or old DS1820    type_s = 1;    break;  case 0x28:    Serial.println("  Chip = DS18B20");    type_s = 0;    break;  case 0x22:    Serial.println("  Chip = DS1822");    type_s = 0;    break;  default:    Serial.println("Device is not a DS18x20 family device.");    return;  }   ds.reset();  ds.select(addr);  ds.write(0x44, 1);        // start conversion, with parasite power on at the end  delay(1000);     // maybe 750ms is enough, maybe not  // we might do a ds.depower() here, but the reset will take care of it.  present = ds.reset();  ds.select(addr);      ds.write(0xBE);         // Read Scratchpad  Serial.print("  Data = ");  Serial.print(present, HEX);  Serial.print(" ");  for ( i = 0; i < 9; i++) {           // we need 9 bytes    data[i] = ds.read();    Serial.print(data[i], HEX);    Serial.print(" ");  }  Serial.print(" CRC=");  Serial.print(OneWire::crc8(data, 8), HEX);  Serial.println();  // Convert the data to actual temperature  // because the result is a 16 bit signed integer, it should  // be stored to an "int16_t" type, which is always 16 bits  // even when compiled on a 32 bit processor.  int16_t raw = (data[1] << 8) | data[0];  if (type_s) {    raw = raw << 3; // 9 bit resolution default    if (data[7] == 0x10) {      // "count remain" gives full 12 bit resolution      raw = (raw & 0xFFF0) + 12 - data[6];    }  }   else {    byte cfg = (data[4] & 0x60);    // at lower res, the low bits are undefined, so let's zero them    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms    //// default is 12 bit resolution, 750 ms conversion time  }    celsius = (float)raw / 16.0;  fahrenheit = celsius * 1.8 + 32.0;  Serial.print("  Temperature = ");  Serial.print(celsius);  Serial.print(" Celsius, ");  Serial.print(fahrenheit);  Serial.println(" Fahrenheit");  lasttemp = celsius;  lcd.setCursor(0, 0);  lcd.print(celsius);  lcd.print(" C ");  lcd.setCursor(0, 1);  lcd.print(fahrenheit);  lcd.print(" F ");  Serial.print(lasttemp);  Serial.println();      if ( (celsius >= lasttemp - 0.2) && (celsius <= lasttemp + 0.2) ) {      digitalWrite(13, HIGH); }      else {        digitalWrite(13, LOW);      }}`

I've tried using a variable to set the led state, but that got the same result.

I kind of know why it's doing it but I can't figure out a solution.

#### XOIIO

#12
##### Jun 21, 2013, 04:39 pmLast Edit: Jun 21, 2013, 04:45 pm by XOIIO Reason: 1
oh wait, maybe if I throw that whole thing into a while statement it will work.

hmm, well that didn't work, I'll try another way but not sure if that will work either.

#### Arrch

#13
##### Jun 21, 2013, 04:46 pm
Code: [Select]
`  celsius = (float)raw / 16.0;  ...  lasttemp = celsius;  ...    if ( (celsius >= lasttemp - 0.2) && (celsius <= lasttemp + 0.2) ) {      digitalWrite(13, HIGH); }      else {        digitalWrite(13, LOW);      }}`

At the time that it gets to the if statement, lasttemp will be equal to celcius. That means that celcius - lasttemp will be equal to 0 every time. 0 is between -0.2 and 0.2, so how can you expect anything other than the if statement always being true?

#### XOIIO

#14
##### Jun 21, 2013, 04:48 pm

Code: [Select]
`  celsius = (float)raw / 16.0;  ...  lasttemp = celsius;  ...    if ( (celsius >= lasttemp - 0.2) && (celsius <= lasttemp + 0.2) ) {      digitalWrite(13, HIGH); }      else {        digitalWrite(13, LOW);      }}`

Yeah, the problem is that that is when the Celsius actually has a value. I've tried putting this section at the top of the loop, but it doesn't do anything since it has been reset.

Also, that while loop theory didn't pan out.

At the time that it gets to the if statement, lasttemp will be equal to celcius. That means that celcius - lasttemp will be equal to 0 every time. 0 is between -0.2 and 0.2, so how can you expect anything other than the if statement always being true?

Go Up