HW 6.2 Spring 2015 NMHU New Mexico Highlands. Changing Octaves on KL tap sensor project.
Fork of slider_diatonic_v1 by
slider_diatonic_v1.cpp@4:761f247c603e, 2015-02-23 (annotated)
- Committer:
- bomalley
- Date:
- Mon Feb 23 06:08:03 2015 +0000
- Revision:
- 4:761f247c603e
- Parent:
- 3:f68e9cdfaf2d
HW 6.2 Spring 2015 NMHU
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 2:7f347d6a6422 | 1 | #include "mbed.h" |
scohennm | 2:7f347d6a6422 | 2 | #include "SLCD.h" |
scohennm | 2:7f347d6a6422 | 3 | #include "TSISensor.h" |
scohennm | 2:7f347d6a6422 | 4 | |
scohennm | 2:7f347d6a6422 | 5 | |
scohennm | 2:7f347d6a6422 | 6 | #define CHANNELON 0 |
scohennm | 2:7f347d6a6422 | 7 | #define CHANNELOFF 1 |
scohennm | 2:7f347d6a6422 | 8 | #define LCDLEN 10 |
scohennm | 3:f68e9cdfaf2d | 9 | #define DATATIME 100 //milli seccnds |
scohennm | 2:7f347d6a6422 | 10 | //LCD messages |
scohennm | 2:7f347d6a6422 | 11 | |
bomalley | 4:761f247c603e | 12 | //Button |
bomalley | 4:761f247c603e | 13 | #define BUTOFF 1 |
bomalley | 4:761f247c603e | 14 | #define BUTON 0 |
bomalley | 4:761f247c603e | 15 | #define BUTTON PTC3 |
scohennm | 2:7f347d6a6422 | 16 | |
scohennm | 2:7f347d6a6422 | 17 | // Operating parameters |
scohennm | 2:7f347d6a6422 | 18 | #define SIDETONE 700.0 |
scohennm | 2:7f347d6a6422 | 19 | #define TONEMIN 200.0 |
scohennm | 2:7f347d6a6422 | 20 | #define TONEINT 800.00 // So tone max is 1000 |
scohennm | 2:7f347d6a6422 | 21 | #define TONEON 0.50 |
scohennm | 2:7f347d6a6422 | 22 | #define TONEOFF 0.0 |
scohennm | 2:7f347d6a6422 | 23 | #define SPEEDAST 0 |
scohennm | 2:7f347d6a6422 | 24 | #define TONEAST 1 |
scohennm | 3:f68e9cdfaf2d | 25 | #define NUMTONES 10 |
scohennm | 3:f68e9cdfaf2d | 26 | #define LEDPERIOD 0.001 |
bomalley | 4:761f247c603e | 27 | |
bomalley | 4:761f247c603e | 28 | #define OCTAVECHANGE 2 //double to increase frequency |
scohennm | 3:f68e9cdfaf2d | 29 | //#define PRINTDEBUG |
scohennm | 2:7f347d6a6422 | 30 | |
scohennm | 3:f68e9cdfaf2d | 31 | Serial pc(USBTX, USBRX); |
scohennm | 3:f68e9cdfaf2d | 32 | |
bomalley | 4:761f247c603e | 33 | InterruptIn myButton(BUTTON);//button to change octaves |
bomalley | 4:761f247c603e | 34 | |
scohennm | 3:f68e9cdfaf2d | 35 | float diatonicScale[NUMTONES] = {246.94, 261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25,587.33}; |
scohennm | 2:7f347d6a6422 | 36 | SLCD slcd; //define LCD display |
scohennm | 2:7f347d6a6422 | 37 | |
scohennm | 2:7f347d6a6422 | 38 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 2:7f347d6a6422 | 39 | |
scohennm | 3:f68e9cdfaf2d | 40 | |
scohennm | 2:7f347d6a6422 | 41 | PwmOut led(LED_RED); |
scohennm | 2:7f347d6a6422 | 42 | DigitalOut outPin(PTC9); //J1-16 |
scohennm | 2:7f347d6a6422 | 43 | PwmOut soundOut(PTA13); |
scohennm | 3:f68e9cdfaf2d | 44 | Timer dataTimer; |
scohennm | 2:7f347d6a6422 | 45 | // Global scalars |
scohennm | 2:7f347d6a6422 | 46 | char lcdData[LCDLEN]; |
scohennm | 2:7f347d6a6422 | 47 | |
scohennm | 2:7f347d6a6422 | 48 | float tonePeriod; |
scohennm | 2:7f347d6a6422 | 49 | float toneFreq = SIDETONE; |
scohennm | 2:7f347d6a6422 | 50 | |
bomalley | 4:761f247c603e | 51 | int buttonState = BUTOFF; |
scohennm | 2:7f347d6a6422 | 52 | |
scohennm | 2:7f347d6a6422 | 53 | void LCDMessNoDwell(char *lMess){ |
scohennm | 2:7f347d6a6422 | 54 | slcd.Home(); |
scohennm | 2:7f347d6a6422 | 55 | slcd.clear(); |
scohennm | 2:7f347d6a6422 | 56 | slcd.printf(lMess); |
scohennm | 2:7f347d6a6422 | 57 | } |
scohennm | 2:7f347d6a6422 | 58 | |
scohennm | 3:f68e9cdfaf2d | 59 | void diatonicAdjust( float scaling) { |
scohennm | 2:7f347d6a6422 | 60 | int tempInt; |
scohennm | 3:f68e9cdfaf2d | 61 | int scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 62 | static int oldScaleIndex = 0; |
scohennm | 3:f68e9cdfaf2d | 63 | /* There appears to be a set up time for setting the PWM time period |
scohennm | 3:f68e9cdfaf2d | 64 | only do a nes set up if the indext changes. |
scohennm | 3:f68e9cdfaf2d | 65 | */ |
scohennm | 3:f68e9cdfaf2d | 66 | scaleIndex = (int)(NUMTONES * scaling); |
scohennm | 3:f68e9cdfaf2d | 67 | if (scaleIndex != oldScaleIndex) { |
bomalley | 4:761f247c603e | 68 | if(buttonState == BUTON) |
bomalley | 4:761f247c603e | 69 | { |
bomalley | 4:761f247c603e | 70 | toneFreq = diatonicScale[scaleIndex * OCTAVECHANGE]; |
bomalley | 4:761f247c603e | 71 | } |
bomalley | 4:761f247c603e | 72 | else |
bomalley | 4:761f247c603e | 73 | { |
bomalley | 4:761f247c603e | 74 | toneFreq = diatonicScale[scaleIndex]; |
bomalley | 4:761f247c603e | 75 | } |
scohennm | 3:f68e9cdfaf2d | 76 | tonePeriod = 1.0/toneFreq; |
scohennm | 3:f68e9cdfaf2d | 77 | soundOut.period(tonePeriod); // adjusting period |
scohennm | 3:f68e9cdfaf2d | 78 | soundOut.write(TONEON); // there is a setup time for both period and DF |
scohennm | 3:f68e9cdfaf2d | 79 | oldScaleIndex = scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 80 | } else { |
scohennm | 3:f68e9cdfaf2d | 81 | return; |
scohennm | 3:f68e9cdfaf2d | 82 | } |
scohennm | 3:f68e9cdfaf2d | 83 | #ifdef PRINTDEBUG |
scohennm | 3:f68e9cdfaf2d | 84 | pc.printf(" %f,%d\n\r",scaling, scaleIndex ); |
scohennm | 3:f68e9cdfaf2d | 85 | #endif |
scohennm | 2:7f347d6a6422 | 86 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 87 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 88 | LCDMessNoDwell(lcdData); |
scohennm | 2:7f347d6a6422 | 89 | return; |
scohennm | 2:7f347d6a6422 | 90 | } |
scohennm | 2:7f347d6a6422 | 91 | void lightAdjust( float scaling) { // Control brightness of LED |
scohennm | 2:7f347d6a6422 | 92 | float tempDutyFactor; |
scohennm | 2:7f347d6a6422 | 93 | |
scohennm | 3:f68e9cdfaf2d | 94 | tempDutyFactor = 1.0 - scaling; //LED is a sinking connection // anode is held at 5V |
scohennm | 3:f68e9cdfaf2d | 95 | led.write(tempDutyFactor); //adjusting duty factor |
scohennm | 2:7f347d6a6422 | 96 | return; |
scohennm | 2:7f347d6a6422 | 97 | } |
scohennm | 3:f68e9cdfaf2d | 98 | |
bomalley | 4:761f247c603e | 99 | void pressed() |
bomalley | 4:761f247c603e | 100 | { |
bomalley | 4:761f247c603e | 101 | buttonState = !buttonState; |
bomalley | 4:761f247c603e | 102 | } |
bomalley | 4:761f247c603e | 103 | |
scohennm | 2:7f347d6a6422 | 104 | int main(){ |
scohennm | 2:7f347d6a6422 | 105 | int tempInt; |
scohennm | 2:7f347d6a6422 | 106 | float tempValue; |
scohennm | 3:f68e9cdfaf2d | 107 | |
scohennm | 2:7f347d6a6422 | 108 | tonePeriod = 1.0/toneFreq; |
scohennm | 2:7f347d6a6422 | 109 | soundOut.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 110 | led.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 111 | led.write(CHANNELOFF); |
scohennm | 3:f68e9cdfaf2d | 112 | outPin.write(CHANNELOFF); |
scohennm | 2:7f347d6a6422 | 113 | |
scohennm | 2:7f347d6a6422 | 114 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 115 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 116 | LCDMessNoDwell(lcdData); |
scohennm | 3:f68e9cdfaf2d | 117 | // Start data timer |
scohennm | 3:f68e9cdfaf2d | 118 | dataTimer.start(); |
scohennm | 3:f68e9cdfaf2d | 119 | dataTimer.reset(); |
bomalley | 4:761f247c603e | 120 | |
scohennm | 2:7f347d6a6422 | 121 | while (true) { |
bomalley | 4:761f247c603e | 122 | myButton.fall(&pressed); |
scohennm | 3:f68e9cdfaf2d | 123 | if (dataTimer.read_ms() > DATATIME){ // check to see if enough time has passed |
scohennm | 3:f68e9cdfaf2d | 124 | // to read the touch pad |
scohennm | 3:f68e9cdfaf2d | 125 | tempValue = tsiScaling.readPercentage(); |
scohennm | 3:f68e9cdfaf2d | 126 | if(tempValue > 0) { |
scohennm | 3:f68e9cdfaf2d | 127 | // soundOut.write(TONEON); // set duty factor to 50% |
scohennm | 3:f68e9cdfaf2d | 128 | diatonicAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 129 | lightAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 130 | } else { |
scohennm | 3:f68e9cdfaf2d | 131 | soundOut.write(TONEOFF); // set dutyfactor to 0% |
scohennm | 3:f68e9cdfaf2d | 132 | LCDMessNoDwell("SOFF"); |
scohennm | 3:f68e9cdfaf2d | 133 | } |
scohennm | 3:f68e9cdfaf2d | 134 | dataTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 135 | } |
scohennm | 2:7f347d6a6422 | 136 | } // while forever |
scohennm | 2:7f347d6a6422 | 137 | }// end main |