Adapted code from original GT_Tuner code (by Andrew Durand) for a school project by Tapton School.

Dependencies:   mbed

Committer:
mptapton
Date:
Mon Feb 06 09:30:34 2017 +0000
Revision:
2:c242fd25e7e2
Parent:
1:c8ec50d75f80
Tapton School guitar project

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:c242fd25e7e2 12 Serial pc(USBTX, USBRX); // tx, rx
mptapton 2:c242fd25e7e2 13
mptapton 2:c242fd25e7e2 14 DigitalOut intune(p22);
mptapton 2:c242fd25e7e2 15 DigitalOut toohigh(p23);
mptapton 2:c242fd25e7e2 16 DigitalOut toolow(p24);
mptapton 2:c242fd25e7e2 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:c242fd25e7e2 23 DigitalIn myInputPin (p21); //select tuner or chord learning mode
mptapton 2:c242fd25e7e2 24
mptapton 2:c242fd25e7e2 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:c242fd25e7e2 34 TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7
mptapton 0:6b0b61d411ad 35
mptapton 2:c242fd25e7e2 36 int intunetrig=2; //set max times in tune for before triggering intune to mbed2
mptapton 2:c242fd25e7e2 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:c242fd25e7e2 38 int txcounttrig=2; // set threshold for triggering success or not
mptapton 2:c242fd25e7e2 39 int txcounter=0; //set counter to control transmit message timing to mbed2
mptapton 2:c242fd25e7e2 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:c242fd25e7e2 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:c242fd25e7e2 77 if (chord_select > 6) chord_select = 0;//change for number of chords supported
mptapton 2:c242fd25e7e2 78 intune=0; //clear all pins to mbed2
mptapton 2:c242fd25e7e2 79 toohigh=0;
mptapton 2:c242fd25e7e2 80 toolow=0;
mptapton 2:c242fd25e7e2 81 state=0;
mptapton 2:c242fd25e7e2 82 txcounter=0; //set counter to control transmit message timing to mbed2
mptapton 2:c242fd25e7e2 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:c242fd25e7e2 87 pc.baud(115200);
mptapton 2:c242fd25e7e2 88 device.baud(19200); //mbed to mbed serial communication speed
mptapton 2:c242fd25e7e2 89 txcounter=0;
mptapton 2:c242fd25e7e2 90 intunecounter=0;
mptapton 2:c242fd25e7e2 91 intune=0; //clear all pins to mbed2
mptapton 2:c242fd25e7e2 92 toohigh=0;
mptapton 2:c242fd25e7e2 93 toolow=0;
mptapton 2:c242fd25e7e2 94 state=0;
mptapton 2:c242fd25e7e2 95 int chordkeyint=0;
mptapton 2:c242fd25e7e2 96 setbuf(stdout, NULL);
mptapton 2:c242fd25e7e2 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:c242fd25e7e2 112 key= "E"; //E2
mptapton 0:6b0b61d411ad 113 break;
mptapton 0:6b0b61d411ad 114 case 1:
mptapton 0:6b0b61d411ad 115 note = 110;
mptapton 2:c242fd25e7e2 116 key= "A";//A2
mptapton 0:6b0b61d411ad 117 break;
mptapton 0:6b0b61d411ad 118 case 2:
mptapton 0:6b0b61d411ad 119 note = 147;
mptapton 2:c242fd25e7e2 120 key= "D";//D3
mptapton 0:6b0b61d411ad 121 break;
mptapton 0:6b0b61d411ad 122 case 3:
mptapton 0:6b0b61d411ad 123 note = 196;
mptapton 2:c242fd25e7e2 124 key= "G";//G3
mptapton 0:6b0b61d411ad 125 break;
mptapton 0:6b0b61d411ad 126 case 4:
mptapton 0:6b0b61d411ad 127 note = 247;
mptapton 2:c242fd25e7e2 128 key= "B";//B3
mptapton 0:6b0b61d411ad 129 break;
mptapton 0:6b0b61d411ad 130 case 5:
mptapton 0:6b0b61d411ad 131 note = 330;
mptapton 2:c242fd25e7e2 132 key= "E";//E4
mptapton 0:6b0b61d411ad 133 break;
mptapton 0:6b0b61d411ad 134 }
mptapton 0:6b0b61d411ad 135
mptapton 0:6b0b61d411ad 136 //Prepare for burst mode on all ADC pins and set up interrupt handler (using ADC library from Simon Blandford
mptapton 0:6b0b61d411ad 137 adc.append(sample_audio);
mptapton 0:6b0b61d411ad 138 adc.startmode(0,0);
mptapton 0:6b0b61d411ad 139 adc.burst(1);
mptapton 0:6b0b61d411ad 140 adc.setup(p20,1);
mptapton 0:6b0b61d411ad 141
mptapton 0:6b0b61d411ad 142 //start the interrupt and wait for about 4096 samples
mptapton 0:6b0b61d411ad 143 adc.interrupt_state(p20,1);
mptapton 0:6b0b61d411ad 144 wait(.2);
mptapton 0:6b0b61d411ad 145
mptapton 0:6b0b61d411ad 146 //Finsh up - Unset pin 20
mptapton 0:6b0b61d411ad 147 adc.interrupt_state(p20,0);
mptapton 0:6b0b61d411ad 148 adc.setup(p20,0);
mptapton 0:6b0b61d411ad 149 int actual_rate = adc.actual_sample_rate();
mptapton 0:6b0b61d411ad 150
mptapton 0:6b0b61d411ad 151 //for debugging tell the terminal sample rate and how many samples we took
mptapton 2:c242fd25e7e2 152 //printf("Requested max sample rate is %u, actual max sample rate is %u.\n",
mptapton 2:c242fd25e7e2 153 // SAMPLE_RATE, actual_rate);
mptapton 2:c242fd25e7e2 154 // printf("We did %i samples\n",Counter);
mptapton 0:6b0b61d411ad 155
mptapton 0:6b0b61d411ad 156 high = 0;
mptapton 0:6b0b61d411ad 157 low = 0;
mptapton 0:6b0b61d411ad 158 for (int i=3; i<46; i+=3) {
mptapton 0:6b0b61d411ad 159 high1 = goertzelFilter(Buffer, (note + i ), Counter);
mptapton 0:6b0b61d411ad 160 if (high1 > high) high=high1;
mptapton 0:6b0b61d411ad 161 }
mptapton 0:6b0b61d411ad 162 for (int i=3; i<46; i+=3) {
mptapton 0:6b0b61d411ad 163 low1 = goertzelFilter(Buffer, (note - i ), Counter);
mptapton 0:6b0b61d411ad 164 if (low1 > low) low=low1;
mptapton 0:6b0b61d411ad 165 }
mptapton 0:6b0b61d411ad 166 in_tune1 = goertzelFilter(Buffer, (note+1), Counter);
mptapton 0:6b0b61d411ad 167 in_tune2 = goertzelFilter(Buffer, note, Counter);
mptapton 0:6b0b61d411ad 168 in_tune3 = goertzelFilter(Buffer, (note-1), Counter);
mptapton 0:6b0b61d411ad 169
mptapton 0:6b0b61d411ad 170 if ((in_tune1 > in_tune2) && (in_tune1 > in_tune3)) in_tune = in_tune1;
mptapton 0:6b0b61d411ad 171 else if ((in_tune2 > in_tune1) && (in_tune2 > in_tune3)) in_tune = in_tune2;
mptapton 0:6b0b61d411ad 172 else in_tune = in_tune3;
mptapton 0:6b0b61d411ad 173
mptapton 0:6b0b61d411ad 174 if ((in_tune > high) && (in_tune > low)) {
mptapton 0:6b0b61d411ad 175 led_high = 0;
mptapton 0:6b0b61d411ad 176 led_ok = 1;
mptapton 0:6b0b61d411ad 177 led_low = 0;
mptapton 0:6b0b61d411ad 178 } else if (high > in_tune) {
mptapton 0:6b0b61d411ad 179 led_high = 1;
mptapton 0:6b0b61d411ad 180 led_ok = 0;
mptapton 0:6b0b61d411ad 181 led_low = 0;
mptapton 0:6b0b61d411ad 182 } else if (low > in_tune) {
mptapton 0:6b0b61d411ad 183 led_high = 0;
mptapton 0:6b0b61d411ad 184 led_ok = 0;
mptapton 0:6b0b61d411ad 185 led_low = 1;
mptapton 0:6b0b61d411ad 186 } else {
mptapton 0:6b0b61d411ad 187 led_high = 0;
mptapton 0:6b0b61d411ad 188 led_ok = 0;
mptapton 0:6b0b61d411ad 189 led_low = 0;
mptapton 0:6b0b61d411ad 190 }
mptapton 0:6b0b61d411ad 191
mptapton 0:6b0b61d411ad 192 int pintwenty = adc.read(p20); //read pin 20
mptapton 0:6b0b61d411ad 193 lcd.locate(0,1);
mptapton 0:6b0b61d411ad 194 lcd.printf("%s %iHz %d\n",key, (int) note, pintwenty);
mptapton 1:c8ec50d75f80 195 if (led_ok) {
mptapton 1:c8ec50d75f80 196 lcd.printf("Tuner- In Tune");
mptapton 2:c242fd25e7e2 197 // device.printf("%s", key);// send to mbed 2
mptapton 1:c8ec50d75f80 198 }
mptapton 1:c8ec50d75f80 199 else if (led_low) {
mptapton 1:c8ec50d75f80 200 lcd.printf("Tuner- 2Low ");
mptapton 2:c242fd25e7e2 201 // device.printf("%s", key);// send to mbed 2
mptapton 1:c8ec50d75f80 202 }
mptapton 1:c8ec50d75f80 203 else if (led_high){
mptapton 1:c8ec50d75f80 204 lcd.printf("Tuner- 2High ");
mptapton 2:c242fd25e7e2 205 // device.printf("%s", key);// send to mbed 2
mptapton 1:c8ec50d75f80 206 }
mptapton 1:c8ec50d75f80 207 Counter = 0;
mptapton 2:c242fd25e7e2 208 } //inner tuner while (1)loop
mptapton 2:c242fd25e7e2 209
mptapton 2:c242fd25e7e2 210 } else { //if myinputpin Chord or Tuner mode = Chord mode selected
mptapton 2:c242fd25e7e2 211
mptapton 2:c242fd25e7e2 212 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
mptapton 2:c242fd25e7e2 213 // ..............CHORD MODE.........................
mptapton 2:c242fd25e7e2 214
mptapton 0:6b0b61d411ad 215 lcd.cls (); //Chord Tutor Section based on p21 being 0v
mptapton 0:6b0b61d411ad 216
mptapton 0:6b0b61d411ad 217 //Interupt for Switching chord selection
mptapton 0:6b0b61d411ad 218 button1.mode(PullDown);
mptapton 0:6b0b61d411ad 219 button1.rise(&button1_pressed);
mptapton 0:6b0b61d411ad 220
mptapton 0:6b0b61d411ad 221 while (1) {
mptapton 0:6b0b61d411ad 222
mptapton 0:6b0b61d411ad 223 switch (chord_select) {
mptapton 0:6b0b61d411ad 224 case 0:
mptapton 0:6b0b61d411ad 225 note = 82;
mptapton 2:c242fd25e7e2 226 chordkey= "E";//E2 send E chord white LED pattern
mptapton 2:c242fd25e7e2 227 chordkeyint=69; //E in ASCII
mptapton 0:6b0b61d411ad 228 break;
mptapton 0:6b0b61d411ad 229 case 1:
mptapton 0:6b0b61d411ad 230 note = 110;
mptapton 2:c242fd25e7e2 231 chordkey= "A";//A chord
mptapton 2:c242fd25e7e2 232 chordkeyint=65; //A in ASCII
mptapton 0:6b0b61d411ad 233 break;
mptapton 0:6b0b61d411ad 234 case 2:
mptapton 0:6b0b61d411ad 235 note = 147;
mptapton 2:c242fd25e7e2 236 chordkey= "D";//D3 chord
mptapton 2:c242fd25e7e2 237 chordkeyint=68; //D in ASCII
mptapton 0:6b0b61d411ad 238 break;
mptapton 0:6b0b61d411ad 239 case 3:
mptapton 0:6b0b61d411ad 240 note = 196;
mptapton 2:c242fd25e7e2 241 chordkey= "G";//G3 chord
mptapton 2:c242fd25e7e2 242 chordkeyint=71; //G in ASCII
mptapton 0:6b0b61d411ad 243 break;
mptapton 0:6b0b61d411ad 244 case 4:
mptapton 0:6b0b61d411ad 245 note = 247;
mptapton 2:c242fd25e7e2 246 chordkey= "B";//B3 chord
mptapton 2:c242fd25e7e2 247 chordkeyint=66; //B in ASCII
mptapton 0:6b0b61d411ad 248 break;
mptapton 0:6b0b61d411ad 249 case 5:
mptapton 2:c242fd25e7e2 250 note = 131;
mptapton 2:c242fd25e7e2 251 chordkey= "C";//C3 chord
mptapton 2:c242fd25e7e2 252 chordkeyint=67; //C in ASCII
mptapton 0:6b0b61d411ad 253 break;
mptapton 0:6b0b61d411ad 254 case 6:
mptapton 2:c242fd25e7e2 255 note = 87;
mptapton 2:c242fd25e7e2 256 chordkey= "F";//F2 chord
mptapton 2:c242fd25e7e2 257 chordkeyint=70; //F in ASCII
mptapton 0:6b0b61d411ad 258 break;
mptapton 2:c242fd25e7e2 259 }
mptapton 2:c242fd25e7e2 260
mptapton 0:6b0b61d411ad 261 //Prepare for burst mode on all ADC pins and set up interrupt handler (using ADC library from Simon Blandford
mptapton 0:6b0b61d411ad 262 adc.append(sample_audio);
mptapton 0:6b0b61d411ad 263 adc.startmode(0,0);
mptapton 0:6b0b61d411ad 264 adc.burst(1);
mptapton 0:6b0b61d411ad 265 adc.setup(p20,1);
mptapton 0:6b0b61d411ad 266
mptapton 0:6b0b61d411ad 267 //start the interrupt and wait for about 4096 samples
mptapton 0:6b0b61d411ad 268 adc.interrupt_state(p20,1);
mptapton 0:6b0b61d411ad 269 wait(.2);
mptapton 0:6b0b61d411ad 270
mptapton 0:6b0b61d411ad 271 //Finsh up - Unset pin 20
mptapton 0:6b0b61d411ad 272 adc.interrupt_state(p20,0);
mptapton 0:6b0b61d411ad 273 adc.setup(p20,0);
mptapton 0:6b0b61d411ad 274 int actual_rate = adc.actual_sample_rate();
mptapton 0:6b0b61d411ad 275
mptapton 0:6b0b61d411ad 276 //for debugging tell the terminal sample rate and how many samples we took
mptapton 2:c242fd25e7e2 277 //printf("Requested max sample rate is %u, actual max sample rate is %u.\n",
mptapton 2:c242fd25e7e2 278 // SAMPLE_RATE, actual_rate);
mptapton 2:c242fd25e7e2 279 //printf("We did %i samples\n",Counter);
mptapton 0:6b0b61d411ad 280
mptapton 0:6b0b61d411ad 281 high = 0;
mptapton 0:6b0b61d411ad 282 low = 0;
mptapton 0:6b0b61d411ad 283 for (int i=3; i<46; i+=3) {
mptapton 0:6b0b61d411ad 284 high1 = goertzelFilter(Buffer, (note + i ), Counter);
mptapton 0:6b0b61d411ad 285 if (high1 > high) high=high1;
mptapton 0:6b0b61d411ad 286 }
mptapton 0:6b0b61d411ad 287 for (int i=3; i<46; i+=3) {
mptapton 0:6b0b61d411ad 288 low1 = goertzelFilter(Buffer, (note - i ), Counter);
mptapton 0:6b0b61d411ad 289 if (low1 > low) low=low1;
mptapton 0:6b0b61d411ad 290 }
mptapton 0:6b0b61d411ad 291 in_tune1 = goertzelFilter(Buffer, (note+1), Counter);
mptapton 0:6b0b61d411ad 292 in_tune2 = goertzelFilter(Buffer, note, Counter);
mptapton 0:6b0b61d411ad 293 in_tune3 = goertzelFilter(Buffer, (note-1), Counter);
mptapton 0:6b0b61d411ad 294
mptapton 0:6b0b61d411ad 295 if ((in_tune1 > in_tune2) && (in_tune1 > in_tune3)) in_tune = in_tune1;
mptapton 0:6b0b61d411ad 296 else if ((in_tune2 > in_tune1) && (in_tune2 > in_tune3)) in_tune = in_tune2;
mptapton 0:6b0b61d411ad 297 else in_tune = in_tune3;
mptapton 0:6b0b61d411ad 298 if ((in_tune > high) && (in_tune > low)) {
mptapton 0:6b0b61d411ad 299 led_high = 0;
mptapton 2:c242fd25e7e2 300 led_ok = 1; // <<IN TUNE>>
mptapton 0:6b0b61d411ad 301 led_low = 0;
mptapton 2:c242fd25e7e2 302 // toohigh=0;
mptapton 2:c242fd25e7e2 303 // toolow=0;
mptapton 2:c242fd25e7e2 304 // intune=1;
mptapton 2:c242fd25e7e2 305 intunecounter++; // increment the intune counter
mptapton 0:6b0b61d411ad 306 } else if (high > in_tune) {
mptapton 2:c242fd25e7e2 307 led_high = 1; // <<TOO HIGH>>
mptapton 0:6b0b61d411ad 308 led_ok = 0;
mptapton 0:6b0b61d411ad 309 led_low = 0;
mptapton 2:c242fd25e7e2 310 // toohigh=1;
mptapton 2:c242fd25e7e2 311 // toolow=0;
mptapton 2:c242fd25e7e2 312 // intune=0;
mptapton 0:6b0b61d411ad 313 } else if (low > in_tune) {
mptapton 0:6b0b61d411ad 314 led_high = 0;
mptapton 0:6b0b61d411ad 315 led_ok = 0;
mptapton 2:c242fd25e7e2 316 led_low = 1; // <<TOO LOW>>
mptapton 2:c242fd25e7e2 317 // toohigh=0;
mptapton 2:c242fd25e7e2 318 // toolow=1;
mptapton 2:c242fd25e7e2 319 // intune=0;
mptapton 0:6b0b61d411ad 320 } else {
mptapton 2:c242fd25e7e2 321 led_high = 0; // not sure if we ever get here
mptapton 0:6b0b61d411ad 322 led_ok = 0;
mptapton 0:6b0b61d411ad 323 led_low = 0;
mptapton 2:c242fd25e7e2 324 // toohigh=0;
mptapton 2:c242fd25e7e2 325 // toolow=0;
mptapton 2:c242fd25e7e2 326 // intune=0;
mptapton 0:6b0b61d411ad 327 }
mptapton 0:6b0b61d411ad 328
mptapton 0:6b0b61d411ad 329 int pintwenty = adc.read(p20); //read pin 20
mptapton 0:6b0b61d411ad 330 lcd.locate(0,1);
mptapton 2:c242fd25e7e2 331 lcd.printf("%s ",chordkey);
mptapton 0:6b0b61d411ad 332 lcd.locate(4,1);// need to deal with lcd screen changes to length of frequencies
mptapton 0:6b0b61d411ad 333 lcd.printf(" ");
mptapton 0:6b0b61d411ad 334 lcd.locate(4,1);
mptapton 0:6b0b61d411ad 335 lcd.printf("%iHz",(int) note);
mptapton 0:6b0b61d411ad 336 lcd.locate(11,1);
mptapton 0:6b0b61d411ad 337 lcd.printf("%4d\n",pintwenty); //need to deal with lcd screen changes to restrict input decimal range to 4sf
mptapton 2:c242fd25e7e2 338 txcounter++; //increment transmit counter acting as a timer before sending chord value to mbed2
mptapton 2:c242fd25e7e2 339 // if (txcounter>txcountmax+20){txcounter=0;}//if there is a counter overrun fix it here
mptapton 2:c242fd25e7e2 340 lcd.printf("Play %s ",chordkey); //send initial message to LCD screen
mptapton 2:c242fd25e7e2 341
mptapton 2:c242fd25e7e2 342 if (txcounter == 1)// First time around
mptapton 2:c242fd25e7e2 343 {
mptapton 2:c242fd25e7e2 344 device.putc(chordkeyint); //SEND INITIAL CHORD LETTER TO MBED2 (ONLY ONCE)per button press or txcounter
mptapton 2:c242fd25e7e2 345 pc.putc('1');
mptapton 2:c242fd25e7e2 346 pc.putc(chordkeyint);
mptapton 2:c242fd25e7e2 347 // intune=0; //reset status pins to mbed2
mptapton 2:c242fd25e7e2 348 // toohigh=0;
mptapton 2:c242fd25e7e2 349 // toolow=0;
mptapton 2:c242fd25e7e2 350 // state=0;
mptapton 1:c8ec50d75f80 351 }
mptapton 2:c242fd25e7e2 352 // else
mptapton 2:c242fd25e7e2 353 // {
mptapton 2:c242fd25e7e2 354 //pc.putc('z');
mptapton 2:c242fd25e7e2 355 // }
mptapton 2:c242fd25e7e2 356 if (txcounter == txcounttrig) //if more than txcounttrig of sample time then set the corrrect outputs to mbed2
mptapton 2:c242fd25e7e2 357 { //for more lengthy intune/out tune notification
mptapton 2:c242fd25e7e2 358 state=1; // ONLY SET THIS THRESHOLD CONDITION TO mbed 2 ONCE till button press r txcounter max
mptapton 2:c242fd25e7e2 359 pc.putc('2');
mptapton 2:c242fd25e7e2 360 if ((led_ok)||(intunecounter >1 ))
mptapton 2:c242fd25e7e2 361 {
mptapton 2:c242fd25e7e2 362 intune=1; //its intune OR BEEN in tune so hold this until
mptapton 2:c242fd25e7e2 363 toohigh=0;
mptapton 2:c242fd25e7e2 364 toolow=0;
mptapton 2:c242fd25e7e2 365 pc.putc('3');
mptapton 2:c242fd25e7e2 366 }
mptapton 2:c242fd25e7e2 367 else if ((led_high)||(led_low))
mptapton 2:c242fd25e7e2 368 {
mptapton 2:c242fd25e7e2 369 pc.putc('4');
mptapton 2:c242fd25e7e2 370 intune=0;
mptapton 2:c242fd25e7e2 371 toohigh=1;// out of tune so set both output pins to mbed2
mptapton 2:c242fd25e7e2 372 toolow=1;
mptapton 2:c242fd25e7e2 373 }
mptapton 1:c8ec50d75f80 374 }
mptapton 2:c242fd25e7e2 375
mptapton 2:c242fd25e7e2 376 if (led_ok) // <<IN TUNE>>
mptapton 2:c242fd25e7e2 377 {
mptapton 2:c242fd25e7e2 378 lcd.locate(0,0);
mptapton 2:c242fd25e7e2 379 lcd.printf("Play %s->In Tune",chordkey); //to LCD screen
mptapton 2:c242fd25e7e2 380 pc.putc('5'); // dont use printf as buffering issues
mptapton 2:c242fd25e7e2 381 if ((txcounter>=txcountmax)&& (intunecounter>=intunetrig))
mptapton 2:c242fd25e7e2 382 { //keep going until time to decide if intune or not
mptapton 2:c242fd25e7e2 383 pc.putc('6'); // send to pc usb- dont use printf as buffering issues
mptapton 2:c242fd25e7e2 384 // IN TUNE FOR LONG ENOUGH AND TEST TIME ENDEDif (intunecounter >= intunetrig)
mptapton 2:c242fd25e7e2 385 // attempt has been IN TUNE for suffcient time to be deemed overall intune
mptapton 2:c242fd25e7e2 386 // pc.putc('4');// send to pc usb- dont use printf as buffering issues
mptapton 2:c242fd25e7e2 387 intune=0; //clear all pins to mbed2
mptapton 2:c242fd25e7e2 388 toohigh=0;
mptapton 2:c242fd25e7e2 389 toolow=0;
mptapton 2:c242fd25e7e2 390 state=0;
mptapton 2:c242fd25e7e2 391 intunecounter=0; //reset for next test
mptapton 2:c242fd25e7e2 392 txcounter=0; // reset for next test
mptapton 2:c242fd25e7e2 393 }
mptapton 2:c242fd25e7e2 394 if (txcounter>=txcountmax)
mptapton 2:c242fd25e7e2 395 { //IN TUNE, TEST TME UP BUT NOT IN TUNE LONG ENOUGH
mptapton 2:c242fd25e7e2 396 pc.putc('7'); // attempt time is up attempt was NOT in tune long enough
mptapton 2:c242fd25e7e2 397 intune=0; //clear all pins to mbed2
mptapton 2:c242fd25e7e2 398 toohigh=0;
mptapton 2:c242fd25e7e2 399 toolow=0;
mptapton 2:c242fd25e7e2 400 state=0;
mptapton 2:c242fd25e7e2 401 intunecounter=0; //reset for next test
mptapton 2:c242fd25e7e2 402 txcounter=0; // reset for next test
mptapton 2:c242fd25e7e2 403 }
mptapton 2:c242fd25e7e2 404 }
mptapton 2:c242fd25e7e2 405 if ((led_high)||(led_low))
mptapton 2:c242fd25e7e2 406 { // <<OUT of TUNE>> either too high or 2 low
mptapton 2:c242fd25e7e2 407 lcd.locate(0,0);
mptapton 2:c242fd25e7e2 408 lcd.printf("Play %s -> Nope ",chordkey);
mptapton 2:c242fd25e7e2 409 pc.putc('8');// send to pc usb- dont use printf as buffering issues
mptapton 2:c242fd25e7e2 410 // Counter = 0; //reset number of samples counter used in algorithm
mptapton 2:c242fd25e7e2 411 if ((txcounter>=txcountmax)&& (intunecounter>=intunetrig))
mptapton 2:c242fd25e7e2 412 { // OUT OF TUNE, TEST TIME UP, BUT BEEN INTUNE FOR LONG ENOUGH
mptapton 2:c242fd25e7e2 413 // if (txcounter<=txcountmax) //keep going until time to decide if intune or not
mptapton 2:c242fd25e7e2 414 // {
mptapton 2:c242fd25e7e2 415 // device.printf("%s", chordkey);// send chord letter to mbed 2
mptapton 2:c242fd25e7e2 416 pc.putc('9'); // send to pc usb- dont use printf as buffering issues
mptapton 2:c242fd25e7e2 417 // if (intunecounter >=intunetrig)
mptapton 2:c242fd25e7e2 418 // { // attempt has been IN TUNE for suffcient time to be deemed overall intune
mptapton 2:c242fd25e7e2 419 // pc.putc('7'); // even though its currently out of tune
mptapton 2:c242fd25e7e2 420 intune=0; //clear output pins to mbed2
mptapton 2:c242fd25e7e2 421 toohigh=0;
mptapton 2:c242fd25e7e2 422 toolow=0;
mptapton 2:c242fd25e7e2 423 state=0;
mptapton 2:c242fd25e7e2 424 intunecounter=0; //reset for next test
mptapton 2:c242fd25e7e2 425 txcounter=0; // reset for next test
mptapton 2:c242fd25e7e2 426 }
mptapton 2:c242fd25e7e2 427 if (txcounter>=txcountmax)
mptapton 2:c242fd25e7e2 428 { //OUT OF TUNE, TEST TME UP BUT NOT IN TUNE LONG ENOUGH
mptapton 2:c242fd25e7e2 429 pc.putc('a');// send to pc usb- dont use printf as buffering issues
mptapton 2:c242fd25e7e2 430 intune=0; //set intune pin to 0 for mbed2 to read
mptapton 2:c242fd25e7e2 431 toohigh=0; // set out of tune pins to 1 for mbed2 to read
mptapton 2:c242fd25e7e2 432 toolow=0;
mptapton 2:c242fd25e7e2 433 state=0;
mptapton 2:c242fd25e7e2 434 intunecounter=0; //reset for next test
mptapton 2:c242fd25e7e2 435 txcounter=0; // reset for next test
mptapton 2:c242fd25e7e2 436 }
mptapton 2:c242fd25e7e2 437 }
mptapton 2:c242fd25e7e2 438 Counter=0;
mptapton 2:c242fd25e7e2 439 pc.printf("(%i %i)\n", txcounter, intunecounter); //ok to use printf here as uses \n to clear buffer
mptapton 2:c242fd25e7e2 440 // pc.putc(txcounter);
mptapton 2:c242fd25e7e2 441 // pc.putc(' ');
mptapton 2:c242fd25e7e2 442 }
mptapton 2:c242fd25e7e2 443
mptapton 2:c242fd25e7e2 444 }
mptapton 2:c242fd25e7e2 445 }
mptapton 2:c242fd25e7e2 446 }