Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <SparkFun_APDS9960.h>
- // Pins
- #define APDS9960_INT 3 // Needs to be an interrupt pin
- // Constants
- #define PROX_INT_HIGH 100 // Proximity level for interrupt
- #define PROX_INT_LOW 0 // No far interrupt
- // Global variables
- SparkFun_APDS9960 apds = SparkFun_APDS9960();
- uint8_t proximity_data = 0;
- int isr_flag = 0;
- void setup() {
- pinMode(APDS9960_INT, INPUT);
- // Initialize Serial port
- Serial.begin(9600);
- Serial.println();
- Serial.println(F("---------------------------------------"));
- Serial.println(F("SparkFun APDS-9960 - ProximityInterrupt"));
- Serial.println(F("---------------------------------------"));
- // Initialize interrupt service routine
- attachInterrupt(1, 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!"));
- }
- apds.setGestureGain(GGAIN_1X); // ohne diese Zeile geht es nicht zuverlässig
- // 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!"));
- }
- // Set proximity interrupt thresholds
- if (!apds.setProximityIntLowThreshold(PROX_INT_LOW)) {
- Serial.println(F("Error writing low threshold"));
- }
- if (!apds.setProximityIntHighThreshold(PROX_INT_HIGH)) {
- Serial.println(F("Error writing high threshold"));
- }
- // Start running the APDS-9960 proximity sensor (interrupts)
- if (apds.enableProximitySensor(true)) {
- Serial.println(F("Proximity sensor is now running"));
- } else {
- Serial.println(F("Something went wrong during sensor init!"));
- }
- // Adjust the Proximity sensor gain
- if (!apds.setProximityGain(PGAIN_4X)) {
- Serial.println(F("Something went wrong trying to set PGAIN"));
- }
- }
- void loop() {
- // If interrupt occurs, print out the proximity level
- if (isr_flag == 1) {
- detachInterrupt(1);
- handleGesture();
- // Read proximity level and print it out
- if (!apds.readProximity(proximity_data)) {
- Serial.println("Error reading proximity value");
- } else {
- Serial.print("Proximity detected! Level: ");
- Serial.println(proximity_data);
- }
- // Reset flag and clear APDS-9960 interrupt (IMPORTANT!)
- if (!apds.clearProximityInt()) {
- Serial.println("Error clearing interrupt");
- }
- isr_flag = 0;
- attachInterrupt(1, 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");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement