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