School project for big bang base code

Dependencies:   mbed

Fork of GT_Tuner3 by Mike Pollock

Committer:
mptapton
Date:
Mon Feb 06 13:12:57 2017 +0000
Revision:
2:4be1b96a5a34
Parent:
1:c8ec50d75f80
Tapton school project guitar code for BB

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mptapton 0:6b0b61d411ad 1 // Guitar Tuner and Chord Learning Tool using Goertzel's Algorithm //
mptapton 0:6b0b61d411ad 2 // Based on original code created by: Andrew Durand //
mptapton 0:6b0b61d411ad 3 // Adjusted and modified by Tapton School EDS Project Team //
mptapton 0:6b0b61d411ad 4
mptapton 0:6b0b61d411ad 5 #include "TextLCD.h"
mptapton 0:6b0b61d411ad 6 #include "mbed.h"
mptapton 0:6b0b61d411ad 7 #include "adc.h"
mptapton 0:6b0b61d411ad 8 #include <math.h>
mptapton 0:6b0b61d411ad 9 #define PI 3.1415
mptapton 0:6b0b61d411ad 10 #define SAMPLE_RATE 24000
mptapton 0:6b0b61d411ad 11
mptapton 2:4be1b96a5a34 12 Serial pc(USBTX, USBRX); // tx, rx
mptapton 2:4be1b96a5a34 13
mptapton 2:4be1b96a5a34 14 DigitalOut intune(p22);
mptapton 2:4be1b96a5a34 15 DigitalOut toohigh(p23);
mptapton 2:4be1b96a5a34 16 DigitalOut toolow(p24);
mptapton 2:4be1b96a5a34 17 DigitalOut state(p25);
mptapton 0:6b0b61d411ad 18 DigitalOut led_low(LED1);
mptapton 0:6b0b61d411ad 19 DigitalOut led_ok(LED2);
mptapton 0:6b0b61d411ad 20 DigitalOut led_high(LED4);
mptapton 0:6b0b61d411ad 21 InterruptIn button1(p6); //mosi
mptapton 0:6b0b61d411ad 22
mptapton 2:4be1b96a5a34 23 DigitalIn myInputPin (p21); //select tuner or chord learning mode
mptapton 2:4be1b96a5a34 24
mptapton 2:4be1b96a5a34 25 Serial device(p28, p27); // tx, rx to connect to mbed 2
mptapton 0:6b0b61d411ad 26
mptapton 0:6b0b61d411ad 27 /* This code uses libraries created for 4-bit LCD's based on the HD44780. This
mptapton 0:6b0b61d411ad 28 program was designed for a similar product (Winstar's WH1602B 2x16 LC) working
mptapton 0:6b0b61d411ad 29 into an Mbed LPC1768.
mptapton 0:6b0b61d411ad 30 LCD pins: Pin 1(VSS) to Mbed Gnd, Pin 2(VDD) to Mbed 5v USB output, Pin 3(Vo- contrast)
mptapton 0:6b0b61d411ad 31 to Mbed Gnd (via a 4k7 resistor), Pin 5(R/W) to Mbed Gnd, Pin 15(A)to Mbed 5v USB output, Pin 16(B) to
mptapton 0:6b0b61d411ad 32 MBed Gnd, Pins 4(RS),20(E) and the 4 data bits (DB4 [11] through to DB7 [14])
mptapton 0:6b0b61d411ad 33 go to the Mbed pins described below: */
mptapton 2:4be1b96a5a34 34 TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7
mptapton 0:6b0b61d411ad 35
mptapton 2:4be1b96a5a34 36 int intunetrig=2; //set max times in tune for before triggering intune to mbed2
mptapton 2:4be1b96a5a34 37 int txcountmax=4;//set max times before determining in tune or not {set to even number as used later in divide by 2
mptapton 2:4be1b96a5a34 38 int txcounttrig=2; // set threshold for triggering success or not
mptapton 2:4be1b96a5a34 39 int txcounter=0; //set counter to control transmit message timing to mbed2
mptapton 2:4be1b96a5a34 40 int intunecounter=0;//set counter to capture how many times in tune during txcounter time
mptapton 0:6b0b61d411ad 41 int string_select = 0;
mptapton 0:6b0b61d411ad 42 int chord_select = 0;
mptapton 0:6b0b61d411ad 43 float high, high1, in_tune, in_tune1, in_tune2, in_tune3,
mptapton 0:6b0b61d411ad 44 low, low1, note, low_mod, high_mod;
mptapton 0:6b0b61d411ad 45 char* key;
mptapton 2:4be1b96a5a34 46 char* chordkey;
mptapton 0:6b0b61d411ad 47 int Counter = 0;
mptapton 0:6b0b61d411ad 48 int Buffer[6000];
mptapton 0:6b0b61d411ad 49
mptapton 0:6b0b61d411ad 50 float goertzelFilter(int samples[], float freq, int N) {
mptapton 0:6b0b61d411ad 51 float s_prev = 0.0;
mptapton 0:6b0b61d411ad 52 float s_prev2 = 0.0;
mptapton 0:6b0b61d411ad 53 float coeff,normalizedfreq,power,s;
mptapton 0:6b0b61d411ad 54 int i;
mptapton 0:6b0b61d411ad 55 normalizedfreq = freq / SAMPLE_RATE;
mptapton 0:6b0b61d411ad 56 coeff = 2*cos(2*PI*normalizedfreq);
mptapton 0:6b0b61d411ad 57 for (i=0; i<N; i++) {
mptapton 0:6b0b61d411ad 58 s = samples[i] + coeff * s_prev - s_prev2;
mptapton 0:6b0b61d411ad 59 s_prev2 = s_prev;
mptapton 0:6b0b61d411ad 60 s_prev = s;
mptapton 0:6b0b61d411ad 61 }
mptapton 0:6b0b61d411ad 62 power = s_prev2*s_prev2+s_prev*s_prev-coeff*s_prev*s_prev2;
mptapton 0:6b0b61d411ad 63 return power;
mptapton 0:6b0b61d411ad 64 }
mptapton 0:6b0b61d411ad 65
mptapton 0:6b0b61d411ad 66 ADC adc(SAMPLE_RATE, 1);
mptapton 0:6b0b61d411ad 67
mptapton 0:6b0b61d411ad 68 void sample_audio(int chan, uint32_t value) {
mptapton 0:6b0b61d411ad 69 Buffer[Counter] = adc.read(p20);
mptapton 0:6b0b61d411ad 70 Counter += 1;
mptapton 0:6b0b61d411ad 71 }
mptapton 0:6b0b61d411ad 72
mptapton 0:6b0b61d411ad 73 void button1_pressed() {
mptapton 0:6b0b61d411ad 74 string_select++;
mptapton 0:6b0b61d411ad 75 chord_select++;
mptapton 0:6b0b61d411ad 76 if (string_select > 5) string_select = 0;
mptapton 2:4be1b96a5a34 77 if (chord_select > 6) chord_select = 0;//change for number of chords supported
mptapton 2:4be1b96a5a34 78 intune=0; //clear all pins to mbed2
mptapton 2:4be1b96a5a34 79 toohigh=0;
mptapton 2:4be1b96a5a34 80 toolow=0;
mptapton 2:4be1b96a5a34 81 state=0;
mptapton 2:4be1b96a5a34 82 txcounter=0; //set counter to control transmit message timing to mbed2
mptapton 2:4be1b96a5a34 83 intunecounter=0;//set counter to capture how many times in tune during txcounter time
mptapton 0:6b0b61d411ad 84 }
mptapton 0:6b0b61d411ad 85
mptapton 0:6b0b61d411ad 86 int main() {
mptapton 2:4be1b96a5a34 87 pc.baud(115200);
mptapton 2:4be1b96a5a34 88 device.baud(19200); //mbed to mbed serial communication speed
mptapton 2:4be1b96a5a34 89 txcounter=0;
mptapton 2:4be1b96a5a34 90 intunecounter=0;
mptapton 2:4be1b96a5a34 91 intune=0; //clear all pins to mbed2
mptapton 2:4be1b96a5a34 92 toohigh=0;
mptapton 2:4be1b96a5a34 93 toolow=0;
mptapton 2:4be1b96a5a34 94 state=0;
mptapton 2:4be1b96a5a34 95 int chordkeyint=0;
mptapton 2:4be1b96a5a34 96 setbuf(stdout, NULL);
mptapton 2:4be1b96a5a34 97
mptapton 0:6b0b61d411ad 98 while (1) {
mptapton 0:6b0b61d411ad 99 myInputPin.mode(PullUp); //set the mbed to use a pullup resistor
mptapton 0:6b0b61d411ad 100 if (myInputPin) { //select guitar tuner or chord teaching
mptapton 0:6b0b61d411ad 101 lcd.cls (); // Guitar Tuner Section based on p21 being +3v
mptapton 0:6b0b61d411ad 102
mptapton 0:6b0b61d411ad 103 //Interupt for Switching Strings
mptapton 0:6b0b61d411ad 104 button1.mode(PullDown);
mptapton 0:6b0b61d411ad 105 button1.rise(&button1_pressed);
mptapton 0:6b0b61d411ad 106
mptapton 0:6b0b61d411ad 107 while (1) {
mptapton 0:6b0b61d411ad 108
mptapton 0:6b0b61d411ad 109 switch (string_select) {
mptapton 0:6b0b61d411ad 110 case 0:
mptapton 0:6b0b61d411ad 111 note = 82;
mptapton 2:4be1b96a5a34 112 key= "E"; //E2
mptapton 2:4be1b96a5a34 113 chordkeyint=69; //E in ASCII
mptapton 0:6b0b61d411ad 114 break;
mptapton 0:6b0b61d411ad 115 case 1:
mptapton 0:6b0b61d411ad 116 note = 110;
mptapton 2:4be1b96a5a34 117 key= "A";//A2
mptapton 2:4be1b96a5a34 118 chordkeyint=65; //A in ASCII
mptapton 0:6b0b61d411ad 119 break;
mptapton 0:6b0b61d411ad 120 case 2:
mptapton 0:6b0b61d411ad 121 note = 147;
mptapton 2:4be1b96a5a34 122 key= "D";//D3
mptapton 2:4be1b96a5a34 123 chordkeyint=68; //D in ASCII
mptapton 0:6b0b61d411ad 124 break;
mptapton 0:6b0b61d411ad 125 case 3:
mptapton 0:6b0b61d411ad 126 note = 196;
mptapton 2:4be1b96a5a34 127 key= "G";//G3
mptapton 2:4be1b96a5a34 128 chordkeyint=71; //G in ASCII
mptapton 0:6b0b61d411ad 129 break;
mptapton 0:6b0b61d411ad 130 case 4:
mptapton 0:6b0b61d411ad 131 note = 247;
mptapton 2:4be1b96a5a34 132 key= "B";//B3
mptapton 2:4be1b96a5a34 133 chordkeyint=66; //B in ASCII
mptapton 0:6b0b61d411ad 134 break;
mptapton 0:6b0b61d411ad 135 case 5:
mptapton 0:6b0b61d411ad 136 note = 330;
mptapton 2:4be1b96a5a34 137 key= "E";//E4
mptapton 2:4be1b96a5a34 138 chordkeyint=101; //e in ASCII
mptapton 0:6b0b61d411ad 139 break;
mptapton 0:6b0b61d411ad 140 }
mptapton 0:6b0b61d411ad 141
mptapton 0:6b0b61d411ad 142 //Prepare for burst mode on all ADC pins and set up interrupt handler (using ADC library from Simon Blandford
mptapton 0:6b0b61d411ad 143 adc.append(sample_audio);
mptapton 0:6b0b61d411ad 144 adc.startmode(0,0);
mptapton 0:6b0b61d411ad 145 adc.burst(1);
mptapton 0:6b0b61d411ad 146 adc.setup(p20,1);
mptapton 0:6b0b61d411ad 147
mptapton 0:6b0b61d411ad 148 //start the interrupt and wait for about 4096 samples
mptapton 0:6b0b61d411ad 149 adc.interrupt_state(p20,1);
mptapton 0:6b0b61d411ad 150 wait(.2);
mptapton 0:6b0b61d411ad 151
mptapton 0:6b0b61d411ad 152 //Finsh up - Unset pin 20
mptapton 0:6b0b61d411ad 153 adc.interrupt_state(p20,0);
mptapton 0:6b0b61d411ad 154 adc.setup(p20,0);
mptapton 0:6b0b61d411ad 155 int actual_rate = adc.actual_sample_rate();
mptapton 0:6b0b61d411ad 156
mptapton 0:6b0b61d411ad 157 //for debugging tell the terminal sample rate and how many samples we took
mptapton 2:4be1b96a5a34 158 //printf("Requested max sample rate is %u, actual max sample rate is %u.\n",
mptapton 2:4be1b96a5a34 159 // SAMPLE_RATE, actual_rate);
mptapton 2:4be1b96a5a34 160 // printf("We did %i samples\n",Counter);
mptapton 0:6b0b61d411ad 161
mptapton 0:6b0b61d411ad 162 high = 0;
mptapton 0:6b0b61d411ad 163 low = 0;
mptapton 0:6b0b61d411ad 164 for (int i=3; i<46; i+=3) {
mptapton 0:6b0b61d411ad 165 high1 = goertzelFilter(Buffer, (note + i ), Counter);
mptapton 0:6b0b61d411ad 166 if (high1 > high) high=high1;
mptapton 0:6b0b61d411ad 167 }
mptapton 0:6b0b61d411ad 168 for (int i=3; i<46; i+=3) {
mptapton 0:6b0b61d411ad 169 low1 = goertzelFilter(Buffer, (note - i ), Counter);
mptapton 0:6b0b61d411ad 170 if (low1 > low) low=low1;
mptapton 0:6b0b61d411ad 171 }
mptapton 0:6b0b61d411ad 172 in_tune1 = goertzelFilter(Buffer, (note+1), Counter);
mptapton 0:6b0b61d411ad 173 in_tune2 = goertzelFilter(Buffer, note, Counter);
mptapton 0:6b0b61d411ad 174 in_tune3 = goertzelFilter(Buffer, (note-1), Counter);
mptapton 0:6b0b61d411ad 175
mptapton 0:6b0b61d411ad 176 if ((in_tune1 > in_tune2) && (in_tune1 > in_tune3)) in_tune = in_tune1;
mptapton 0:6b0b61d411ad 177 else if ((in_tune2 > in_tune1) && (in_tune2 > in_tune3)) in_tune = in_tune2;
mptapton 0:6b0b61d411ad 178 else in_tune = in_tune3;
mptapton 0:6b0b61d411ad 179
mptapton 0:6b0b61d411ad 180 if ((in_tune > high) && (in_tune > low)) {
mptapton 0:6b0b61d411ad 181 led_high = 0;
mptapton 0:6b0b61d411ad 182 led_ok = 1;
mptapton 0:6b0b61d411ad 183 led_low = 0;
mptapton 0:6b0b61d411ad 184 } else if (high > in_tune) {
mptapton 0:6b0b61d411ad 185 led_high = 1;
mptapton 0:6b0b61d411ad 186 led_ok = 0;
mptapton 0:6b0b61d411ad 187 led_low = 0;
mptapton 0:6b0b61d411ad 188 } else if (low > in_tune) {
mptapton 0:6b0b61d411ad 189 led_high = 0;
mptapton 0:6b0b61d411ad 190 led_ok = 0;
mptapton 0:6b0b61d411ad 191 led_low = 1;
mptapton 0:6b0b61d411ad 192 } else {
mptapton 0:6b0b61d411ad 193 led_high = 0;
mptapton 0:6b0b61d411ad 194 led_ok = 0;
mptapton 0:6b0b61d411ad 195 led_low = 0;
mptapton 0:6b0b61d411ad 196 }
mptapton 0:6b0b61d411ad 197
mptapton 0:6b0b61d411ad 198 int pintwenty = adc.read(p20); //read pin 20
mptapton 0:6b0b61d411ad 199 lcd.locate(0,1);
mptapton 0:6b0b61d411ad 200 lcd.printf("%s %iHz %d\n",key, (int) note, pintwenty);
mptapton 1:c8ec50d75f80 201 if (led_ok) {
mptapton 1:c8ec50d75f80 202 lcd.printf("Tuner- In Tune");
mptapton 2:4be1b96a5a34 203 intune=1;
mptapton 2:4be1b96a5a34 204 toohigh=0;
mptapton 2:4be1b96a5a34 205 toolow=0;
mptapton 1:c8ec50d75f80 206 }
mptapton 1:c8ec50d75f80 207 else if (led_low) {
mptapton 1:c8ec50d75f80 208 lcd.printf("Tuner- 2Low ");
mptapton 2:4be1b96a5a34 209 intune=0;
mptapton 2:4be1b96a5a34 210 toohigh=0;
mptapton 2:4be1b96a5a34 211 toolow=1;
mptapton 1:c8ec50d75f80 212 }
mptapton 1:c8ec50d75f80 213 else if (led_high){
mptapton 1:c8ec50d75f80 214 lcd.printf("Tuner- 2High ");
mptapton 2:4be1b96a5a34 215 intune=0;
mptapton 2:4be1b96a5a34 216 toohigh=1;
mptapton 2:4be1b96a5a34 217 toolow=0;
mptapton 1:c8ec50d75f80 218 }
mptapton 1:c8ec50d75f80 219 Counter = 0;
mptapton 2:4be1b96a5a34 220 txcounter++;
mptapton 2:4be1b96a5a34 221 if (txcounter == 1)// First time around
mptapton 2:4be1b96a5a34 222 {
mptapton 2:4be1b96a5a34 223 device.putc(chordkeyint); //SEND INITIAL CHORD LETTER TO MBED2 (ONLY ONCE)per button press or txcounter
mptapton 2:4be1b96a5a34 224 // pc.putc('1');
mptapton 2:4be1b96a5a34 225 // pc.putc(chordkeyint);
mptapton 2:4be1b96a5a34 226 }
mptapton 2:4be1b96a5a34 227 if (txcounter %4 == 0)// send update chord letter to mbed2 {
mptapton 2:4be1b96a5a34 228 {
mptapton 2:4be1b96a5a34 229 device.putc(chordkeyint); //SEND INITIAL CHORD LETTER TO MBED2 (ONLY ONCE)per button press or txcounter
mptapton 2:4be1b96a5a34 230 // pc.putc('1');
mptapton 2:4be1b96a5a34 231 // pc.putc(chordkeyint);
mptapton 2:4be1b96a5a34 232 }
mptapton 2:4be1b96a5a34 233
mptapton 2:4be1b96a5a34 234 } //inner tuner while (1)loop
mptapton 2:4be1b96a5a34 235
mptapton 2:4be1b96a5a34 236 } else { //if myinputpin Chord or Tuner mode = Chord mode selected
mptapton 2:4be1b96a5a34 237
mptapton 2:4be1b96a5a34 238 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
mptapton 2:4be1b96a5a34 239 // ..............CHORD MODE.........................
mptapton 2:4be1b96a5a34 240
mptapton 0:6b0b61d411ad 241 lcd.cls (); //Chord Tutor Section based on p21 being 0v
mptapton 0:6b0b61d411ad 242
mptapton 0:6b0b61d411ad 243 //Interupt for Switching chord selection
mptapton 0:6b0b61d411ad 244 button1.mode(PullDown);
mptapton 0:6b0b61d411ad 245 button1.rise(&button1_pressed);
mptapton 0:6b0b61d411ad 246
mptapton 0:6b0b61d411ad 247 while (1) {
mptapton 0:6b0b61d411ad 248
mptapton 0:6b0b61d411ad 249 switch (chord_select) {
mptapton 0:6b0b61d411ad 250 case 0:
mptapton 0:6b0b61d411ad 251 note = 82;
mptapton 2:4be1b96a5a34 252 chordkey= "E";//E2 send E chord white LED pattern
mptapton 2:4be1b96a5a34 253 chordkeyint=69; //E in ASCII
mptapton 0:6b0b61d411ad 254 break;
mptapton 0:6b0b61d411ad 255 case 1:
mptapton 0:6b0b61d411ad 256 note = 110;
mptapton 2:4be1b96a5a34 257 chordkey= "A";//A chord
mptapton 2:4be1b96a5a34 258 chordkeyint=65; //A in ASCII
mptapton 0:6b0b61d411ad 259 break;
mptapton 0:6b0b61d411ad 260 case 2:
mptapton 0:6b0b61d411ad 261 note = 147;
mptapton 2:4be1b96a5a34 262 chordkey= "D";//D3 chord
mptapton 2:4be1b96a5a34 263 chordkeyint=68; //D in ASCII
mptapton 0:6b0b61d411ad 264 break;
mptapton 0:6b0b61d411ad 265 case 3:
mptapton 0:6b0b61d411ad 266 note = 196;
mptapton 2:4be1b96a5a34 267 chordkey= "G";//G3 chord
mptapton 2:4be1b96a5a34 268 chordkeyint=71; //G in ASCII
mptapton 0:6b0b61d411ad 269 break;
mptapton 0:6b0b61d411ad 270 case 4:
mptapton 0:6b0b61d411ad 271 note = 247;
mptapton 2:4be1b96a5a34 272 chordkey= "B";//B3 chord
mptapton 2:4be1b96a5a34 273 chordkeyint=66; //B in ASCII
mptapton 0:6b0b61d411ad 274 break;
mptapton 0:6b0b61d411ad 275 case 5:
mptapton 2:4be1b96a5a34 276 note = 131;
mptapton 2:4be1b96a5a34 277 chordkey= "C";//C3 chord
mptapton 2:4be1b96a5a34 278 chordkeyint=67; //C in ASCII
mptapton 0:6b0b61d411ad 279 break;
mptapton 0:6b0b61d411ad 280 case 6:
mptapton 2:4be1b96a5a34 281 note = 87;
mptapton 2:4be1b96a5a34 282 chordkey= "F";//F2 chord
mptapton 2:4be1b96a5a34 283 chordkeyint=70; //F in ASCII
mptapton 0:6b0b61d411ad 284 break;
mptapton 2:4be1b96a5a34 285 }
mptapton 2:4be1b96a5a34 286
mptapton 0:6b0b61d411ad 287 //Prepare for burst mode on all ADC pins and set up interrupt handler (using ADC library from Simon Blandford
mptapton 0:6b0b61d411ad 288 adc.append(sample_audio);
mptapton 0:6b0b61d411ad 289 adc.startmode(0,0);
mptapton 0:6b0b61d411ad 290 adc.burst(1);
mptapton 0:6b0b61d411ad 291 adc.setup(p20,1);
mptapton 0:6b0b61d411ad 292
mptapton 0:6b0b61d411ad 293 //start the interrupt and wait for about 4096 samples
mptapton 0:6b0b61d411ad 294 adc.interrupt_state(p20,1);
mptapton 0:6b0b61d411ad 295 wait(.2);
mptapton 0:6b0b61d411ad 296
mptapton 0:6b0b61d411ad 297 //Finsh up - Unset pin 20
mptapton 0:6b0b61d411ad 298 adc.interrupt_state(p20,0);
mptapton 0:6b0b61d411ad 299 adc.setup(p20,0);
mptapton 0:6b0b61d411ad 300 int actual_rate = adc.actual_sample_rate();
mptapton 0:6b0b61d411ad 301
mptapton 0:6b0b61d411ad 302 //for debugging tell the terminal sample rate and how many samples we took
mptapton 2:4be1b96a5a34 303 //printf("Requested max sample rate is %u, actual max sample rate is %u.\n",
mptapton 2:4be1b96a5a34 304 // SAMPLE_RATE, actual_rate);
mptapton 2:4be1b96a5a34 305 //printf("We did %i samples\n",Counter);
mptapton 0:6b0b61d411ad 306
mptapton 0:6b0b61d411ad 307 high = 0;
mptapton 0:6b0b61d411ad 308 low = 0;
mptapton 0:6b0b61d411ad 309 for (int i=3; i<46; i+=3) {
mptapton 0:6b0b61d411ad 310 high1 = goertzelFilter(Buffer, (note + i ), Counter);
mptapton 0:6b0b61d411ad 311 if (high1 > high) high=high1;
mptapton 0:6b0b61d411ad 312 }
mptapton 0:6b0b61d411ad 313 for (int i=3; i<46; i+=3) {
mptapton 0:6b0b61d411ad 314 low1 = goertzelFilter(Buffer, (note - i ), Counter);
mptapton 0:6b0b61d411ad 315 if (low1 > low) low=low1;
mptapton 0:6b0b61d411ad 316 }
mptapton 0:6b0b61d411ad 317 in_tune1 = goertzelFilter(Buffer, (note+1), Counter);
mptapton 0:6b0b61d411ad 318 in_tune2 = goertzelFilter(Buffer, note, Counter);
mptapton 0:6b0b61d411ad 319 in_tune3 = goertzelFilter(Buffer, (note-1), Counter);
mptapton 0:6b0b61d411ad 320
mptapton 0:6b0b61d411ad 321 if ((in_tune1 > in_tune2) && (in_tune1 > in_tune3)) in_tune = in_tune1;
mptapton 0:6b0b61d411ad 322 else if ((in_tune2 > in_tune1) && (in_tune2 > in_tune3)) in_tune = in_tune2;
mptapton 0:6b0b61d411ad 323 else in_tune = in_tune3;
mptapton 0:6b0b61d411ad 324 if ((in_tune > high) && (in_tune > low)) {
mptapton 0:6b0b61d411ad 325 led_high = 0;
mptapton 2:4be1b96a5a34 326 led_ok = 1; // <<IN TUNE>>
mptapton 0:6b0b61d411ad 327 led_low = 0;
mptapton 2:4be1b96a5a34 328 // toohigh=0;
mptapton 2:4be1b96a5a34 329 // toolow=0;
mptapton 2:4be1b96a5a34 330 // intune=1;
mptapton 2:4be1b96a5a34 331 intunecounter++; // increment the intune counter
mptapton 0:6b0b61d411ad 332 } else if (high > in_tune) {
mptapton 2:4be1b96a5a34 333 led_high = 1; // <<TOO HIGH>>
mptapton 0:6b0b61d411ad 334 led_ok = 0;
mptapton 0:6b0b61d411ad 335 led_low = 0;
mptapton 2:4be1b96a5a34 336 // toohigh=1;
mptapton 2:4be1b96a5a34 337 // toolow=0;
mptapton 2:4be1b96a5a34 338 // intune=0;
mptapton 0:6b0b61d411ad 339 } else if (low > in_tune) {
mptapton 0:6b0b61d411ad 340 led_high = 0;
mptapton 0:6b0b61d411ad 341 led_ok = 0;
mptapton 2:4be1b96a5a34 342 led_low = 1; // <<TOO LOW>>
mptapton 2:4be1b96a5a34 343 // toohigh=0;
mptapton 2:4be1b96a5a34 344 // toolow=1;
mptapton 2:4be1b96a5a34 345 // intune=0;
mptapton 0:6b0b61d411ad 346 } else {
mptapton 2:4be1b96a5a34 347 led_high = 0; // not sure if we ever get here
mptapton 0:6b0b61d411ad 348 led_ok = 0;
mptapton 0:6b0b61d411ad 349 led_low = 0;
mptapton 2:4be1b96a5a34 350 // toohigh=0;
mptapton 2:4be1b96a5a34 351 // toolow=0;
mptapton 2:4be1b96a5a34 352 // intune=0;
mptapton 0:6b0b61d411ad 353 }
mptapton 0:6b0b61d411ad 354
mptapton 0:6b0b61d411ad 355 int pintwenty = adc.read(p20); //read pin 20
mptapton 0:6b0b61d411ad 356 lcd.locate(0,1);
mptapton 2:4be1b96a5a34 357 lcd.printf("%s ",chordkey);
mptapton 0:6b0b61d411ad 358 lcd.locate(4,1);// need to deal with lcd screen changes to length of frequencies
mptapton 0:6b0b61d411ad 359 lcd.printf(" ");
mptapton 0:6b0b61d411ad 360 lcd.locate(4,1);
mptapton 0:6b0b61d411ad 361 lcd.printf("%iHz",(int) note);
mptapton 0:6b0b61d411ad 362 lcd.locate(11,1);
mptapton 0:6b0b61d411ad 363 lcd.printf("%4d\n",pintwenty); //need to deal with lcd screen changes to restrict input decimal range to 4sf
mptapton 2:4be1b96a5a34 364 txcounter++; //increment transmit counter acting as a timer before sending chord value to mbed2
mptapton 2:4be1b96a5a34 365 // if (txcounter>txcountmax+20){txcounter=0;}//if there is a counter overrun fix it here
mptapton 2:4be1b96a5a34 366 lcd.printf("Play %s ",chordkey); //send initial message to LCD screen
mptapton 2:4be1b96a5a34 367
mptapton 2:4be1b96a5a34 368 if (txcounter == 1)// First time around
mptapton 2:4be1b96a5a34 369 {
mptapton 2:4be1b96a5a34 370 device.putc(chordkeyint); //SEND INITIAL CHORD LETTER TO MBED2 (ONLY ONCE)per button press or txcounter
mptapton 2:4be1b96a5a34 371 pc.putc('1');
mptapton 2:4be1b96a5a34 372 pc.putc(chordkeyint);
mptapton 2:4be1b96a5a34 373 // intune=0; //reset status pins to mbed2
mptapton 2:4be1b96a5a34 374 // toohigh=0;
mptapton 2:4be1b96a5a34 375 // toolow=0;
mptapton 2:4be1b96a5a34 376 // state=0;
mptapton 1:c8ec50d75f80 377 }
mptapton 2:4be1b96a5a34 378 // else
mptapton 2:4be1b96a5a34 379 // {
mptapton 2:4be1b96a5a34 380 //pc.putc('z');
mptapton 2:4be1b96a5a34 381 // }
mptapton 2:4be1b96a5a34 382 if (txcounter == txcounttrig) //if more than txcounttrig of sample time then set the corrrect outputs to mbed2
mptapton 2:4be1b96a5a34 383 { //for more lengthy intune/out tune notification
mptapton 2:4be1b96a5a34 384 state=1; // ONLY SET THIS THRESHOLD CONDITION TO mbed 2 ONCE till button press r txcounter max
mptapton 2:4be1b96a5a34 385 pc.putc('2');
mptapton 2:4be1b96a5a34 386 if ((led_ok)||(intunecounter >1 ))
mptapton 2:4be1b96a5a34 387 {
mptapton 2:4be1b96a5a34 388 intune=1; //its intune OR BEEN in tune so hold this until
mptapton 2:4be1b96a5a34 389 toohigh=0;
mptapton 2:4be1b96a5a34 390 toolow=0;
mptapton 2:4be1b96a5a34 391 pc.putc('3');
mptapton 2:4be1b96a5a34 392 }
mptapton 2:4be1b96a5a34 393 else if ((led_high)||(led_low))
mptapton 2:4be1b96a5a34 394 {
mptapton 2:4be1b96a5a34 395 pc.putc('4');
mptapton 2:4be1b96a5a34 396 intune=0;
mptapton 2:4be1b96a5a34 397 toohigh=1;// out of tune so set both output pins to mbed2
mptapton 2:4be1b96a5a34 398 toolow=1;
mptapton 2:4be1b96a5a34 399 }
mptapton 1:c8ec50d75f80 400 }
mptapton 2:4be1b96a5a34 401
mptapton 2:4be1b96a5a34 402 if (led_ok) // <<IN TUNE>>
mptapton 2:4be1b96a5a34 403 {
mptapton 2:4be1b96a5a34 404 lcd.locate(0,0);
mptapton 2:4be1b96a5a34 405 lcd.printf("Play %s->In Tune",chordkey); //to LCD screen
mptapton 2:4be1b96a5a34 406 pc.putc('5'); // dont use printf as buffering issues
mptapton 2:4be1b96a5a34 407 if ((txcounter>=txcountmax)&& (intunecounter>=intunetrig))
mptapton 2:4be1b96a5a34 408 { //keep going until time to decide if intune or not
mptapton 2:4be1b96a5a34 409 pc.putc('6'); // send to pc usb- dont use printf as buffering issues
mptapton 2:4be1b96a5a34 410 // IN TUNE FOR LONG ENOUGH AND TEST TIME ENDEDif (intunecounter >= intunetrig)
mptapton 2:4be1b96a5a34 411 // attempt has been IN TUNE for suffcient time to be deemed overall intune
mptapton 2:4be1b96a5a34 412 // pc.putc('4');// send to pc usb- dont use printf as buffering issues
mptapton 2:4be1b96a5a34 413 intune=0; //clear all pins to mbed2
mptapton 2:4be1b96a5a34 414 toohigh=0;
mptapton 2:4be1b96a5a34 415 toolow=0;
mptapton 2:4be1b96a5a34 416 state=0;
mptapton 2:4be1b96a5a34 417 intunecounter=0; //reset for next test
mptapton 2:4be1b96a5a34 418 txcounter=0; // reset for next test
mptapton 2:4be1b96a5a34 419 }
mptapton 2:4be1b96a5a34 420 if (txcounter>=txcountmax)
mptapton 2:4be1b96a5a34 421 { //IN TUNE, TEST TME UP BUT NOT IN TUNE LONG ENOUGH
mptapton 2:4be1b96a5a34 422 pc.putc('7'); // attempt time is up attempt was NOT in tune long enough
mptapton 2:4be1b96a5a34 423 intune=0; //clear all pins to mbed2
mptapton 2:4be1b96a5a34 424 toohigh=0;
mptapton 2:4be1b96a5a34 425 toolow=0;
mptapton 2:4be1b96a5a34 426 state=0;
mptapton 2:4be1b96a5a34 427 intunecounter=0; //reset for next test
mptapton 2:4be1b96a5a34 428 txcounter=0; // reset for next test
mptapton 2:4be1b96a5a34 429 }
mptapton 2:4be1b96a5a34 430 }
mptapton 2:4be1b96a5a34 431 if ((led_high)||(led_low))
mptapton 2:4be1b96a5a34 432 { // <<OUT of TUNE>> either too high or 2 low
mptapton 2:4be1b96a5a34 433 lcd.locate(0,0);
mptapton 2:4be1b96a5a34 434 lcd.printf("Play %s -> Nope ",chordkey);
mptapton 2:4be1b96a5a34 435 pc.putc('8');// send to pc usb- dont use printf as buffering issues
mptapton 2:4be1b96a5a34 436 // Counter = 0; //reset number of samples counter used in algorithm
mptapton 2:4be1b96a5a34 437 if ((txcounter>=txcountmax)&& (intunecounter>=intunetrig))
mptapton 2:4be1b96a5a34 438 { // OUT OF TUNE, TEST TIME UP, BUT BEEN INTUNE FOR LONG ENOUGH
mptapton 2:4be1b96a5a34 439 // if (txcounter<=txcountmax) //keep going until time to decide if intune or not
mptapton 2:4be1b96a5a34 440 // {
mptapton 2:4be1b96a5a34 441 // device.printf("%s", chordkey);// send chord letter to mbed 2
mptapton 2:4be1b96a5a34 442 pc.putc('9'); // send to pc usb- dont use printf as buffering issues
mptapton 2:4be1b96a5a34 443 // if (intunecounter >=intunetrig)
mptapton 2:4be1b96a5a34 444 // { // attempt has been IN TUNE for suffcient time to be deemed overall intune
mptapton 2:4be1b96a5a34 445 // pc.putc('7'); // even though its currently out of tune
mptapton 2:4be1b96a5a34 446 intune=0; //clear output pins to mbed2
mptapton 2:4be1b96a5a34 447 toohigh=0;
mptapton 2:4be1b96a5a34 448 toolow=0;
mptapton 2:4be1b96a5a34 449 state=0;
mptapton 2:4be1b96a5a34 450 intunecounter=0; //reset for next test
mptapton 2:4be1b96a5a34 451 txcounter=0; // reset for next test
mptapton 2:4be1b96a5a34 452 }
mptapton 2:4be1b96a5a34 453 if (txcounter>=txcountmax)
mptapton 2:4be1b96a5a34 454 { //OUT OF TUNE, TEST TME UP BUT NOT IN TUNE LONG ENOUGH
mptapton 2:4be1b96a5a34 455 pc.putc('a');// send to pc usb- dont use printf as buffering issues
mptapton 2:4be1b96a5a34 456 intune=0; //set intune pin to 0 for mbed2 to read
mptapton 2:4be1b96a5a34 457 toohigh=0; // set out of tune pins to 1 for mbed2 to read
mptapton 2:4be1b96a5a34 458 toolow=0;
mptapton 2:4be1b96a5a34 459 state=0;
mptapton 2:4be1b96a5a34 460 intunecounter=0; //reset for next test
mptapton 2:4be1b96a5a34 461 txcounter=0; // reset for next test
mptapton 2:4be1b96a5a34 462 }
mptapton 2:4be1b96a5a34 463 }
mptapton 2:4be1b96a5a34 464 Counter=0;
mptapton 2:4be1b96a5a34 465 pc.printf("(%i %i)\n", txcounter, intunecounter); //ok to use printf here as uses \n to clear buffer
mptapton 2:4be1b96a5a34 466 // pc.putc(txcounter);
mptapton 2:4be1b96a5a34 467 // pc.putc(' ');
mptapton 2:4be1b96a5a34 468 }
mptapton 2:4be1b96a5a34 469
mptapton 2:4be1b96a5a34 470 }
mptapton 2:4be1b96a5a34 471 }
mptapton 2:4be1b96a5a34 472 }