Page 1 of 1

BINARY HIGH & LOW

Posted: 10 Jul 2017 03:28
by Black Cat
Please refer to the code snippet below that come from the Sensor wall based Switch example.

Code: Select all

byte lastValue1 = HIGH;
byte lastValue2 = HIGH;

// the setup routine runs once when you press reset:
void setup() {
  pinMode(BTN_PIN1, INPUT_PULLUP);
  pinMode(BTN_PIN2, INPUT_PULLUP);
}

// the loop routine runs over and over again forever:
void loop() {
  byte currentValue1 = digitalRead(BTN_PIN1);
  byte currentValue2 = digitalRead(BTN_PIN2);
  
  // Button Down
  if (currentValue1 != lastValue1) {  // if state changes
    lastValue1 = currentValue1;       // save new state
    if (lastValue1 == LOW) {
      zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_OFF); // if button pressed - send switch OFF command
    }
I can see where the HIGH comes from, in this case byte lastValue1=HIGH) but later in the code there is reference to LOW, as it hasn't been defined is it assumed that it is NOT HIGH (!HIGH) or has it been an omission form the the code?
If so can the line of code be written as !HIGH ?

Re: BINARY HIGH & LOW

Posted: 10 Jul 2017 09:08
by A.Harrenberg
Hi,

HIGH and LOW are some standard defined values. At the moment I can't find where they are defined but you can just use them without any declaration.

In the example you posted the

Code: Select all

byte lastValue1=HIGH
is just the declaration of a byte-variable with the name lastValue1 and an initialization of the value with "HIGH".

Something like !HIGH might NOT work, as it is not a boolean value but a byte. Most likely LOW has the value "0" and HIGH has the value "1", so a !HIGH would mean that you try to do a !1...

BR,
Andreas.

Re: BINARY HIGH & LOW

Posted: 10 Jul 2017 10:05
by Black Cat
Thanks,
As HIGH & LOW do not to be defined, that answers the most important question.

I couldn't see where

Code: Select all

 lastValue1 == LOW
could be used as LOW was not defined, but as it does not need definition you have answered the question.

I thought that == was a Boolean expression, have I mistook it for something else?

Re: BINARY HIGH & LOW

Posted: 10 Jul 2017 10:52
by PoltoS
digitalRead can return LOW or HIGH. See http://z-uno.z-wave.me/Reference/digitalRead/

Re: BINARY HIGH & LOW

Posted: 10 Jul 2017 19:45
by A.Harrenberg
Hi BlackCat,

to be more precise, "==" is a logical function that returns a boolean value.

It just mean "equal to" and returns a TRUE if the two values are equal or return a FALSE if the two values are not equal.
So in your code example

Code: Select all

if (lastValue1 == LOW) {
means that the "==" operator compares the value of the variable lastValue1 and the pre-defined "LOW" value and then the returned boolean value is evaluated with the "if" statement.

BR,
Andreas.