I am trying to read data from the sensors of: "VCNL4010 proximity sensor" and "Adafruit LIS3DH Triple-Axis Accelerometer Breakout". When I collect data from the sensors individually, they work fine. When I try to combine the code, that is where things go wrong. I combine the code, upload it, and tap my accelerometer.
As you can see in the screenshot, the code tries to run and gets to line 65 and starts to print out "Click Detected" but it just just kind of stops and reloads the void setup! Here is the full code that I am trying to accomplish. The comments of: "// Distance sensor addition" denote the additions I have added.
Code: Select all
// Basic demo for tap/doubletap readings from Adafruit LIS3DH
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
// Distance sensor addition
#include "Adafruit_VCNL4010.h"
Adafruit_VCNL4010 vcnl;
// end addition
// Used for software SPI
#define LIS3DH_CLK 13
#define LIS3DH_MISO 12
#define LIS3DH_MOSI 11
// Used for hardware & software SPI
#define LIS3DH_CS 10
// software SPI
//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK);
// hardware SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS);
// I2C
//Adafruit_LIS3DH lis = Adafruit_LIS3DH();
// Adjust this number for the sensitivity of the 'click' force
// this strongly depend on the range! for 16G, try 5-10
// for 8G, try 10-20. for 4G try 20-40. for 2G try 40-80
#define CLICKTHRESHHOLD 80
void setup(void) {
#ifndef ESP8266
while (!Serial) yield(); // will pause Zero, Leonardo, etc until serial console opens
#endif
Serial.begin(9600);
Serial.println("Adafruit LIS3DH Tap Test!");
if (!lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1) yield();
}
Serial.println("LIS3DH found!");
lis.setRange(LIS3DH_RANGE_2_G); // 2, 4, 8 or 16 G!
Serial.print("Range = ");
Serial.print(2 << lis.getRange());
Serial.println("G");
// 0 = turn off click detection & interrupt
// 1 = single click only interrupt output
// 2 = double click only interrupt output, detect single click
// Adjust threshhold, higher numbers are less sensitive
lis.setClick(2, CLICKTHRESHHOLD);
delay(100);
}
void loop() {
uint8_t click = lis.getClick();
if (click == 0) return;
if (!(click & 0x30)) return;
Serial.print("Click detected (0x");
Serial.print(click, HEX);
Serial.print("): ");
if (click & 0x10) Serial.print(" single click");
if (click & 0x20) Serial.print(" double click");
// Distance sensor addition
Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
// end addition
Serial.println();
delay(100);
return;
}