Code executed when in setup() but not when in independent function

Hello,

I am trying to put some examples from the Pozyx library together and execute each of them according to an int transmitted from a computer over the serial port.

However, I can't have code executed when moved from setup() to an independant function. There's no issue at compile time and the function is called correctly because the codeSerial.println("Function basicTroubleshooting() executed."); is executed.

So what's wrong?

Here's the original code:

#include <Pozyx.h>
#include <Pozyx_definitions.h>
#include <Wire.h>


void setup(){
  Serial.begin(115200);

  if(Pozyx.begin() == POZYX_FAILURE){
    Serial.println("ERROR: Unable to connect to POZYX shield");
    Serial.println("Reset required");
    delay(100);
    abort();
  }

  // this performs device check locally.
  device_check(NULL);

  // uncomment and fill in own remote ID to check that device.
  // device_check(0x10);

  network_check_discovery();

}

void device_check(uint16_t remote_id){
  uint8_t data[5] = {0,0,0,0,0};

  if (remote_id == NULL){
    Pozyx.regRead(POZYX_WHO_AM_I, data, 5);
    Serial.println("local device:");
  }else{
    Pozyx.remoteRegRead(remote_id, POZYX_WHO_AM_I, data, 5);
    Serial.print("device 0x");
    Serial.println(remote_id, HEX);
  }

  Serial.print("who am i: ");
  Serial.println(data[0], HEX);
  Serial.print("firmware version: 0x");
  Serial.println(data[1], HEX);
  Serial.print("hardware version: 0x");
  Serial.println(data[2], HEX);
  Serial.print("self test result: 0b");
  Serial.println(data[3], BIN);
  Serial.print("error: 0x");
  Serial.println(data[4], HEX);
}


void network_check_discovery(){
  if( Pozyx.doDiscovery(POZYX_DISCOVERY_ALL_DEVICES) == POZYX_SUCCESS){
    uint8_t num_devices = 0;
    Pozyx.getDeviceListSize(&num_devices);
    Serial.print("Discovery found: ");
    Serial.print(num_devices);
    Serial.println(" device(s).");
    uint16_t tags[num_devices];
    if (num_devices == 0){
      Serial.println("FIN.");
      return;
    }
    Pozyx.getDeviceIds(tags, num_devices);

    for(int i = 0; i < num_devices; i++){
      Serial.print("0x");
      Serial.print(tags[i], HEX);
      if (i != num_devices - 1){
        Serial.print(", ");
      }
    }
    Serial.println();
  }
}

void loop() {

}

Here's the modified code:

// inclusions for Pozyx
#include <Pozyx.h>
#include <Pozyx_definitions.h>
#include <Wire.h>

int myCode = 0;

void setup() {
  // initialize serial:
  Serial.begin(115200);
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    myCode = Serial.parseInt();

    // look for the newline. That's the end of the code
    if (Serial.read() == '\n') {
      // constrain the values to 0 - 255
      myCode = constrain(myCode, 0, 255);

    // print the code back to serial for confirmation to sender
    Serial.print("Typed code is: ");
    Serial.println(myCode);
    }
  }

  // Execute a function according to the value of the received int if any
  switch(myCode){
    case 0:
    break;
    case 1: 
    Serial.println("Function started: basic troubleshooting.");
    basicTroubleshooting();  
    myCode = 0;
    break;
    default:
    Serial.println("Code not assigned to a function yet.");
    myCode = 0;
    break;
  }   

}

void basicTroubleshooting() {
  Serial.println("Function basicTroubleshooting() executed.");
  Serial.print("Pozyx.begin() results: ");
  int a = Serial.println(Pozyx.begin());
    if(a   == POZYX_FAILURE){
    Serial.println("ERROR: Unable to connect to POZYX shield");
    Serial.println("Reset required");
    delay(100);
    abort();

  // this performs device check locally.
  device_check(NULL);

  // uncomment and fill in own remote ID to check that device.
  // device_check(0x10);

  network_check_discovery();
  }
}

