Advertisement
Guest User

Untitled

a guest
Mar 4th, 2023
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <SparkFun_APDS9960.h>
  3.  
  4. // Pins
  5. #define APDS9960_INT 3  // Needs to be an interrupt pin
  6.  
  7. // Constants
  8. #define PROX_INT_HIGH 100  // Proximity level for interrupt
  9. #define PROX_INT_LOW 0     // No far interrupt
  10.  
  11. // Global variables
  12. SparkFun_APDS9960 apds = SparkFun_APDS9960();
  13. uint8_t proximity_data = 0;
  14. int isr_flag = 0;
  15.  
  16. void setup() {
  17.  
  18.   pinMode(APDS9960_INT, INPUT);
  19.  
  20.   // Initialize Serial port
  21.   Serial.begin(9600);
  22.   Serial.println();
  23.   Serial.println(F("---------------------------------------"));
  24.   Serial.println(F("SparkFun APDS-9960 - ProximityInterrupt"));
  25.   Serial.println(F("---------------------------------------"));
  26.  
  27.   // Initialize interrupt service routine
  28.   attachInterrupt(1, interruptRoutine, FALLING);
  29.  
  30.   // Initialize APDS-9960 (configure I2C and initial values)
  31.   if (apds.init()) {
  32.     Serial.println(F("APDS-9960 initialization complete"));
  33.   } else {
  34.     Serial.println(F("Something went wrong during APDS-9960 init!"));
  35.   }
  36.   apds.setGestureGain(GGAIN_1X);  // ohne diese Zeile geht es nicht zuverlässig
  37.  
  38.   // Start running the APDS-9960 gesture sensor engine
  39.   if (apds.enableGestureSensor(true)) {
  40.     Serial.println(F("Gesture sensor is now running"));
  41.   } else {
  42.     Serial.println(F("Something went wrong during gesture sensor init!"));
  43.   }
  44.  
  45.   // Set proximity interrupt thresholds
  46.   if (!apds.setProximityIntLowThreshold(PROX_INT_LOW)) {
  47.     Serial.println(F("Error writing low threshold"));
  48.   }
  49.   if (!apds.setProximityIntHighThreshold(PROX_INT_HIGH)) {
  50.     Serial.println(F("Error writing high threshold"));
  51.   }
  52.  
  53.   // Start running the APDS-9960 proximity sensor (interrupts)
  54.   if (apds.enableProximitySensor(true)) {
  55.     Serial.println(F("Proximity sensor is now running"));
  56.   } else {
  57.     Serial.println(F("Something went wrong during sensor init!"));
  58.   }
  59.   // Adjust the Proximity sensor gain
  60.   if (!apds.setProximityGain(PGAIN_4X)) {
  61.     Serial.println(F("Something went wrong trying to set PGAIN"));
  62.   }
  63. }
  64.  
  65. void loop() {
  66.  
  67.   // If interrupt occurs, print out the proximity level
  68.   if (isr_flag == 1) {
  69.     detachInterrupt(1);
  70.     handleGesture();
  71.  
  72.     // Read proximity level and print it out
  73.     if (!apds.readProximity(proximity_data)) {
  74.       Serial.println("Error reading proximity value");
  75.     } else {
  76.       Serial.print("Proximity detected! Level: ");
  77.       Serial.println(proximity_data);
  78.     }
  79.  
  80.     // Reset flag and clear APDS-9960 interrupt (IMPORTANT!)
  81.     if (!apds.clearProximityInt()) {
  82.       Serial.println("Error clearing interrupt");
  83.     }
  84.  
  85.     isr_flag = 0;
  86.     attachInterrupt(1, interruptRoutine, FALLING);
  87.   }
  88. }
  89.  
  90. void interruptRoutine() {
  91.   isr_flag = 1;
  92. }
  93.  
  94. void handleGesture() {
  95.   if (apds.isGestureAvailable()) {
  96.     switch (apds.readGesture()) {
  97.       case DIR_UP:
  98.         Serial.println("UP");
  99.         break;
  100.       case DIR_DOWN:
  101.         Serial.println("DOWN");
  102.         break;
  103.       case DIR_LEFT:
  104.         Serial.println("LEFT");
  105.         break;
  106.       case DIR_RIGHT:
  107.         Serial.println("RIGHT");
  108.         break;
  109.       case DIR_NEAR:
  110.         Serial.println("NEAR");
  111.         break;
  112.       case DIR_FAR:
  113.         Serial.println("FAR");
  114.         break;
  115.       default:
  116.         Serial.println("NONE");
  117.     }
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement