[OT] Help needed with Arduino-style "C"

This forum is meant for anything you would like to share with other visitors
FFF
Posts: 1521
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

[OT] Help needed with Arduino-style "C"

Post by FFF »

Anyone Arduino savy here?
Variables:
int TuWas = LOW;
float tmin = 25.0;
float tmax = 28.0;


loop() calls:

Code: Select all

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  }
  else {
    Serial.print(tempC);
    Serial.print(" C");
  }

  if (tempC < tmin) {
    TuWas = HIGH;
  }

 if (tempC > tmax) {
    TuWas = LOW;
  }

...
}
Does anyone see a reason, why the first comparison tempC < tmin gets executed, but not the tempC > tmax?

I see in output the temperature values. Initially the first comparison is true, so TuWas is set to High. But later on the temp increases above tmax, but TuWas never gets set to Low. Why?

Any help greatly appreciated!
EDIT:
Solved. In the ellipsis (...) part i had:
If (TuWas = HIGH);
which seems to not only compare, but also assign :evil:
Replace with
If (TuWas)
all works.
While at it: If i need the equality compare, how can i avoid this side effect?
Regards
Karl
(on Win8.1/64, Xide32 2.19, X#2.19.0.2.)
User avatar
lumberjack
Posts: 723
Joined: Fri Sep 25, 2015 3:11 pm

[OT] Help needed with Arduino-style "C"

Post by lumberjack »

Karl,
Karl Faller wrote: EDIT:
Solved. In the ellipsis (...) part i had:
If (TuWas = HIGH);
which seems to not only compare, but also assign :evil:
Replace with
If (TuWas)
all works.
While at it: If i need the equality compare, how can i avoid this side effect?

Code: Select all

c/c# "=" means ":=" assign
c/c# "==" means "==" aka exactly equal.  c/c# does not have the same meaning as "=" of X#
if (TuWas == HIGH)
FFF
Posts: 1521
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

[OT] Help needed with Arduino-style "C"

Post by FFF »

I have to tinker only rarely (> years) with this, so i better wrote it down for reference ;)
Thx, Johan!
Regards
Karl
(on Win8.1/64, Xide32 2.19, X#2.19.0.2.)
User avatar
lumberjack
Posts: 723
Joined: Fri Sep 25, 2015 3:11 pm

[OT] Help needed with Arduino-style "C"

Post by lumberjack »

Karl,

Yes gets confusing when you then try VFP syntax mode, where = means ":=" and "==" or is it or...:lol:

Code: Select all

<VFP:On>
a = b // This is assign apparently
? b = c // This is compare apparently
a = b = c // This I think means a := (b == c)
<VFP:Off>
FFF
Posts: 1521
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

[OT] Help needed with Arduino-style "C"

Post by FFF »

;)
While ages ago, Foxpro was my very first encounter with xBase-style languages, i certainly will steer clear from these shallows...
But, still better than this awful brace-hell torture of C and its descendants.
Regards
Karl
(on Win8.1/64, Xide32 2.19, X#2.19.0.2.)
User avatar
lumberjack
Posts: 723
Joined: Fri Sep 25, 2015 3:11 pm

[OT] Help needed with Arduino-style "C"

Post by lumberjack »

FFF wrote:;)
While ages ago, Foxpro was my very first encounter with xBase-style languages, i certainly will steer clear from these shallows...
But, still better than this awful brace-hell torture of C and its descendants.
I agree, specially like in your example when they use:

Code: Select all

if (a > b) {
    a = b;
}
// instead of
if (a > b)
{
    a = b;
}
// or
if (a > b)
{   a = b; }
else
{    }
// or 
if (a > b)
{  a = b;
}
It is just not pleasing on the eye...

Code: Select all

IF a > b
  a := b
ENDIF
Terry
Posts: 306
Joined: Wed Jan 03, 2018 11:58 am

[OT] Help needed with Arduino-style "C"

Post by Terry »

Just one other point:

You are using a float in your error trap "=="
Doubles should not be used that way due to rounding errors.
The same may apply to floats. I don't know - just a guess.

Terry
User avatar
lumberjack
Posts: 723
Joined: Fri Sep 25, 2015 3:11 pm

[OT] Help needed with Arduino-style "C"

Post by lumberjack »

Hi Terry,
Terry wrote: You are using a float in your error trap "=="
Doubles should not be used that way due to rounding errors.
The same may apply to floats. I don't know - just a guess.
Good point!
FFF
Posts: 1521
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

[OT] Help needed with Arduino-style "C"

Post by FFF »

Well, that part is not my code, came with the sensor samples... I'm not quite sure, how internally the encode the readings, the output is "digital", and one can set the resolution. Possibly "-127.00" is only an equivalent for 0x0 ;)
BTW, 10 one wire temp-sensors, shipping included, for about 2.5 €. Interesting times we live in...
Regards
Karl
(on Win8.1/64, Xide32 2.19, X#2.19.0.2.)
FFF
Posts: 1521
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

[OT] Help needed with Arduino-style "C"

Post by FFF »

Well, that part is not my code, came with the sensor samples... I'm not quite sure, how internally they encode the readings, the output is "digital", and one can set the resolution. Possibly "-127.00" is only an equivalent for 0x0 ;)
BTW, 10 one wire temp-sensors, shipping included, for about 2.5 €. Interesting times we live in...
Regards
Karl
(on Win8.1/64, Xide32 2.19, X#2.19.0.2.)
Post Reply