On the Nano 33 BLE Sense, I am trying to read most sensors in the same program. This worked well until I tried to read the APDS9960 sensor.
The ColorSensor.ino example that comes with the Arduino_APDS9960 library works fine with my Nano board. But as soon as I try to use that library in a more complex code, that loads a bunch of Sense libraries, it stops working. The r, g, b variables are 0, and half the time the APDS.readColor() call fails.
Not sure what else to do. The logic is correct - I am following the example from GitHub. The APDS library, when used alone in a program, works. But as soon as I try to move beyond "hello world", it does not work anymore.
Something's wrong somewhere, and I'm out of ideas for troubleshooting.
This is my current code:
#include <Arduino_HTS221.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_LSM9DS1.h>
#include <Arduino_APDS9960.h>
float temperature, humidity, pressure;
float acc_x, acc_y, acc_z;
float gyro_x, gyro_y, gyro_z;
float magnet_x, magnet_y, magnet_z;
char linebuf_atm[80];
char linebuf_acc[80];
char linebuf_gyro[80];
char linebuf_magnet[80];
char linebuf_color[80];
int r, g, b, w;
int ledState = LOW;
void setup() {
Serial.begin(9600);
delay(100);
HTS.begin();
delay(100);
BARO.begin();
delay(100);
// The baro sensor reads wrong first time after init
// so let's do a throw-away read here.
pressure = BARO.readPressure(MILLIBAR);
IMU.begin();
delay(100);
APDS.begin();
delay(100);
pinMode(LED_BUILTIN, OUTPUT);
// Let's allow things to settle down.
delay(1000);
}
void loop() {
Serial.println();
if (APDS.colorAvailable()) {
int r, g, b, w;
APDS.readColor(r, g, b, w);
} else {
Serial.println("APDS fail");
}
temperature = HTS.readTemperature();
humidity = HTS.readHumidity();
pressure = BARO.readPressure(MILLIBAR);
IMU.readAcceleration(acc_x, acc_y, acc_z);
IMU.readGyroscope(gyro_x, gyro_y, gyro_z);
IMU.readMagneticField(magnet_x, magnet_y, magnet_z);
sprintf(linebuf_atm,
"atmo:\t\ttemp=%.1fC\thumid=%.1f%%\tpress=%umb",
temperature, humidity, int(pressure));
sprintf(linebuf_acc,
"accel (g):\tx=%.3f\ty=%.3f\tz=%.3f",
acc_x, acc_y, acc_z);
sprintf(linebuf_gyro,
"gyro (deg/sec):\tx=%.3f\t\ty=%.3f\tz=%.3f",
gyro_x, gyro_y, gyro_z);
sprintf(linebuf_magnet,
"magneto (uT):\tx=%.2f\t\ty=%.2f\t\tz=%.2f",
magnet_x, magnet_y, magnet_z);
sprintf(linebuf_color,
"light:\t\tred=%u\t\tgreen=%u\tblue=%u\t\tall=%u",
r, g, b, w);
Serial.println(linebuf_atm);
Serial.println(linebuf_acc);
Serial.println(linebuf_gyro);
Serial.println(linebuf_magnet);
Serial.println(linebuf_color);
ledState = ledState ? LOW: HIGH;
digitalWrite(LED_BUILTIN, ledState);
delay(1000);
}