Proximity_Trinkey_Arduino_Demo.ino
1 // SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #include <Adafruit_NeoPixel.h> 6 #include "Adafruit_FreeTouch.h" 7 #include "Adafruit_APDS9960.h" 8 9 10 // Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL 11 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); 12 int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255) 13 14 // Create the two touch pads on pins 1 and 2: 15 Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(1, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); 16 Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); 17 18 Adafruit_APDS9960 apds; 19 20 void setup() { 21 Serial.begin(9600); 22 //while (!Serial); 23 24 strip.begin(); 25 strip.setBrightness(neo_brightness); 26 strip.show(); // Initialize all pixels to 'off' 27 28 if (! qt_1.begin()) 29 Serial.println("Failed to begin qt on pin 1"); 30 if (! qt_2.begin()) 31 Serial.println("Failed to begin qt on pin 2"); 32 33 pinMode(PIN_INTERRUPT, INPUT_PULLUP); 34 if(!apds.begin()){ 35 Serial.println("failed to initialize device! Please check your wiring."); 36 while (1) { 37 strip.fill(0xFF0000); 38 strip.show(); 39 delay(100); 40 strip.fill(0x00); 41 strip.show(); 42 delay(100); 43 } 44 } 45 46 Serial.println("APDS initialized!"); 47 apds.enableProximity(true); 48 apds.setProxGain(APDS9960_PGAIN_8X); 49 apds.setLED(APDS9960_LEDDRIVE_100MA, APDS9960_LEDBOOST_300PCNT); 50 apds.setProxPulse(APDS9960_PPULSELEN_16US, 1); 51 52 //set the interrupt threshold to fire when proximity reading goes above 2 53 apds.setProximityInterruptThreshold(0, 2); 54 apds.enableProximityInterrupt(); 55 } 56 57 uint8_t j=0; 58 59 void loop() { 60 61 // print the proximity reading when the interrupt pin goes low 62 if (!digitalRead(PIN_INTERRUPT)){ 63 uint16_t prox = apds.readProximity(); 64 Serial.print("Proximity: "); 65 Serial.println(prox); 66 67 if (prox < 3) prox = 0; // ignore 1 and 2 readings 68 strip.setBrightness(prox); 69 70 //clear the interrupt 71 apds.clearInterrupt(); 72 } else { 73 strip.setBrightness(0); 74 } 75 76 strip.fill(Wheel(j)); 77 strip.show(); 78 79 // measure the captouches 80 uint16_t touch1 = qt_1.measure(); 81 uint16_t touch2 = qt_2.measure(); 82 83 // If the first pad is touched, go forward 84 if (touch1 > 500) { 85 Serial.println("QT 1 touched"); 86 j++; 87 } 88 89 // If the second pad is touched, go backward 90 if (touch2 > 500) { 91 Serial.println("QT 2 touched"); 92 j--; 93 } 94 95 delay(10); 96 } 97 98 // Input a value 0 to 255 to get a color value. 99 // The colours are a transition r - g - b - back to r. 100 uint32_t Wheel(byte WheelPos) { 101 if(WheelPos < 85) { 102 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 103 } else if(WheelPos < 170) { 104 WheelPos -= 85; 105 return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); 106 } else { 107 WheelPos -= 170; 108 return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); 109 } 110 }