-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFullProgram
More file actions
280 lines (187 loc) · 6.08 KB
/
FullProgram
File metadata and controls
280 lines (187 loc) · 6.08 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <SPI.h>
#include <SD.h>
#define SD_ChipSelectPin 53
#include "TMRpcm.h"
// use more minutes sound to avoid tmrpcm.loop breaks
TMRpcm tmrpcm;
float getsamplerate(char* namefile);
void playsound();
// keep basefreq half of original samplerate,to see frequency change effect
char* filename = "SPORTSTERCRANK.WAV";
int vol=0 ,basevol = 2,highval =7 ;
float basefreq;
// double timefactor;
byte btt;
double x=0,lastz=0,z=0;
enum PinAssignments {
encoderPinA = 2, // right
encoderPinB = 3, // left
clearButton = 8 // another two pins
};
volatile unsigned int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating = false; // debounce management
// interrupt service routine vars
boolean A_set = false;
boolean B_set = false;
const int buttonPin = 5; // the number of the pushbutton pin
// const int ledPin = 13; // the number of the LED pin
// Variables will change:
// int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
File root;
// String s;
void setup(){
pinMode(buttonPin, INPUT_PULLUP);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(clearButton, INPUT_PULLUP);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(digitalPinToInterrupt(encoderPinA), doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(digitalPinToInterrupt(encoderPinB), doEncoderB, CHANGE);
tmrpcm.speakerPin = 11;
tmrpcm.speakerPin2 = 12;
btt = B01100000;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
Serial.println("SD initialization success");
basefreq = getsamplerate(filename);
// basefreq = (basefreq * 5)/8;
// Serial.println(basefreq);
tmrpcm.SAMPLE_RATE = basefreq;
tmrpcm.quality(1);
tmrpcm.loop(1);
tmrpcm.setVolume(basevol);
tmrpcm.play(filename);
root = SD.open("/");
// tmrpcm.quality(1);
}
void loop(){
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == LOW) {
playsound();
}
}
}
// set the LED:
// digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
// read the state of the pushbutton value:
// Serial.println(mag);
// Serial.println("outside");
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
rotating = true; // reset the debouncer
if (digitalRead(clearButton) == LOW ) {
encoderPos = 1;
}
if (lastReportedPos != encoderPos) {
Serial.print("Index:");
Serial.println(encoderPos, DEC);
lastReportedPos = encoderPos;
}
//remove the comment,when used with rotatry encoder
z= encoderPos;
// z= Serial.parseFloat();
if(z>0&&z<=highval&&z!=lastz){
// lastReportedPos = encoderPos;
// map function ,takes value between 0 and 1000 and adjust freq and sound accordingly
x = map(z,0,highval,basefreq,32000);
vol = map(z,0,highval,basevol,5);
tmrpcm.setVolume(vol);
// setting sample rate
tmrpcm.SAMPLE_RATE = x;
// setting resolution
if(bitRead(btt,6)){tmrpcm.resolution = 10 * (800000/tmrpcm.SAMPLE_RATE);}
else{ tmrpcm.resolution = 10 * (1600000/tmrpcm.SAMPLE_RATE);
}
noInterrupts();
tmrpcm.timerSt();
//timefactor = y/x;
interrupts();
// currentval/newval
// timefactor = x/(32000.00/time);
// start = millis();
// currentseektime = seektime;
// round(elapsed/1000.00)+prev
// prev = prev + round(elapsed/1000.00);
// con++;
lastz = z;
}
}
float getsamplerate(char* namefile){
tmrpcm.wavInfo(namefile);
return tmrpcm.orgsamplerate;
}
// Interrupt on A changing state
void doEncoderA() {
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set && encoderPos>0 )
encoderPos -= 1;
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state, same as A above
void doEncoderB() {
if ( rotating ) delay (1);
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter - 1 if B leads A
if ( B_set && !A_set && encoderPos<highval-1 )
encoderPos += 1;
rotating = false;
}
}
void playsound(){
tmrpcm.stopPlayback();
// Serial.println("inside playsound");
File f = root.openNextFile();
char* fname;
fname = f.name();
// Serial.println(fname);
// char* empty = "";
// s = String(f.name());
if(strcmp(fname, "") == 0){
// Serial.println("empty string");
root.rewindDirectory();
// s = String(f.name());
f = root.openNextFile();
fname = f.name();
}
f.close();
basefreq = getsamplerate(fname);
// basefreq = (basefreq * 5)/8;
// Serial.println("abc");
tmrpcm.SAMPLE_RATE = basefreq;
tmrpcm.play(fname);
Serial.println(fname);
}