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