Two digital sensors one Arduino

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
lonesoac0
 
Posts: 21
Joined: Sat Aug 19, 2017 3:58 pm

Two digital sensors one Arduino

Post by lonesoac0 »

Hello all,

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!
supposed_to_work.png
supposed_to_work.png (103.04 KiB) Viewed 131 times
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;
}

The wiring I am using for the "Adafruit LIS3DH Triple-Axis Accelerometer Breakout" is SPI wiring.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: Two digital sensors one Arduino

Post by adafruit_support_bill »

What processor are you using?

User avatar
lonesoac0
 
Posts: 21
Joined: Sat Aug 19, 2017 3:58 pm

Re: Two digital sensors one Arduino

Post by lonesoac0 »

Arduino Uno R3

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: Two digital sensors one Arduino

Post by adafruit_support_bill »

I suspect maybe a stack-crash. There is not a lot of code there, but the UNO is rather limited in RAM capacity.

Try adding the freeMemory code from the link below an make a call to it at the end of your setup. That will give us an idea of how much room you have to work with:
https://learn.adafruit.com/memories-of- ... ram-370031

User avatar
lonesoac0
 
Posts: 21
Joined: Sat Aug 19, 2017 3:58 pm

Re: Two digital sensors one Arduino

Post by lonesoac0 »

I am copying and pasting the code at the bottom of my setup function and this is what I am getting.
Screenshot 2022-11-30 093132.png
Screenshot 2022-11-30 093132.png (116.01 KiB) Viewed 118 times
Here is my new code:

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);
// Suggested Code
#ifdef __arm__
  // should use uinstd.h to define sbrk but Due causes a conflict
  extern "C" char* sbrk(int incr);
#else   // __ARM__
  extern char *__brkval;
#endif  // __arm__

  int freeMemory() {
    char top;
#ifdef __arm__
    return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
    return &top - __brkval;
#else   // __arm__
    return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif  // __arm__
  }
}


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;
}

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: Two digital sensors one Arduino

Post by adafruit_support_bill »

You need to copy the freeMemory function at the top level, then call it from your setup:

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

#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else  // __ARM__
extern char *__brkval;
#endif  // __arm__

int freeMemory() {
  char top;
#ifdef __arm__
  return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
  return &top - __brkval;
#else  // __arm__
  return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif  // __arm__
}

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);
  
  freeMemory();
}



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;
}

User avatar
lonesoac0
 
Posts: 21
Joined: Sat Aug 19, 2017 3:58 pm

Re: Two digital sensors one Arduino

Post by lonesoac0 »

I am not going to lie, I just gave up on using the Arduino and switched to the Metro M4. A few lines of code, some libraries, and connections and I am up and running.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: Two digital sensors one Arduino

Post by adafruit_support_bill »

Good to hear you have a working solution. The M4 has tons more memory than the UNO.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”