Can't figure out this one, my Multi-dimensional Array doesn't work.
Fork of slider_diatonic_v1_HW6_2-ChangingOctaves by
slider_diatonic_v1.cpp@5:d7747f156e9a, 2015-02-25 (annotated)
- Committer:
- destradafilm
- Date:
- Wed Feb 25 17:04:35 2015 +0000
- Revision:
- 5:d7747f156e9a
- Parent:
- 4:de991c90af6a
HW_7_1 - Using two timers
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 |
destradafilm | 5:d7747f156e9a | 9 | #define DATATIME 50 //milli seccnds |
destradafilm | 5:d7747f156e9a | 10 | #define PUSHTIME 100 // MS |
scohennm | 2:7f347d6a6422 | 11 | //LCD messages |
scohennm | 2:7f347d6a6422 | 12 | |
scohennm | 2:7f347d6a6422 | 13 | |
scohennm | 2:7f347d6a6422 | 14 | |
scohennm | 2:7f347d6a6422 | 15 | // Operating parameters |
scohennm | 2:7f347d6a6422 | 16 | #define SIDETONE 700.0 |
scohennm | 2:7f347d6a6422 | 17 | #define TONEMIN 200.0 |
scohennm | 2:7f347d6a6422 | 18 | #define TONEINT 800.00 // So tone max is 1000 |
scohennm | 2:7f347d6a6422 | 19 | #define TONEON 0.50 |
scohennm | 2:7f347d6a6422 | 20 | #define TONEOFF 0.0 |
scohennm | 2:7f347d6a6422 | 21 | #define SPEEDAST 0 |
scohennm | 2:7f347d6a6422 | 22 | #define TONEAST 1 |
scohennm | 3:f68e9cdfaf2d | 23 | #define NUMTONES 10 |
scohennm | 3:f68e9cdfaf2d | 24 | #define LEDPERIOD 0.001 |
destradafilm | 5:d7747f156e9a | 25 | #define OCTAVESTATE 1 |
destradafilm | 5:d7747f156e9a | 26 | #define NUMOCTS 2 |
scohennm | 3:f68e9cdfaf2d | 27 | //#define PRINTDEBUG |
scohennm | 2:7f347d6a6422 | 28 | |
scohennm | 3:f68e9cdfaf2d | 29 | Serial pc(USBTX, USBRX); |
scohennm | 3:f68e9cdfaf2d | 30 | |
destradafilm | 5:d7747f156e9a | 31 | int octaveState = 0; |
destradafilm | 5:d7747f156e9a | 32 | float diatonicScale[OCTAVESTATE][NUMTONES] = {{246.94, 261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25,587.33}, |
destradafilm | 5:d7747f156e9a | 33 | {493.88, 523.25,587.33,659.25,698.46,783.99,880.00,987.77,1046.50,1174.66}}; |
destradafilm | 5:d7747f156e9a | 34 | //float diatonicScale2[NUMTONES] =; |
scohennm | 2:7f347d6a6422 | 35 | SLCD slcd; //define LCD display |
scohennm | 2:7f347d6a6422 | 36 | |
scohennm | 2:7f347d6a6422 | 37 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 2:7f347d6a6422 | 38 | |
destradafilm | 4:de991c90af6a | 39 | // changes |
destradafilm | 5:d7747f156e9a | 40 | DigitalOut RtButton(PTC3); // make RIGHT button interrrupt |
destradafilm | 4:de991c90af6a | 41 | int RtbuttonState; // switch changes when RtButton is pressed |
scohennm | 3:f68e9cdfaf2d | 42 | |
scohennm | 2:7f347d6a6422 | 43 | PwmOut led(LED_RED); |
scohennm | 2:7f347d6a6422 | 44 | DigitalOut outPin(PTC9); //J1-16 |
scohennm | 2:7f347d6a6422 | 45 | PwmOut soundOut(PTA13); |
destradafilm | 5:d7747f156e9a | 46 | |
destradafilm | 5:d7747f156e9a | 47 | |
destradafilm | 5:d7747f156e9a | 48 | Timer dataTimer; // timer - check if slider has been changed every DATATIME (50ms) |
destradafilm | 5:d7747f156e9a | 49 | Timer buttonTimer; // check if button has been pressed every PUSHTIME (100ms) |
scohennm | 2:7f347d6a6422 | 50 | // Global scalars |
scohennm | 2:7f347d6a6422 | 51 | char lcdData[LCDLEN]; |
scohennm | 2:7f347d6a6422 | 52 | |
scohennm | 2:7f347d6a6422 | 53 | float tonePeriod; |
scohennm | 2:7f347d6a6422 | 54 | float toneFreq = SIDETONE; |
scohennm | 2:7f347d6a6422 | 55 | |
scohennm | 2:7f347d6a6422 | 56 | |
scohennm | 2:7f347d6a6422 | 57 | |
scohennm | 2:7f347d6a6422 | 58 | void LCDMessNoDwell(char *lMess){ |
scohennm | 2:7f347d6a6422 | 59 | slcd.Home(); |
scohennm | 2:7f347d6a6422 | 60 | slcd.clear(); |
scohennm | 2:7f347d6a6422 | 61 | slcd.printf(lMess); |
scohennm | 2:7f347d6a6422 | 62 | } |
scohennm | 2:7f347d6a6422 | 63 | |
destradafilm | 5:d7747f156e9a | 64 | void diatonicAdjust( float scaling, float octfactors[octaveState]) { |
scohennm | 2:7f347d6a6422 | 65 | int tempInt; |
scohennm | 3:f68e9cdfaf2d | 66 | int scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 67 | static int oldScaleIndex = 0; |
destradafilm | 5:d7747f156e9a | 68 | |
scohennm | 3:f68e9cdfaf2d | 69 | /* There appears to be a set up time for setting the PWM time period |
scohennm | 3:f68e9cdfaf2d | 70 | only do a nes set up if the indext changes. |
scohennm | 3:f68e9cdfaf2d | 71 | */ |
scohennm | 3:f68e9cdfaf2d | 72 | scaleIndex = (int)(NUMTONES * scaling); |
scohennm | 3:f68e9cdfaf2d | 73 | if (scaleIndex != oldScaleIndex) { |
destradafilm | 4:de991c90af6a | 74 | if(RtbuttonState == false){ |
destradafilm | 5:d7747f156e9a | 75 | toneFreq = diatonicScale[0][scaleIndex]; |
destradafilm | 4:de991c90af6a | 76 | } else { |
destradafilm | 5:d7747f156e9a | 77 | toneFreq = diatonicScale[1][scaleIndex]; |
destradafilm | 4:de991c90af6a | 78 | } |
scohennm | 3:f68e9cdfaf2d | 79 | tonePeriod = 1.0/toneFreq; |
scohennm | 3:f68e9cdfaf2d | 80 | soundOut.period(tonePeriod); // adjusting period |
scohennm | 3:f68e9cdfaf2d | 81 | soundOut.write(TONEON); // there is a setup time for both period and DF |
scohennm | 3:f68e9cdfaf2d | 82 | oldScaleIndex = scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 83 | } else { |
scohennm | 3:f68e9cdfaf2d | 84 | return; |
scohennm | 3:f68e9cdfaf2d | 85 | } |
scohennm | 3:f68e9cdfaf2d | 86 | #ifdef PRINTDEBUG |
scohennm | 3:f68e9cdfaf2d | 87 | pc.printf(" %f,%d\n\r",scaling, scaleIndex ); |
scohennm | 3:f68e9cdfaf2d | 88 | #endif |
scohennm | 2:7f347d6a6422 | 89 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 90 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 91 | LCDMessNoDwell(lcdData); |
scohennm | 2:7f347d6a6422 | 92 | return; |
scohennm | 2:7f347d6a6422 | 93 | } |
scohennm | 2:7f347d6a6422 | 94 | void lightAdjust( float scaling) { // Control brightness of LED |
scohennm | 2:7f347d6a6422 | 95 | float tempDutyFactor; |
scohennm | 2:7f347d6a6422 | 96 | |
scohennm | 3:f68e9cdfaf2d | 97 | tempDutyFactor = 1.0 - scaling; //LED is a sinking connection // anode is held at 5V |
scohennm | 3:f68e9cdfaf2d | 98 | led.write(tempDutyFactor); //adjusting duty factor |
scohennm | 2:7f347d6a6422 | 99 | return; |
scohennm | 2:7f347d6a6422 | 100 | } |
scohennm | 3:f68e9cdfaf2d | 101 | |
scohennm | 2:7f347d6a6422 | 102 | int main(){ |
scohennm | 2:7f347d6a6422 | 103 | int tempInt; |
scohennm | 2:7f347d6a6422 | 104 | float tempValue; |
scohennm | 3:f68e9cdfaf2d | 105 | |
scohennm | 2:7f347d6a6422 | 106 | tonePeriod = 1.0/toneFreq; |
scohennm | 2:7f347d6a6422 | 107 | soundOut.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 108 | led.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 109 | led.write(CHANNELOFF); |
scohennm | 3:f68e9cdfaf2d | 110 | outPin.write(CHANNELOFF); |
scohennm | 2:7f347d6a6422 | 111 | |
scohennm | 2:7f347d6a6422 | 112 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 113 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 114 | LCDMessNoDwell(lcdData); |
destradafilm | 5:d7747f156e9a | 115 | |
scohennm | 3:f68e9cdfaf2d | 116 | // Start data timer |
scohennm | 3:f68e9cdfaf2d | 117 | dataTimer.start(); |
scohennm | 3:f68e9cdfaf2d | 118 | dataTimer.reset(); |
destradafilm | 5:d7747f156e9a | 119 | |
destradafilm | 5:d7747f156e9a | 120 | buttonTimer.start(); |
destradafilm | 5:d7747f156e9a | 121 | buttonTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 122 | |
scohennm | 2:7f347d6a6422 | 123 | while (true) { |
destradafilm | 5:d7747f156e9a | 124 | |
destradafilm | 5:d7747f156e9a | 125 | if(buttonTimer.read_ms() > PUSHTIME){ |
destradafilm | 5:d7747f156e9a | 126 | //HANDLE USER INPUT FOR DISPLAY SECTIONS |
destradafilm | 5:d7747f156e9a | 127 | // |
destradafilm | 5:d7747f156e9a | 128 | RtbuttonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream |
destradafilm | 5:d7747f156e9a | 129 | if(RtbuttonState) { |
destradafilm | 5:d7747f156e9a | 130 | octaveState++; |
destradafilm | 5:d7747f156e9a | 131 | octaveState = octaveState % NUMOCTS; |
destradafilm | 5:d7747f156e9a | 132 | } |
destradafilm | 5:d7747f156e9a | 133 | buttonTimer.reset(); |
destradafilm | 4:de991c90af6a | 134 | |
scohennm | 3:f68e9cdfaf2d | 135 | if (dataTimer.read_ms() > DATATIME){ // check to see if enough time has passed |
scohennm | 3:f68e9cdfaf2d | 136 | // to read the touch pad |
scohennm | 3:f68e9cdfaf2d | 137 | tempValue = tsiScaling.readPercentage(); |
scohennm | 3:f68e9cdfaf2d | 138 | if(tempValue > 0) { |
scohennm | 3:f68e9cdfaf2d | 139 | // soundOut.write(TONEON); // set duty factor to 50% |
destradafilm | 5:d7747f156e9a | 140 | diatonicAdjust(tempValue, octfactors[octaveState]); |
scohennm | 3:f68e9cdfaf2d | 141 | lightAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 142 | } else { |
scohennm | 3:f68e9cdfaf2d | 143 | soundOut.write(TONEOFF); // set dutyfactor to 0% |
destradafilm | 5:d7747f156e9a | 144 | sprintf (lcdData, "%4d", octaveState + 1);// just to make thigns user readable |
scohennm | 3:f68e9cdfaf2d | 145 | LCDMessNoDwell("SOFF"); |
scohennm | 3:f68e9cdfaf2d | 146 | } |
scohennm | 3:f68e9cdfaf2d | 147 | dataTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 148 | } |
scohennm | 2:7f347d6a6422 | 149 | } // while forever |
scohennm | 2:7f347d6a6422 | 150 | }// end main |