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:
- 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);
}
- 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?