|
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 | | -} |
| 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 | +} |
0 commit comments