Go Down

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

#### XOIIO

#30
##### Jun 21, 2013, 06:41 pm
if I threw this line in to set the temperature
if (currentMillis - previousMillis > interval) {

then they would sync up.

#### Arrch

#31
##### Jun 21, 2013, 06:42 pm

then they would sync up.

Who would? Avoid using pronouns when describing issues.

#### XOIIO

#32
##### Jun 21, 2013, 06:45 pm
if I used the same delay to set lasttemp and check it they would both sync up.

I've tried setting the code like this which you would think would work fine, it sets lasttemp every 10 seconds, and on each run the current temperature is compared, however unless there is a drastic change (of .3 degrees or so) the light just stays on).

Code: [Select]
`   if ( (celsius >= lasttemp - 00.10) && (celsius <= lasttemp + 00.10) ) {    digitalWrite(13, HIGH);   }  else {    digitalWrite(13, LOW);  }    lcd.print(" C ");  lcd.setCursor(0, 1);  lcd.print(fahrenheit);  lcd.print(" F ");      if (currentMillis - previousMillis > interval) {  lasttemp = celsius;}  }`

#### XOIIO

#33
##### Jun 21, 2013, 06:48 pm
Also when it is climbing the light stays on which makes no sense, unless there's a problem when it's checking if the temperature is within + / - .10

#### Arrch

#34
##### Jun 21, 2013, 07:03 pm
By sampling the previous temperature at a different rate than the current temperature, you're making time a variable. Previously, time was a variable, but it took relatively the same amount of time for the loop to complete, so the variable time could be neglected.

This if statement:
Code: [Select]
`if ( (celsius >= lasttemp - 00.10) && (celsius <= lasttemp + 00.10) `
does not take into account how long it has been since lasttemp was sampled. If you want to calculate the derivative, you need to take into account time. The easiest way to do this, would be to every X amount of time, calculate the difference in temperature and call it delta. delta divided by X seconds will give you the rate of change (derivative). If its under a certain amount, then its stable.

#### Arrch

#35
##### Jun 21, 2013, 07:04 pm

Also when it is climbing the light stays on which makes no sense, unless there's a problem when it's checking if the temperature is within + / - .10

Because you're missing an important statement in the code copied from the blink without delay example. Without said statement, you're not doing things in intervals, you're implementing a time delay.

#### XOIIO

#36
##### Jun 21, 2013, 07:32 pm

By sampling the previous temperature at a different rate than the current temperature, you're making time a variable. Previously, time was a variable, but it took relatively the same amount of time for the loop to complete, so the variable time could be neglected.

This if statement:
Code: [Select]
`if ( (celsius >= lasttemp - 00.10) && (celsius <= lasttemp + 00.10) `
does not take into account how long it has been since lasttemp was sampled. If you want to calculate the derivative, you need to take into account time. The easiest way to do this, would be to every X amount of time, calculate the difference in temperature and call it delta. delta divided by X seconds will give you the rate of change (derivative). If its under a certain amount, then its stable.
[/quote

well, that sounds good, but difficult to figure out :/

I guess I have no choice but to try, although I doubt I'll figure it out today.

#### Arrch

#37
##### Jun 21, 2013, 07:36 pm

well, that sounds good, but difficult to figure out :/

It's actually not that difficult:

Code: [Select]
`If (its been X seconds since the lastTime){  set lastTime to Now  set difference to absolute value of thisValue minus lastValue  set derivative equal to difference divided by X  set lastReading to thisReading  check if the deriviative is within a certain threshold and do something based on it}`

#### XOIIO

#38
##### Jun 21, 2013, 07:53 pmLast Edit: Jun 21, 2013, 07:56 pm by XOIIO Reason: 1

well, that sounds good, but difficult to figure out :/

It's actually not that difficult:

Code: [Select]
`If (its been X seconds since the lastTime){  set lastTime to Now  set difference to absolute value of thisValue minus lastValue  set derivative equal to difference divided by X  set lastReading to thisReading  check if the deriviative is within a certain threshold and do something based on it}`

yeah these lines are stumping me, I'm not sure if it's because of fatigue or not, I do know that I've never dealt with this sort of stuff though

set difference to absolute value of thisValue minus lastValue
set derivative equal to difference divided by X

I guess I'm going to sleep and work on it tomorrow (which will actually be tonight), not sure if that will help much though.

#### Arrch

#39
##### Jun 21, 2013, 08:01 pm

yeah these lines are stumping me, I'm not sure if it's because of fatigue or not, I do know that I've never dealt with this sort of stuff though

set difference to absolute value of thisValue minus lastValue
set derivative equal to difference divided by X

difference and derivative are simply variables, and the rest of it is simple arithmetic.

#### XOIIO

#40
##### Jun 22, 2013, 08:02 am
hmm, making some progress, however I am still kind of stuck, at this point.

set derivative equal to difference divided by X

Does that mean that it should be divided by the amount of time, or something like that?
anyways here's hat I have, not much, once that middle line is solved the rest will be easy.

if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
delta = celsius - lasttemp;
}

#### XOIIO

#41
##### Jun 22, 2013, 10:37 am
hmm, not sure if this is getting closer, derivative is always 0.00

Code: [Select]
`  if (currentMillis - previousMillis > interval) {    previousMillis = currentMillis;    delta = celsius - lasttemp;    derivative = delta / interval;    celsius = currenttemp;    Serial.print(derivative);  }`

#### XOIIO

#42
##### Jun 22, 2013, 11:01 am
hmm, I thought this would work, it kind of is it seems, although when the temp was climbing it turned on once for no reason, same thing just happened as it was dropping. I think I it's not comparing the time properly or something.

Code: [Select]
`  if (currentMillis - previousMillis > interval) {    previousMillis = currentMillis;    delta = celsius - lasttemp;    derivative = delta / 5;    celsius = currenttemp;    if ((delta < 00.10) && (delta > -00.10)) {      digitalWrite(13, HIGH); }      else {        digitalWrite(13, LOW);      }  }`

#### XOIIO

#43
##### Jun 22, 2013, 11:04 am
I think part of the problem is that it only checks to turn the led on or off is at that interval, I'm not sure how to solve that.

#### XOIIO

#44
##### Jun 22, 2013, 11:17 am
ok, i refined it, now the only problem that is left seems to be that it doesn't turn the led off until it sets delta, I need to find a way to change that, maybe using a second if statement, I'm not sure.

Code: [Select]
`  if (currentMillis - previousMillis > interval) {    previousMillis = currentMillis;    delta = celsius - lasttemp;    derivative = delta / 5;  }  if ((delta < 00.05) && (delta > -00.05)) {    digitalWrite(13, HIGH);   }  else {    digitalWrite(13, LOW);  }`

Go Up

Please enter a valid email to subscribe