This repository has been archived by the owner on Dec 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch_map.ino
163 lines (140 loc) · 5.06 KB
/
touch_map.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Authored By: Nicholas Whittaker
// FIT1041: Research Project
// Tangible Interfaces for the Vision Impaired
// WAVE SHIELD LIBRARY
// https://github.com/adafruit/WaveHC
// Code for reading and playing sound from the chip has been copied and adapted from the
// example files, as well as from the tutorials on Adafruit.com
// https://learn.adafruit.com/adafruit-wave-shield-audio-shield-for-arduino
// Uses a lot of RAM, so you should be using an Arduino with at least an ATmega328 chip
// This project was built using an Arduino Uno
#include <WaveHC.h>
#include <WaveUtil.h>
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're playing
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
// CONFIG
int controlPINS[3] { 9, 8, 7 }; // S0, S1, S2
int readPIN = A0;
int connectedPoints = 4;
int setOnOff[2]{ LOW, HIGH };
int triggerThreshold = 550;
char *songs[4] { "TRACK001.WAV", "TRACK002.WAV", "TRACK003.WAV", "TRACK004.WAV" };
int pollingRate = 1000;
int readDelay = 100;
bool verboseOutput = false;
// function definitions
void error(String msg);
bool readMultiplexer(int calibrationPINS[3]);
void playcomplete(char *name);
void playfile(char *name);
void setup() {
// GENERAL SETUP
Serial.begin(9600);
Serial.println("begin");
// MULTIPLEXER SETUP
// set control pins to ouput mode
for (int pin = 0; pin < 3; pin++) {
pinMode(controlPINS[pin], OUTPUT);
}
pinMode(readPIN, INPUT);
// AUDIO SHIELD SETUP
putstring_nl("\nWave test!"); // say we woke up!
putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
Serial.println(FreeRam());
// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
if (!card.init()) { //play with 8 MHz spi (default faster!)
error("Card init. failed!"); // Something went wrong, lets print out why
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
error("No valid FAT partition!"); // Something went wrong, lets print out why
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(), DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
error("Can't open root dir!"); // Something went wrong,
}
// Whew! We got past the tough parts.
putstring_nl("Files found (* = fragmented):");
// Print out all of the files in all the directories.
root.ls(LS_R | LS_FLAG_FRAGMENTED);
}
void loop() {
// calibrate the multiplexer pins and check the touchpoint
int calibrationPINS[3] {};
for (int point = 0; point < connectedPoints; point++) {
int val = point;
for (int i = 0; i < 3; i++) {
calibrationPINS[i] = setOnOff[val % 2];
val /= 2;
}
// read the touch point and trigger if needed
bool trigger = readMultiplexer(calibrationPINS);
if (trigger) {
if (verboseOutput) {
Serial.println("Activated point ");
Serial.println(point);
}
playfile(songs[point]);
}
}
// wait and reread the touch points
delay(pollingRate);
Serial.print('.');
}
void error(String msg) {
// stop the Arduino if there is an error during file reading
Serial.println(msg);
while(1);
}
bool readMultiplexer(int calibrationPINS[3]) {
// set the calibration pins
if (verboseOutput) { Serial.print("Reading "); }
for (int i = 0; i < 3; i++) {
if (verboseOutput) { Serial.print(calibrationPINS[i]); }
digitalWrite(controlPINS[i], calibrationPINS[i]);
}
// delay momentarilly to allow the multiplexer to reset
delay(readDelay);
if (verboseOutput) { Serial.print(" - "); }
// check whether the pressure at the chosen point surpasses the threshold
int pressure = analogRead(readPIN);
if (verboseOutput) { Serial.println(pressure); }
if (pressure > triggerThreshold) { return true; }
else { return false; }
}
void playcomplete(char *name) {
// plays a file from beginning to end
// call our helper to find and play this name
playfile(name);
}
void playfile(char *name) {
// check for a currently playing file and interrupt it
if (wave.isplaying) {
wave.stop();
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file "); Serial.print(name); return;
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV"); return;
}
// begin playing
wave.play();
}