Hello everyone,
I am currently trying to get the APDS9960 to work with a Teensy 4.0 microcontroller. I am using the Sparkfun Library and the the GestureTest Sketch as a basis. I’m a beginner when it comes to microcontrollers so I’m hoping it’s something obvious
Maybe important to note is that i already tried using the adafruit library for the sensor and everything worked. I would prefer to use the Sparkfun Library because it has the NEAR/FAR Gestures that I need for my project which the adafruit library doesn’t.
There are two problems with the Sparkfun Libary
-
the interrupt routine only get’s called once. So when I wave my hand in front of the sensor the flag gets changed, the gesture is handled, the flag gets changed back and then the routine won’t get called again.
-
the gesture handling doesn’t work even when i remove all the interrupt stuff and call handleGesture() in the Loop method. isGestureAvailable() is false at the beginning (which it should be) and when i wave my hand in front of the sensor it becomes true but the code goes straight for the default case in the switch which is NONE. After that isGestureAvailable() is stuck on true and I just get infinite NONEs in the console.
I don’t need to use interrupts that badly but I would especially appreciate help with the second Problem. I assume it’s a Teensy Problem but I don’t know what it is. If there are no solutions for both Problems then I guess I could use the Adafruit Library and manually add the NEAR/FAR gestures but I have no experience with changing libraries and coding so close to the hardware.
This is the code I’m using:
#include <SparkFun_APDS9960.h>
// Pins
#define APDS9960_INT 2 // Needs to be an interrupt pin
// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
void setup() {
// Set interrupt pin as input
pinMode(APDS9960_INT, INPUT);
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("SparkFun APDS-9960 - GestureTest"));
Serial.println(F("--------------------------------"));
// Initialize interrupt service routine
attachInterrupt(digitalPinToInterrupt(APDS9960_INT), interruptRoutine, FALLING);
// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// Start running the APDS-9960 gesture sensor engine
if ( apds.enableGestureSensor(true) ) {
Serial.println(F("Gesture sensor is now running"));
} else {
Serial.println(F("Something went wrong during gesture sensor init!"));
}
}
void loop() {
if( isr_flag == 1 ) {
Serial.println("Interrupt works");
Serial.println(isr_flag);
detachInterrupt(digitalPinToInterrupt(APDS9960_INT));
handleGesture();
isr_flag = 0;
Serial.println(isr_flag);
attachInterrupt(digitalPinToInterrupt(APDS9960_INT), interruptRoutine, FALLING);
}
}
void interruptRoutine() {
isr_flag = 1;
}
void handleGesture() {
if ( apds.isGestureAvailable() ) {
switch ( apds.readGesture() ) {
case DIR_UP:
Serial.println("UP");
break;
case DIR_DOWN:
Serial.println("DOWN");
break;
case DIR_LEFT:
Serial.println("LEFT");
break;
case DIR_RIGHT:
Serial.println("RIGHT");
break;
case DIR_NEAR:
Serial.println("NEAR");
break;
case DIR_FAR:
Serial.println("FAR");
break;
default:
Serial.println("NONE");
}
}
}
My console outputs are:
SparkFun APDS-9960 - GestureTest
APDS-9960 initialization complete
Gesture sensor is now running
Interrupt works
1
NONE
0
And nothing after that, no matter how many times I wave my hand.
Thanks in advance for any help.