Loop breaking w Qwiic LED and Lidar

I am using the redboard qwiik, qwiic led stick, and lidar lite v4. I am new to arduino/C++ but have lots of python experience.

I am having one major issue and one minor one:

  1. Using the following code to change LED color based on distance from the lidar. At an random moment the loop is breaking with no error report (again I am a python guy, so not sure how to retrieve any errors). I was initially thinking I was updating the state of the LED too quickly, so I added colorState to avoid unneeded changes. But that didn’t resolve things.

Just wondering what the issue might be, and whether or not I can log any errors.

#include <Wire.h>
#include "Qwiic_LED_Stick.h"
#include "LIDARLite_v4LED.h"

LED LEDStick;
LIDARLite_v4LED myLIDAR;

int ledAddress = 0x23;
int lidarAddress =  0x62;
void setup() {
  Wire.begin();
  Serial.begin(115200);

  if (LEDStick.begin( ledAddress) == false) {
    Serial.println("Qwiic LED Stick failed to begin. Please check wiring and try again!");
    while (1);
  }

  if (myLIDAR.begin(lidarAddress) == false) {
    Serial.println("LIDAR did not acknowledge! Freezing.");
    while (1);
  }

  Serial.println("Qwiic LED Stick and LIDAR ready!");
}

int getColorState(float distance) {
  int colorState;

  if (distance < 0.25) {
    colorState = 1;
  }
  else if (distance >= 0.25 && distance < 0.5) {
    colorState = 2;
  }
  else if (distance >= 0.5 && distance < 1.0) {
    colorState = 3;
  }
  else {
    colorState = 4;
  }

  return colorState;
}


int colorState = 0;
void loop() {
  float distance = myLIDAR.getDistance() / 100.0; // Convert distance to meters

  // Print distance to Serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" m");
  
  // set initial color state
  int CurrentColorState = getColorState(distance);
  

  if (CurrentColorState != colorState){
    if (distance < 0.25) {
        LEDStick.setLEDColor(255, 0, 0); // Red
        colorState = 1;
      }
    else if (distance >= 0.25 && distance < 0.5) {
        LEDStick.setLEDColor(255, 255, 0); // Yellow
        colorState = 2;
      }
    else if (distance >= 0.5 && distance < 1.0) {
        LEDStick.setLEDColor(0, 255, 0); // Green
        colorState = 3;
      }
    else {
        LEDStick.LEDOff();
        colorState = 4;
      }
  }

  delay(100);  
}
  1. This is a minor one, but the ‘reset’ button on my redboard doesn’t work as expected, the 13 LED blinks blue, but it doesn’t seem to restart the script. Instead I need to unplug/plug. Any ideas why this might be? Or is this as expected?

Any ideas?

Unlike Python a loop will not break with an error code. On exit of a loop() it will just be restarted as it is running in a for() from main.cpp in one of the cores folder.

It might “hang” in case one of the routines that is called from loop() is not returning or in case of memory leak or buffer overrun etc.

Does your distance keep changing?

You could put in Serial.println() on different places to debug and see after which function it fails?

For debug try increasing the delay(100) to delay (1000)

That reset is not working either as it should is weird. Either your board has a problem but also check that your power supply strong enough. It could be the reason for random behaviour.

Both the LIDAR and the LED stick use quite a bit of power (especially the ELD stick). How are you powering them? Powering everything from the redboard +3.3V supply might be pushing your luck.