Advertisement
Guest User

APDS-9930

a guest
Dec 9th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. /****************************************************************
  2. ProximityInterrupt.ino
  3. APDS-9930 Ambient light and proximity sensor
  4. Davide Depau
  5. December 11, 2015
  6. https://github.com/Davideddu/APDS9930
  7.  
  8. Shawn Hymel @ SparkFun Electronics
  9. October 24, 2014
  10. https://github.com/sparkfun/APDS-9930_RGB_and_Gesture_Sensor
  11.  
  12. Tests the proximity interrupt abilities of the APDS-9930.
  13. Configures the APDS-9930 over I2C and waits for an external
  14. interrupt based on high or low proximity conditions. Move your
  15. hand near the sensor and watch the LED on pin 13.
  16.  
  17. Hardware Connections:
  18.  
  19. IMPORTANT: The APDS-9930 can only accept 3.3V!
  20.  
  21. Arduino Pin APDS-9930 Board Function
  22.  
  23. 3.3V VCC Power
  24. GND GND Ground
  25. A4 SDA I2C Data
  26. A5 SCL I2C Clock
  27. 2 INT Interrupt
  28. 13 - LED
  29.  
  30. Resources:
  31. Include Wire.h and APDS9930.h
  32.  
  33. Development environment specifics:
  34. Written in Arduino 1.6.5
  35. Tested with Arduino Uno and Mega
  36.  
  37. This code is beerware; if you see me (or any other SparkFun
  38. employee) at the local, and you've found our code helpful, please
  39. buy us a round!
  40.  
  41. Distributed as-is; no warranty is given.
  42. ****************************************************************/
  43.  
  44. #define DUMP_REGS
  45.  
  46. #include <Wire.h>
  47. #include <APDS9930.h>
  48.  
  49. // Pins
  50. #define APDS9930_INT 2 // Needs to be an interrupt pin
  51. #define LED_PIN 13 // LED for showing interrupt
  52.  
  53. // Constants
  54. #define PROX_INT_HIGH 600 // Proximity level for interrupt
  55. #define PROX_INT_LOW 0 // No far interrupt
  56.  
  57. // Global variables
  58. APDS9930 apds = APDS9930();
  59. uint16_t proximity_data = 0;
  60. volatile bool isr_flag = false;
  61.  
  62. void setup() {
  63.  
  64. // Set LED as output
  65. pinMode(LED_PIN, OUTPUT);
  66. pinMode(APDS9930_INT, INPUT);
  67.  
  68. // Initialize Serial port
  69. Serial.begin(9600);
  70. Serial.println();
  71. Serial.println(F("------------------------------"));
  72. Serial.println(F("APDS-9930 - ProximityInterrupt"));
  73. Serial.println(F("------------------------------"));
  74.  
  75. // Initialize interrupt service routine
  76. attachInterrupt(0, interruptRoutine, FALLING);
  77.  
  78. // Initialize APDS-9930 (configure I2C and initial values)
  79. if ( apds.init() ) {
  80. Serial.println(F("APDS-9930 initialization complete"));
  81. } else {
  82. Serial.println(F("Something went wrong during APDS-9930 init!"));
  83. }
  84.  
  85. // Adjust the Proximity sensor gain
  86. if ( !apds.setProximityGain(PGAIN_2X) ) {
  87. Serial.println(F("Something went wrong trying to set PGAIN"));
  88. }
  89.  
  90. // Set proximity interrupt thresholds
  91. if ( !apds.setProximityIntLowThreshold(PROX_INT_LOW) ) {
  92. Serial.println(F("Error writing low threshold"));
  93. }
  94. if ( !apds.setProximityIntHighThreshold(PROX_INT_HIGH) ) {
  95. Serial.println(F("Error writing high threshold"));
  96. }
  97.  
  98. // Start running the APDS-9930 proximity sensor (interrupts)
  99. if ( apds.enableProximitySensor(true) ) {
  100. Serial.println(F("Proximity sensor is now running"));
  101. } else {
  102. Serial.println(F("Something went wrong during sensor init!"));
  103. }
  104.  
  105. #ifdef DUMP_REGS
  106. /* Register dump */
  107. uint8_t reg;
  108. uint8_t val;
  109.  
  110. for(reg = 0x00; reg <= 0x19; reg++) {
  111. if( (reg != 0x10) && \
  112. (reg != 0x11) )
  113. {
  114. apds.wireReadDataByte(reg, val);
  115. Serial.print(reg, HEX);
  116. Serial.print(": 0x");
  117. Serial.println(val, HEX);
  118. }
  119. }
  120. apds.wireReadDataByte(0x1E, val);
  121. Serial.print(0x1E, HEX);
  122. Serial.print(": 0x");
  123. Serial.println(val, HEX);
  124. #endif
  125.  
  126. }
  127.  
  128. void loop() {
  129.  
  130. // If interrupt occurs, print out the proximity level
  131. if ( isr_flag ) {
  132.  
  133. // Read proximity level and print it out
  134. if ( !apds.readProximity(proximity_data) ) {
  135. Serial.println("Error reading proximity value");
  136. } else {
  137. Serial.print("Proximity detected! Level: ");
  138. Serial.println(proximity_data);
  139. }
  140.  
  141. // Turn on LED for a half a second
  142. digitalWrite(LED_PIN, HIGH);
  143. delay(500);
  144. digitalWrite(LED_PIN, LOW);
  145.  
  146. // Reset flag and clear APDS-9930 interrupt (IMPORTANT!)
  147. isr_flag = false;
  148. if ( !apds.clearProximityInt() ) {
  149. Serial.println("Error clearing interrupt");
  150. }
  151.  
  152. }
  153. }
  154.  
  155. void interruptRoutine() {
  156. isr_flag = true;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement