/ Adafruit_Feather_Sense / feather_sense_sensor_demo / feather_sense_sensor_demo.ino
feather_sense_sensor_demo.ino
  1  // SPDX-FileCopyrightText: 2020 Carter Nelson for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  //
  5  #include <Adafruit_APDS9960.h>
  6  #include <Adafruit_BMP280.h>
  7  #include <Adafruit_LIS3MDL.h>
  8  #include <Adafruit_LSM6DS33.h>
  9  #include <Adafruit_SHT31.h>
 10  #include <Adafruit_Sensor.h>
 11  #include <PDM.h>
 12  
 13  Adafruit_APDS9960 apds9960; // proximity, light, color, gesture
 14  Adafruit_BMP280 bmp280;     // temperautre, barometric pressure
 15  Adafruit_LIS3MDL lis3mdl;   // magnetometer
 16  Adafruit_LSM6DS33 lsm6ds33; // accelerometer, gyroscope
 17  Adafruit_SHT31 sht30;       // humidity
 18  
 19  uint8_t proximity;
 20  uint16_t r, g, b, c;
 21  float temperature, pressure, altitude;
 22  float magnetic_x, magnetic_y, magnetic_z;
 23  float accel_x, accel_y, accel_z;
 24  float gyro_x, gyro_y, gyro_z;
 25  float humidity;
 26  int32_t mic;
 27  
 28  extern PDMClass PDM;
 29  short sampleBuffer[256];  // buffer to read samples into, each sample is 16-bits
 30  volatile int samplesRead; // number of samples read
 31  
 32  void setup(void) {
 33    Serial.begin(115200);
 34    // while (!Serial) delay(10);
 35    Serial.println("Feather Sense Sensor Demo");
 36  
 37    // initialize the sensors
 38    apds9960.begin();
 39    apds9960.enableProximity(true);
 40    apds9960.enableColor(true);
 41    bmp280.begin();
 42    lis3mdl.begin_I2C();
 43    lsm6ds33.begin_I2C();
 44    sht30.begin();
 45    PDM.onReceive(onPDMdata);
 46    PDM.begin(1, 16000);
 47  }
 48  
 49  void loop(void) {
 50    proximity = apds9960.readProximity();
 51    while (!apds9960.colorDataReady()) {
 52      delay(5);
 53    }
 54    apds9960.getColorData(&r, &g, &b, &c);
 55  
 56    temperature = bmp280.readTemperature();
 57    pressure = bmp280.readPressure();
 58    altitude = bmp280.readAltitude(1013.25);
 59  
 60    lis3mdl.read();
 61    magnetic_x = lis3mdl.x;
 62    magnetic_y = lis3mdl.y;
 63    magnetic_z = lis3mdl.z;
 64  
 65    sensors_event_t accel;
 66    sensors_event_t gyro;
 67    sensors_event_t temp;
 68    lsm6ds33.getEvent(&accel, &gyro, &temp);
 69    accel_x = accel.acceleration.x;
 70    accel_y = accel.acceleration.y;
 71    accel_z = accel.acceleration.z;
 72    gyro_x = gyro.gyro.x;
 73    gyro_y = gyro.gyro.y;
 74    gyro_z = gyro.gyro.z;
 75  
 76    humidity = sht30.readHumidity();
 77  
 78    samplesRead = 0;
 79    mic = getPDMwave(4000);
 80  
 81    Serial.println("\nFeather Sense Sensor Demo");
 82    Serial.println("---------------------------------------------");
 83    Serial.print("Proximity: ");
 84    Serial.println(apds9960.readProximity());
 85    Serial.print("Red: ");
 86    Serial.print(r);
 87    Serial.print(" Green: ");
 88    Serial.print(g);
 89    Serial.print(" Blue :");
 90    Serial.print(b);
 91    Serial.print(" Clear: ");
 92    Serial.println(c);
 93    Serial.print("Temperature: ");
 94    Serial.print(temperature);
 95    Serial.println(" C");
 96    Serial.print("Barometric pressure: ");
 97    Serial.println(pressure);
 98    Serial.print("Altitude: ");
 99    Serial.print(altitude);
100    Serial.println(" m");
101    Serial.print("Magnetic: ");
102    Serial.print(magnetic_x);
103    Serial.print(" ");
104    Serial.print(magnetic_y);
105    Serial.print(" ");
106    Serial.print(magnetic_z);
107    Serial.println(" uTesla");
108    Serial.print("Acceleration: ");
109    Serial.print(accel_x);
110    Serial.print(" ");
111    Serial.print(accel_y);
112    Serial.print(" ");
113    Serial.print(accel_z);
114    Serial.println(" m/s^2");
115    Serial.print("Gyro: ");
116    Serial.print(gyro_x);
117    Serial.print(" ");
118    Serial.print(gyro_y);
119    Serial.print(" ");
120    Serial.print(gyro_z);
121    Serial.println(" dps");
122    Serial.print("Humidity: ");
123    Serial.print(humidity);
124    Serial.println(" %");
125    Serial.print("Mic: ");
126    Serial.println(mic);
127    delay(300);
128  }
129  
130  /*****************************************************************/
131  int32_t getPDMwave(int32_t samples) {
132    short minwave = 30000;
133    short maxwave = -30000;
134  
135    while (samples > 0) {
136      if (!samplesRead) {
137        yield();
138        continue;
139      }
140      for (int i = 0; i < samplesRead; i++) {
141        minwave = min(sampleBuffer[i], minwave);
142        maxwave = max(sampleBuffer[i], maxwave);
143        samples--;
144      }
145      // clear the read count
146      samplesRead = 0;
147    }
148    return maxwave - minwave;
149  }
150  
151  void onPDMdata() {
152    // query the number of bytes available
153    int bytesAvailable = PDM.available();
154  
155    // read into the sample buffer
156    PDM.read(sampleBuffer, bytesAvailable);
157  
158    // 16-bit, 2 bytes per sample
159    samplesRead = bytesAvailable / 2;
160  }