Skip to content

Commit cc40ba4

Browse files
authored
Merge pull request adafruit#372 from adafruit/TheKitty-patch-27
Create code.py for Pumpkin Theremin
2 parents dfdcacd + 94d5a3e commit cc40ba4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Pumpkin_Theremin/code.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import time
2+
import board
3+
import adafruit_hcsr04
4+
from adafruit_circuitplayground.express import cpx
5+
6+
# This line creates the distance sensor as an object.
7+
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D9, echo_pin=board.D6, timeout=0.1)
8+
pixels = cpx.pixels
9+
pitchMultiplier = 300 # Change this value to modify the pitch of the theremin.
10+
11+
def wheel(pos):
12+
# Input a value 0 to 255 to get a color value.
13+
# The colours are a transition r - g - b - back to r.
14+
if pos < 0 or pos > 255:
15+
return (0, 0, 0)
16+
if pos < 85:
17+
return (255 - pos * 3, pos * 3, 0)
18+
if pos < 170:
19+
pos -= 85
20+
return (0, 255 - pos * 3, pos * 3)
21+
pos -= 170
22+
return (pos * 3, 0, 255 - pos * 3)
23+
24+
while True:
25+
try:
26+
handDistance = int(sonar.distance)
27+
print("Distance:", handDistance)
28+
except RuntimeError:
29+
print("retrying!")
30+
time.sleep(.00001)
31+
break
32+
33+
pitch = handDistance*pitchMultiplier
34+
35+
# Limits on the distances that trigger sound/light to between 3 and 25 cm.
36+
if (handDistance >= 3) & (handDistance < 25):
37+
cpx.play_tone(pitch, 0.1)
38+
pixels.fill(wheel(handDistance*10))
39+
pixels.show()
40+
time.sleep(.00001)
41+
print(pitch)
42+
else:
43+
cpx.stop_tone()
44+
pixels.fill((0, 0, 0))
45+
pixels.show()

0 commit comments

Comments
 (0)