xsharp.eu • [OT] Help needed with Arduino-style "C"
Page 1 of 2

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

Posted: Sun Oct 27, 2019 8:28 pm
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?

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

Posted: Sun Oct 27, 2019 9:21 pm
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)

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

Posted: Sun Oct 27, 2019 9:32 pm
by FFF
I have to tinker only rarely (> years) with this, so i better wrote it down for reference ;)
Thx, Johan!

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

Posted: Sun Oct 27, 2019 9:41 pm
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>

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

Posted: Mon Oct 28, 2019 8:08 am
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.

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

Posted: Mon Oct 28, 2019 8:18 am
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

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

Posted: Mon Oct 28, 2019 8:57 am
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

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

Posted: Mon Oct 28, 2019 10:08 am
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!

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

Posted: Mon Oct 28, 2019 11:54 am
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...

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

Posted: Mon Oct 28, 2019 11:54 am
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...