If function not true, not working

Hello guys,

I have this code from this site https://www.gammon.com.au/forum/?id=11428

 if (myChannel.update()) {
    digitalWrite (LED_PIN, LOW);  // no error

    memcpy (
        buf,
        myChannel.getData(),
        myChannel.getLength()
    );

    if (buf[1] == 1) {
        Distance = buf[2];
    }
  }
if (!myChannel.update()) {
digitalWrite (LED_PIN, HIGH);  // turn on LED if error
  
}

When there is no serial connection, or there i a problem i want the build-in led to light on.

If i dont use the

if (!myChannel.update()) {
digitalWrite (LED_PIN, HIGH);  // turn on LED if error
  
}

The code works ok, but if i use the not true statement the build in led always stay on and no data are exchanged, any idea why the program just skips the if (myChannel.update() ?

Please post the complete program and tell us what you are trying to create.

And what Arduino you are using.

...R

I thought my goal is clear,

When there is no serial connection, or there i a problem i want the build-in led to light on.

I can send you the whole code but it is 320 lines and i think it would be even more confusing.

On the link i send (https://www.gammon.com.au/forum/?id=11428) in the Master example nick gammon does this exact same thing and it works.

byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);

digitalWrite (LED_PIN, received == 0);  // turn on LED if error

This works with the normal library RS485_protocol but it does not work with the RS485_non_blocking library.

The compiler runs an error 'recvMsg' was not declared in this scope

I looked on both libraries and recvMsg does not exist, how does it work with the one library and not the other

Here are the libraries files Nick-Gammon-RS485/ArduinoLibraries at master · Sthing/Nick-Gammon-RS485 · GitHub

What does it do if instead of 'if (!myChannel.update())' you use the rather simpler 'else'?

Steve

It does the same thing, it just ignores the if (myChannel.update())

I was wrong, i was looking in the RS485_protocol.cpp line 97-101 and i found this

byte recvMsg (AvailableCallback fAvailable,   // return available count
              ReadCallback fRead,             // read one byte
              byte * data,                    // buffer to receive into
              const byte length,              // maximum buffer size
              unsigned long timeout)          // milliseconds before timing out

So it checks for a timeout in the RS485_protocol library but not the RS485_non_blocking library.

Do i need to edit the library? or does he check for a timeout in a different way in the non_blocking library?

theomv:
I thought my goal is clear,

Now I am worried that there is some reason why you don't want to tell us what your project is.

I can send you the whole code but it is 320 lines and i think it would be even more confusing.

Please post the whole program - it should not be necessary to ask twice.

...R

or does he check for a timeout in a different way in the non_blocking library?

Obviously.