-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTuner CodeV1.0.txt
204 lines (186 loc) · 4.46 KB
/
Tuner CodeV1.0.txt
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
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
#define I2C_ADDRESS 0x3C
#define RST_PIN -1
SSD1306AsciiAvrI2c oled;
#include "arduinoFFT.h"
#define SAMPLES 128 //Must be a power of 2
#define SAMPLING_FREQUENCY 1000 //Hz, must be less than 10000 due to ADC
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
const int buttonOne = 2;
const int buttonTwo = 3;
const int buttonThree = 4;
int buttonOneState = 0;
int buttonTwoState = 0;
int buttonThreeState = 0;
char selectionNote = 'E';
int selectionOctave = 2;
int currentPitch = 440;
int targetPitch = 82;
float twelfthRootTwo = 1.059463094;
int adjustment = 1;
void setup() {
sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
pinMode(buttonThree, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
#if RST_PIN >= 0
oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
oled.begin(&Adafruit128x64, I2C_ADDRESS);
#endif // RST_PIN >= 0
oled.setFont(Adafruit5x7);
}
void loop() {
getCurrentPitch();
selectPitch();
drawScreen();
if(digitalRead(buttonTwo) == HIGH){
tensionAdjustment();
}
else{
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
}
void getCurrentPitch(){
for(int i=0; i<SAMPLES; i++)
{
microseconds = micros(); //Overflows after around 70 minutes!
vReal[i] = analogRead(0);
vImag[i] = 0;
while(micros() < (microseconds + sampling_period_us)){
}
}
/*FFT*/
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
/*PRINT RESULTS*/
currentPitch = peak;
delay(500); //Repeat the process every second
}
void selectPitch(){
delay(100);
buttonOneState = digitalRead(buttonOne);
buttonThreeState = digitalRead(buttonThree);
if(buttonOneState == HIGH){
if(selectionNote == 'C' && selectionOctave == 2){
}
else{
if(selectionNote == 'C'){
selectionNote--;
selectionOctave--;
}
else if(selectionNote == 'A'){
selectionNote = 'G';
}
else{
selectionNote--;
}
}
getTargetPitch();
oled.clear();
}
if(buttonThreeState == HIGH){
if(selectionNote == 'A' && selectionOctave == 4){
}
else{
if(selectionNote == 'B'){
selectionNote++;
selectionOctave++;
}
else if(selectionNote == 'G'){
selectionNote = 'A';
}
else{
selectionNote++;
}
}
getTargetPitch();
oled.clear();
}
}
void getTargetPitch(){
int semitoneDifference = 0;
char searchNote = 'A';
int searchOctave = 4;
boolean differenceFound = false;
while(!differenceFound){
if(searchNote == selectionNote && searchOctave == selectionOctave){
differenceFound = true;
}
else{
if(searchNote == 'C'){
searchNote--;
searchOctave--;
semitoneDifference++;
}
else if(searchNote == 'F'){
searchNote--;
semitoneDifference++;
}
else if(searchNote == 'A'){
searchNote = 'G';
semitoneDifference += 2;
}
else{
searchNote--;
semitoneDifference += 2;
}
}
}
Serial.println(semitoneDifference);
targetPitch = 440*pow(twelfthRootTwo, -semitoneDifference);
Serial.println(targetPitch);
}
void drawScreen(){
oled.set2X();
oled.setCursor(60,2);
oled.print(selectionNote);
oled.set1X();
oled.setCursor(70,4);
oled.print(selectionOctave);
oled.setCursor(15,6);
oled.print(String(currentPitch));
oled.setCursor(35,6);
oled.print("Hz");
oled.setCursor(60,6);
if(currentPitch < targetPitch){
oled.print("/\\");
adjustment = 2;
}
else if(currentPitch == targetPitch){
oled.print("==");
adjustment = 1;
}
else if(currentPitch > targetPitch){
oled.print("\\/");
adjustment = 0;
}
oled.setCursor(85,6);
oled.print(String(targetPitch));
oled.setCursor(105,6);
oled.print("Hz");
}
void tensionAdjustment(){
if(adjustment == 0){
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
}
else if(adjustment == 1){
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
else if(adjustment == 2){
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
}
}