void device_check(uint16_t remote_id){
  uint8_t data[5] = {0,0,0,0,0};
  
  if (remote_id == NULL){
    Pozyx.regRead(POZYX_WHO_AM_I, data, 5);
    Serial.println("local device:");
  }else{
    Pozyx.remoteRegRead(remote_id, POZYX_WHO_AM_I, data, 5);
    Serial.print("device 0x");
    Serial.println(remote_id, HEX);
  }

  Serial.print("who am i: ");
  Serial.println(data[0], HEX);
  Serial.print("firmware version: 0x");
  Serial.println(data[1], HEX);
  Serial.print("hardware version: 0x");
  Serial.println(data[2], HEX);
  Serial.print("self test result: 0b");
  Serial.println(data[3], BIN);
  Serial.print("error: 0x");
  Serial.println(data[4], HEX);
}

void network_check_discovery(){
  if( Pozyx.doDiscovery(POZYX_DISCOVERY_ALL_DEVICES) == POZYX_SUCCESS){
    uint8_t num_devices = 0;
    Pozyx.getDeviceListSize(&num_devices);
    Serial.print("Discovery found: ");
    Serial.print(num_devices);
    Serial.println(" device(s).");
    uint16_t tags[num_devices];
    if (num_devices == 0){
      Serial.println("FIN.");
      return;
    }
    Pozyx.getDeviceIds(tags, num_devices);

    for(int i = 0; i < num_devices; i++){
      Serial.print("0x");
      Serial.print(tags[i], HEX);
      if (i != num_devices - 1){
        Serial.print(", ");
      }
    }
    Serial.println();
  }
}

do you see a difference between what's really being tested in  if(Pozyx.begin() == POZYX_FAILURE){ and

  int a = Serial.println(Pozyx.begin());
    if(a   == POZYX_FAILURE){

I do...

J-M-L:
do you see a difference between what's really being tested in

  if(Pozyx.begin() == POZYX_FAILURE){

and

 int a = Serial.println(Pozyx.begin());

if(a   == POZYX_FAILURE){



I do...

I indeed see the difference, but it's just a way to display if the Pozyx device was found. However does it change the results of the test within the if instruction?

What do you think Serial print returns?
RTFM early, RTFM often

Now I see. What about this?

int a  = Pozyx.begin();
Serial.println(a);
if(a   == POZYX_FAILURE){

What about this?

Are you saying you haven't tried it?
Why not?

I have tried this and it doesn't function:

Serial.print("Pozyx.begin() results: ");
int a = Pozyx.begin();
Serial.println(a);
if(a == POZYX_FAILURE){

Mayve useful to know: POZYX_FAILURE is defined like this in the Poxyx library:

#define POZYX_FAILURE               0x0

I can't compile the code in reply #6 so I can't comment on whether or not it functions.

I have tried this and it doesn't function

define "function"... what do you expect? what happens?

What does your code print for a?

The code prints 1 for a.

I have found the problem. It was a stupid brace not in the right place:

Bad code:

void basicTroubleshooting() {
  Serial.print("Pozyx.begin() results: ");
  int a = Pozyx.begin();
  Serial.println(a);
  if(a == POZYX_FAILURE){
    Serial.println("ERROR: Unable to connect to POZYX shield");
    Serial.println("Reset required");
    delay(100);
    abort();
    // this performs device check locally.
    device_check(NULL);

    // uncomment and fill in own remote ID to check that device.
    // device_check(0x10);

    network_check_discovery();
  Serial.print("Execution of basicTroubleshooting function completed.");
  } // <<<<<<<<<< WRONG! WRONG! WRONG!
}

Right code:

void basicTroubleshooting() {
  Serial.print("Pozyx.begin() results: ");
  int a = Pozyx.begin();
  Serial.println(a);
  if(a == POZYX_FAILURE){
    Serial.println("ERROR: Unable to connect to POZYX shield");
    Serial.println("Reset required");
    delay(100);
    abort();
  } // CLOSING BRACE AT THE RIGHT POSITION!!!
    // this performs device check locally.
    device_check(NULL);

    // uncomment and fill in own remote ID to check that device.
    // device_check(0x10);

    network_check_discovery();
  Serial.print("Execution of basicTroubleshooting function completed.");
}

If you put every stupid open curly brace on a line BY ITSELF, and put every stupid close curly brace on a line BY ITSELF, and use Tools + Auto Format often, it would be simple to see when the stupid curly braces are in the wrong place.

Curly braces can not be stupid. Programmers can be.