Skip to content

Commit ed84ee7

Browse files
authored
Merge pull request #652 from adafruit/Capacitive_Touch_Sensors_on_the_Raspberry_Pi
Capacitive touch sensors on the raspberry pi
2 parents 8f280bb + 605d66d commit ed84ee7

10 files changed

Lines changed: 219 additions & 0 deletions

File tree

Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import time
2+
import board
3+
from digitalio import DigitalInOut, Direction
4+
5+
# set the GPIO input pins
6+
pad0_pin = board.D22
7+
pad1_pin = board.D21
8+
pad2_pin = board.D17
9+
pad3_pin = board.D24
10+
pad4_pin = board.D23
11+
12+
pad0 = DigitalInOut(pad0_pin)
13+
pad1 = DigitalInOut(pad1_pin)
14+
pad2 = DigitalInOut(pad2_pin)
15+
pad3 = DigitalInOut(pad3_pin)
16+
pad4 = DigitalInOut(pad4_pin)
17+
18+
pad0.direction = Direction.INPUT
19+
pad1.direction = Direction.INPUT
20+
pad2.direction = Direction.INPUT
21+
pad3.direction = Direction.INPUT
22+
pad4.direction = Direction.INPUT
23+
24+
pad0_already_pressed = True
25+
pad1_already_pressed = True
26+
pad2_already_pressed = True
27+
pad3_already_pressed = True
28+
pad4_already_pressed = True
29+
30+
while True:
31+
32+
if pad0.value and not pad0_already_pressed:
33+
print("Pad 0 pressed")
34+
pad0_already_pressed = pad0.value
35+
36+
if pad1.value and not pad1_already_pressed:
37+
print("Pad 1 pressed")
38+
pad1_already_pressed = pad1.value
39+
40+
if pad2.value and not pad2_already_pressed:
41+
print("Pad 2 pressed")
42+
pad2_already_pressed = pad2.value
43+
44+
if pad3.value and not pad3_already_pressed:
45+
print("Pad 3 pressed")
46+
pad3_already_pressed = pad3.value
47+
48+
if pad4.value and not pad4_already_pressed:
49+
print("Pad 4 pressed")
50+
pad4_already_pressed = pad4.value
51+
52+
time.sleep(0.1)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import time
2+
import board
3+
from digitalio import DigitalInOut, Direction
4+
5+
pad_pin = board.D23
6+
7+
pad = DigitalInOut(pad_pin)
8+
pad.direction = Direction.INPUT
9+
10+
while True:
11+
12+
if pad.value:
13+
print("pressed")
14+
15+
time.sleep(0.1)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import time
2+
import board
3+
import mcpi.minecraft as minecraft
4+
from digitalio import DigitalInOut, Direction
5+
mc = minecraft.Minecraft.create()
6+
7+
# set the GPIO input pins
8+
pad0_pin = board.D22
9+
pad1_pin = board.D21
10+
pad2_pin = board.D17
11+
pad3_pin = board.D24
12+
pad4_pin = board.D23
13+
14+
pad0 = DigitalInOut(pad0_pin)
15+
pad1 = DigitalInOut(pad1_pin)
16+
pad2 = DigitalInOut(pad2_pin)
17+
pad3 = DigitalInOut(pad3_pin)
18+
pad4 = DigitalInOut(pad4_pin)
19+
20+
pad0.direction = Direction.INPUT
21+
pad1.direction = Direction.INPUT
22+
pad2.direction = Direction.INPUT
23+
pad3.direction = Direction.INPUT
24+
pad4.direction = Direction.INPUT
25+
26+
pad0_already_pressed = False
27+
pad3_already_pressed = False
28+
pad4_already_pressed = False
29+
30+
immutable = False
31+
32+
tnt = 46
33+
water = 9
34+
flowers = 38
35+
36+
while True:
37+
pad0_pressed = not pad0.value
38+
pad1_pressed = not pad1.value
39+
pad2_pressed = not pad2.value
40+
pad3_pressed = not pad3.value
41+
pad4_pressed = not pad4.value
42+
43+
if pad0_pressed and not pad0_already_pressed:
44+
#teleport
45+
x = 0
46+
y = 0
47+
z = 0
48+
mc.player.setPos(x, y, z)
49+
pad0_already_pressed = pad0_pressed
50+
51+
if pad1_pressed:
52+
#Flowers
53+
pos = mc.player.getPos()
54+
x = pos.x
55+
y = pos.y
56+
z = pos.z
57+
mc.setBlock(x, y, z, flowers)
58+
59+
if pad2_pressed:
60+
#TNT
61+
pos = mc.player.getPos()
62+
x = pos.x
63+
y = pos.y
64+
z = pos.z
65+
mc.setBlock(x, y, z, tnt, 1)
66+
67+
if pad3_pressed and not pad3_already_pressed:
68+
#Chat message: Are in water?
69+
pos = mc.player.getPos()
70+
block = mc.getBlock(pos.x, pos.y, pos.z)
71+
inWater = block == water
72+
mc.postToChat("In water: " + str(inWater))
73+
pad3_already_pressed = pad3_pressed
74+
75+
if pad4_pressed and not pad4_already_pressed:
76+
#Immutable
77+
immutable = not immutable
78+
mc.setting("world_immutable", immutable)
79+
mc.postToChat("Immutable: " + str(immutable))
80+
pad4_already_pressed = pad4_pressed
81+
82+
time.sleep(0.1)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import time
2+
import board
3+
import mcpi.minecraft as minecraft
4+
from digitalio import DigitalInOut, Direction
5+
6+
mc = minecraft.Minecraft.create()
7+
8+
pad_pin = board.D23
9+
10+
pad = DigitalInOut(pad_pin)
11+
pad.direction = Direction.INPUT
12+
13+
# melon block
14+
block_type = 103
15+
16+
while True:
17+
18+
if pad.value:
19+
pos = mc.player.getPos()
20+
x = pos.x
21+
y = pos.y
22+
z = pos.z
23+
mc.setBlock(x, y, z, block_type)
24+
25+
time.sleep(0.1)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import time
2+
import board
3+
import mcpi.minecraft as minecraft
4+
from digitalio import DigitalInOut, Direction
5+
6+
mc = minecraft.Minecraft.create()
7+
8+
pad_pin = board.D23
9+
pad = DigitalInOut(pad_pin)
10+
pad.direction = Direction.INPUT
11+
12+
already_pressed = False
13+
14+
while True:
15+
16+
if pad.value and not already_pressed:
17+
pos = mc.player.getPos()
18+
x = pos.x
19+
y = pos.y + 10
20+
z = pos.z
21+
mc.player.setPos(x, y, z)
22+
alreadyPressed = pad.value
23+
time.sleep(0.1)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Capacitive_Touch_Sensors_on_the_Raspberry_Pi
2+
3+
Code to accompany this tutorial:
4+
https://learn.adafruit.com/capacitive-touch-sensors-on-the-raspberry-pi
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
import board
3+
from digitalio import DigitalInOut, Direction
4+
5+
pad_pin = board.D23
6+
7+
pad = DigitalInOut(pad_pin)
8+
pad.direction = Direction.INPUT
9+
10+
already_pressed = False
11+
12+
while True:
13+
14+
if pad.value and not already_pressed:
15+
print("pressed")
16+
17+
already_pressed = pad.value
18+
time.sleep(0.1)
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)