Skip to content

Commit 568abca

Browse files
authored
Merge pull request #1843 from kattni/prox-trinkey
Adding Prox Trinkey code
2 parents 4388e10 + 74201d5 commit 568abca

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
CircuitPython Capacitive Touch Pad Example - Print to the serial console when one pad is touched.
3+
"""
4+
import time
5+
import board
6+
import touchio
7+
8+
touch = touchio.TouchIn(board.TOUCH1)
9+
10+
while True:
11+
if touch.value:
12+
print("Pad touched!")
13+
time.sleep(0.1)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
CircuitPython Capacitive Two Touch Pad Example - Print to the serial console when a pad is touched.
3+
"""
4+
import time
5+
import board
6+
import touchio
7+
8+
touch_one = touchio.TouchIn(board.TOUCH1)
9+
touch_two = touchio.TouchIn(board.TOUCH2)
10+
11+
while True:
12+
if touch_one.value:
13+
print("Pad one touched!")
14+
if touch_two.value:
15+
print("Pad two touched!")
16+
time.sleep(0.1)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
CircuitPython NeoPixel Blink example - blinking the built-in NeoPixels.
3+
"""
4+
import time
5+
import board
6+
import neopixel
7+
8+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 2)
9+
10+
while True:
11+
pixel.fill((255, 0, 0))
12+
time.sleep(0.5)
13+
pixel.fill((0, 0, 0))
14+
time.sleep(0.5)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
NeoPixel brightness proximity example. Increases the brightness of the NeoPixels as you move closer
3+
to the proximity sensor.
4+
"""
5+
import time
6+
import board
7+
import neopixel
8+
from adafruit_apds9960.apds9960 import APDS9960
9+
10+
apds = APDS9960(board.I2C())
11+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 2)
12+
13+
apds.enable_proximity = True
14+
15+
16+
def proximity_to_brightness(value):
17+
"""Maps the proximity values (0 - 255) to the brightness values (0.0 - 1.0)"""
18+
return value / 255 * 1.0
19+
20+
21+
pixels.fill((255, 0, 0))
22+
23+
while True:
24+
print(apds.proximity)
25+
pixels.brightness = proximity_to_brightness(apds.proximity)
26+
time.sleep(0.2)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Proximity spacebar dino game example. Sends a space when you move your hand close to the proximity
3+
sensor and turns the LEDs on to let you know you're in the right range. For use with the Chrome
4+
Dino game, reachable in Chrome with chrome://dino or when you have no network connectivity.
5+
"""
6+
import board
7+
import usb_hid
8+
from adafruit_hid.keyboard import Keyboard
9+
from adafruit_hid.keycode import Keycode
10+
import neopixel
11+
from adafruit_apds9960.apds9960 import APDS9960
12+
13+
apds = APDS9960(board.I2C())
14+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 2)
15+
keyboard = Keyboard(usb_hid.devices)
16+
17+
apds.enable_proximity = True
18+
19+
space = False
20+
while True:
21+
print(apds.proximity)
22+
current_proximity = apds.proximity
23+
if current_proximity > 100 and not space:
24+
pixels.fill((255, 0, 0))
25+
keyboard.send(Keycode.SPACE)
26+
space = True
27+
elif current_proximity < 50 and space:
28+
pixels.fill(0)
29+
space = False
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)