Added button interrupt to change from one octave to other
Fork of slider_diatonic_v1 by
slider_diatonic_v1.cpp@5:c047f46f8726, 2015-02-25 (annotated)
- Committer:
- vbharam
- Date:
- Wed Feb 25 18:30:23 2015 +0000
- Revision:
- 5:c047f46f8726
- Parent:
- 4:d59d946649b8
Added timer interrupt;
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 | #define CHANNELON 0 |
scohennm | 2:7f347d6a6422 | 6 | #define CHANNELOFF 1 |
scohennm | 2:7f347d6a6422 | 7 | #define LCDLEN 10 |
vbharam | 5:c047f46f8726 | 8 | #define DATATIME 50 //milli seccnds |
vbharam | 5:c047f46f8726 | 9 | #define PUSHTIME 300 //milli seccnds |
scohennm | 2:7f347d6a6422 | 10 | //LCD messages |
scohennm | 2:7f347d6a6422 | 11 | |
vbharam | 4:d59d946649b8 | 12 | #define TONEARRAY |
scohennm | 2:7f347d6a6422 | 13 | |
scohennm | 2:7f347d6a6422 | 14 | // Operating parameters |
scohennm | 2:7f347d6a6422 | 15 | #define SIDETONE 700.0 |
scohennm | 2:7f347d6a6422 | 16 | #define TONEMIN 200.0 |
scohennm | 2:7f347d6a6422 | 17 | #define TONEINT 800.00 // So tone max is 1000 |
scohennm | 2:7f347d6a6422 | 18 | #define TONEON 0.50 |
scohennm | 2:7f347d6a6422 | 19 | #define TONEOFF 0.0 |
scohennm | 2:7f347d6a6422 | 20 | #define SPEEDAST 0 |
scohennm | 2:7f347d6a6422 | 21 | #define TONEAST 1 |
scohennm | 3:f68e9cdfaf2d | 22 | #define NUMTONES 10 |
vbharam | 5:c047f46f8726 | 23 | #define NUMOCTS 2 |
scohennm | 3:f68e9cdfaf2d | 24 | #define LEDPERIOD 0.001 |
scohennm | 3:f68e9cdfaf2d | 25 | //#define PRINTDEBUG |
scohennm | 2:7f347d6a6422 | 26 | |
scohennm | 3:f68e9cdfaf2d | 27 | Serial pc(USBTX, USBRX); |
scohennm | 3:f68e9cdfaf2d | 28 | |
vbharam | 5:c047f46f8726 | 29 | // Array of array |
vbharam | 5:c047f46f8726 | 30 | float diatonicScale[NUMOCTS][NUMTONES] = {{246.94, 261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25,587.33}, {659.25, 698.46, 783.99, 880.00, 987.77, 1046.50, 1174.66, 1318.51, 1396.91, 1567.98 }}; |
scohennm | 2:7f347d6a6422 | 31 | SLCD slcd; //define LCD display |
scohennm | 2:7f347d6a6422 | 32 | |
scohennm | 2:7f347d6a6422 | 33 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 2:7f347d6a6422 | 34 | |
vbharam | 5:c047f46f8726 | 35 | //InterruptIn mybutton(PTC3); //push botton with internal pullup |
scohennm | 2:7f347d6a6422 | 36 | PwmOut led(LED_RED); |
scohennm | 2:7f347d6a6422 | 37 | DigitalOut outPin(PTC9); //J1-16 |
vbharam | 5:c047f46f8726 | 38 | // Right Switch set as a Digital Input |
vbharam | 5:c047f46f8726 | 39 | DigitalIn buttonState(PTC3); |
scohennm | 2:7f347d6a6422 | 40 | PwmOut soundOut(PTA13); |
vbharam | 5:c047f46f8726 | 41 | |
vbharam | 5:c047f46f8726 | 42 | // Two timer Interrupts |
scohennm | 3:f68e9cdfaf2d | 43 | Timer dataTimer; |
vbharam | 5:c047f46f8726 | 44 | Timer buttonTimer; |
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 | |
vbharam | 4:d59d946649b8 | 51 | // Int used to switch between different octave |
vbharam | 5:c047f46f8726 | 52 | int octaveState = 0; |
vbharam | 4:d59d946649b8 | 53 | |
scohennm | 2:7f347d6a6422 | 54 | void LCDMessNoDwell(char *lMess){ |
scohennm | 2:7f347d6a6422 | 55 | slcd.Home(); |
scohennm | 2:7f347d6a6422 | 56 | slcd.clear(); |
scohennm | 2:7f347d6a6422 | 57 | slcd.printf(lMess); |
scohennm | 2:7f347d6a6422 | 58 | } |
scohennm | 2:7f347d6a6422 | 59 | |
vbharam | 5:c047f46f8726 | 60 | void diatonicAdjust( float scaling, int octaveState) { |
vbharam | 5:c047f46f8726 | 61 | // octaveState here works as the index of array diatonicScale |
vbharam | 5:c047f46f8726 | 62 | |
scohennm | 2:7f347d6a6422 | 63 | int tempInt; |
scohennm | 3:f68e9cdfaf2d | 64 | int scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 65 | static int oldScaleIndex = 0; |
scohennm | 3:f68e9cdfaf2d | 66 | /* There appears to be a set up time for setting the PWM time period |
scohennm | 3:f68e9cdfaf2d | 67 | only do a nes set up if the indext changes. |
scohennm | 3:f68e9cdfaf2d | 68 | */ |
scohennm | 3:f68e9cdfaf2d | 69 | scaleIndex = (int)(NUMTONES * scaling); |
scohennm | 3:f68e9cdfaf2d | 70 | if (scaleIndex != oldScaleIndex) { |
vbharam | 5:c047f46f8726 | 71 | toneFreq = diatonicScale[octaveState][scaleIndex]; |
scohennm | 3:f68e9cdfaf2d | 72 | tonePeriod = 1.0/toneFreq; |
scohennm | 3:f68e9cdfaf2d | 73 | soundOut.period(tonePeriod); // adjusting period |
scohennm | 3:f68e9cdfaf2d | 74 | soundOut.write(TONEON); // there is a setup time for both period and DF |
scohennm | 3:f68e9cdfaf2d | 75 | oldScaleIndex = scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 76 | } else { |
scohennm | 3:f68e9cdfaf2d | 77 | return; |
scohennm | 3:f68e9cdfaf2d | 78 | } |
scohennm | 3:f68e9cdfaf2d | 79 | #ifdef PRINTDEBUG |
scohennm | 3:f68e9cdfaf2d | 80 | pc.printf(" %f,%d\n\r",scaling, scaleIndex ); |
scohennm | 3:f68e9cdfaf2d | 81 | #endif |
scohennm | 2:7f347d6a6422 | 82 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 83 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 84 | LCDMessNoDwell(lcdData); |
scohennm | 2:7f347d6a6422 | 85 | return; |
scohennm | 2:7f347d6a6422 | 86 | } |
vbharam | 4:d59d946649b8 | 87 | |
vbharam | 4:d59d946649b8 | 88 | |
vbharam | 4:d59d946649b8 | 89 | |
scohennm | 2:7f347d6a6422 | 90 | void lightAdjust( float scaling) { // Control brightness of LED |
scohennm | 2:7f347d6a6422 | 91 | float tempDutyFactor; |
scohennm | 2:7f347d6a6422 | 92 | |
scohennm | 3:f68e9cdfaf2d | 93 | tempDutyFactor = 1.0 - scaling; //LED is a sinking connection // anode is held at 5V |
scohennm | 3:f68e9cdfaf2d | 94 | led.write(tempDutyFactor); //adjusting duty factor |
scohennm | 2:7f347d6a6422 | 95 | return; |
scohennm | 2:7f347d6a6422 | 96 | } |
scohennm | 3:f68e9cdfaf2d | 97 | |
scohennm | 2:7f347d6a6422 | 98 | int main(){ |
scohennm | 2:7f347d6a6422 | 99 | int tempInt; |
vbharam | 4:d59d946649b8 | 100 | float tempValue; |
scohennm | 2:7f347d6a6422 | 101 | tonePeriod = 1.0/toneFreq; |
scohennm | 2:7f347d6a6422 | 102 | soundOut.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 103 | led.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 104 | led.write(CHANNELOFF); |
scohennm | 3:f68e9cdfaf2d | 105 | outPin.write(CHANNELOFF); |
vbharam | 4:d59d946649b8 | 106 | // set up interrrupts to be used later for taps |
vbharam | 5:c047f46f8726 | 107 | // mybutton.fall(&pressed); |
scohennm | 2:7f347d6a6422 | 108 | |
scohennm | 2:7f347d6a6422 | 109 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 110 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 111 | LCDMessNoDwell(lcdData); |
scohennm | 3:f68e9cdfaf2d | 112 | // Start data timer |
scohennm | 3:f68e9cdfaf2d | 113 | dataTimer.start(); |
scohennm | 3:f68e9cdfaf2d | 114 | dataTimer.reset(); |
vbharam | 5:c047f46f8726 | 115 | |
vbharam | 5:c047f46f8726 | 116 | // Start button timer |
vbharam | 5:c047f46f8726 | 117 | buttonTimer.start(); |
vbharam | 5:c047f46f8726 | 118 | buttonTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 119 | |
scohennm | 2:7f347d6a6422 | 120 | while (true) { |
vbharam | 5:c047f46f8726 | 121 | //Button and data Timers are checked at different times |
vbharam | 5:c047f46f8726 | 122 | if (buttonTimer.read_ms() > PUSHTIME){ // check to see if enough time has passed |
vbharam | 5:c047f46f8726 | 123 | // to button interrupt |
vbharam | 5:c047f46f8726 | 124 | // If the pushbutton is pressed: |
vbharam | 5:c047f46f8726 | 125 | if (buttonState == CHANNELON){ |
vbharam | 5:c047f46f8726 | 126 | octaveState++; |
vbharam | 5:c047f46f8726 | 127 | octaveState = octaveState % NUMOCTS; |
vbharam | 5:c047f46f8726 | 128 | sprintf (lcdData,"%4d",octaveState); |
vbharam | 5:c047f46f8726 | 129 | LCDMessNoDwell(lcdData); |
vbharam | 5:c047f46f8726 | 130 | } |
vbharam | 5:c047f46f8726 | 131 | buttonTimer.reset(); |
vbharam | 5:c047f46f8726 | 132 | } |
vbharam | 5:c047f46f8726 | 133 | |
scohennm | 3:f68e9cdfaf2d | 134 | if (dataTimer.read_ms() > DATATIME){ // check to see if enough time has passed |
scohennm | 3:f68e9cdfaf2d | 135 | // to read the touch pad |
scohennm | 3:f68e9cdfaf2d | 136 | tempValue = tsiScaling.readPercentage(); |
scohennm | 3:f68e9cdfaf2d | 137 | if(tempValue > 0) { |
vbharam | 5:c047f46f8726 | 138 | soundOut.write(TONEON); // set duty factor to 50% |
vbharam | 5:c047f46f8726 | 139 | diatonicAdjust(tempValue, octaveState); |
scohennm | 3:f68e9cdfaf2d | 140 | lightAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 141 | } else { |
scohennm | 3:f68e9cdfaf2d | 142 | soundOut.write(TONEOFF); // set dutyfactor to 0% |
scohennm | 3:f68e9cdfaf2d | 143 | LCDMessNoDwell("SOFF"); |
scohennm | 3:f68e9cdfaf2d | 144 | } |
scohennm | 3:f68e9cdfaf2d | 145 | dataTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 146 | } |
scohennm | 2:7f347d6a6422 | 147 | } // while forever |
scohennm | 2:7f347d6a6422 | 148 | }// end main |