arduino-libraries/Arduino_APDS9960

APDS lib does not work in a real-life situation

FlorinAndrei opened this issue · 2 comments

Arduino Nano 33 BLE Sense

This library works great with your ColorSensor.ino example. The sensor is initialized without errors, and then the colors are read and printed out just fine every second.

But then I tried a real-life scenario: I loaded several Sense libraries, accessing most sensors on the Nano 33 BLE Sense board, printing the values to Serial. In this case, the library does not work. The r, g, b variables are all 0.

It's the same logic. It's the same board. But as soon as I try something more complex than a hello world example, it does not work.

Code below:

#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);

  APDS.begin();
  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);

  pinMode(LED_BUILTIN, OUTPUT);

  // Let's allow things to settle down.
  delay(1000);
}

void loop() {
  if (APDS.colorAvailable()) {
    int r, g, b, w;
    APDS.readColor(r, g, b, w);
  }
  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();
  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);
}

If this pull request gets merged...

#6

...then this bug report can be considered solved.

fixed by #6