![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Works to change an octave higher ONLY when you hold down the button_need to figure out how to CHANGE with toggling with button
Fork of slider_diatonic_v1 by
slider_diatonic_v1.cpp@4:de991c90af6a, 2015-02-23 (annotated)
- Committer:
- destradafilm
- Date:
- Mon Feb 23 04:49:39 2015 +0000
- Revision:
- 4:de991c90af6a
- Parent:
- 3:f68e9cdfaf2d
slider_diatonic_changing Octaves
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 | |
scohennm | 2:7f347d6a6422 | 12 | |
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 |
scohennm | 3:f68e9cdfaf2d | 23 | #define LEDPERIOD 0.001 |
scohennm | 3:f68e9cdfaf2d | 24 | //#define PRINTDEBUG |
scohennm | 2:7f347d6a6422 | 25 | |
scohennm | 3:f68e9cdfaf2d | 26 | Serial pc(USBTX, USBRX); |
scohennm | 3:f68e9cdfaf2d | 27 | |
scohennm | 3:f68e9cdfaf2d | 28 | float diatonicScale[NUMTONES] = {246.94, 261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25,587.33}; |
destradafilm | 4:de991c90af6a | 29 | float diatonicScale2[NUMTONES] ={493.88, 523.25,587.33,659.25,698.46,783.99,880.00,987.77,1046.50,1174.66}; |
scohennm | 2:7f347d6a6422 | 30 | SLCD slcd; //define LCD display |
scohennm | 2:7f347d6a6422 | 31 | |
scohennm | 2:7f347d6a6422 | 32 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 2:7f347d6a6422 | 33 | |
destradafilm | 4:de991c90af6a | 34 | // changes |
destradafilm | 4:de991c90af6a | 35 | DigitalIn RtButton(PTC3); // make RIGHT button interrrupt |
destradafilm | 4:de991c90af6a | 36 | int RtbuttonState; // switch changes when RtButton is pressed |
scohennm | 3:f68e9cdfaf2d | 37 | |
scohennm | 2:7f347d6a6422 | 38 | PwmOut led(LED_RED); |
scohennm | 2:7f347d6a6422 | 39 | DigitalOut outPin(PTC9); //J1-16 |
scohennm | 2:7f347d6a6422 | 40 | PwmOut soundOut(PTA13); |
scohennm | 3:f68e9cdfaf2d | 41 | Timer dataTimer; |
scohennm | 2:7f347d6a6422 | 42 | // Global scalars |
scohennm | 2:7f347d6a6422 | 43 | char lcdData[LCDLEN]; |
scohennm | 2:7f347d6a6422 | 44 | |
scohennm | 2:7f347d6a6422 | 45 | float tonePeriod; |
scohennm | 2:7f347d6a6422 | 46 | float toneFreq = SIDETONE; |
scohennm | 2:7f347d6a6422 | 47 | |
scohennm | 2:7f347d6a6422 | 48 | |
scohennm | 2:7f347d6a6422 | 49 | |
scohennm | 2:7f347d6a6422 | 50 | void LCDMessNoDwell(char *lMess){ |
scohennm | 2:7f347d6a6422 | 51 | slcd.Home(); |
scohennm | 2:7f347d6a6422 | 52 | slcd.clear(); |
scohennm | 2:7f347d6a6422 | 53 | slcd.printf(lMess); |
scohennm | 2:7f347d6a6422 | 54 | } |
scohennm | 2:7f347d6a6422 | 55 | |
scohennm | 3:f68e9cdfaf2d | 56 | void diatonicAdjust( float scaling) { |
scohennm | 2:7f347d6a6422 | 57 | int tempInt; |
scohennm | 3:f68e9cdfaf2d | 58 | int scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 59 | static int oldScaleIndex = 0; |
scohennm | 3:f68e9cdfaf2d | 60 | /* There appears to be a set up time for setting the PWM time period |
scohennm | 3:f68e9cdfaf2d | 61 | only do a nes set up if the indext changes. |
scohennm | 3:f68e9cdfaf2d | 62 | */ |
scohennm | 3:f68e9cdfaf2d | 63 | scaleIndex = (int)(NUMTONES * scaling); |
scohennm | 3:f68e9cdfaf2d | 64 | if (scaleIndex != oldScaleIndex) { |
destradafilm | 4:de991c90af6a | 65 | if(RtbuttonState == false){ |
scohennm | 3:f68e9cdfaf2d | 66 | toneFreq = diatonicScale[scaleIndex]; |
destradafilm | 4:de991c90af6a | 67 | } else { |
destradafilm | 4:de991c90af6a | 68 | toneFreq = diatonicScale2[scaleIndex]; |
destradafilm | 4:de991c90af6a | 69 | } |
scohennm | 3:f68e9cdfaf2d | 70 | tonePeriod = 1.0/toneFreq; |
scohennm | 3:f68e9cdfaf2d | 71 | soundOut.period(tonePeriod); // adjusting period |
scohennm | 3:f68e9cdfaf2d | 72 | soundOut.write(TONEON); // there is a setup time for both period and DF |
scohennm | 3:f68e9cdfaf2d | 73 | oldScaleIndex = scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 74 | } else { |
scohennm | 3:f68e9cdfaf2d | 75 | return; |
scohennm | 3:f68e9cdfaf2d | 76 | } |
scohennm | 3:f68e9cdfaf2d | 77 | #ifdef PRINTDEBUG |
scohennm | 3:f68e9cdfaf2d | 78 | pc.printf(" %f,%d\n\r",scaling, scaleIndex ); |
scohennm | 3:f68e9cdfaf2d | 79 | #endif |
scohennm | 2:7f347d6a6422 | 80 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 81 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 82 | LCDMessNoDwell(lcdData); |
scohennm | 2:7f347d6a6422 | 83 | return; |
scohennm | 2:7f347d6a6422 | 84 | } |
scohennm | 2:7f347d6a6422 | 85 | void lightAdjust( float scaling) { // Control brightness of LED |
scohennm | 2:7f347d6a6422 | 86 | float tempDutyFactor; |
scohennm | 2:7f347d6a6422 | 87 | |
scohennm | 3:f68e9cdfaf2d | 88 | tempDutyFactor = 1.0 - scaling; //LED is a sinking connection // anode is held at 5V |
scohennm | 3:f68e9cdfaf2d | 89 | led.write(tempDutyFactor); //adjusting duty factor |
scohennm | 2:7f347d6a6422 | 90 | return; |
scohennm | 2:7f347d6a6422 | 91 | } |
scohennm | 3:f68e9cdfaf2d | 92 | |
scohennm | 2:7f347d6a6422 | 93 | int main(){ |
scohennm | 2:7f347d6a6422 | 94 | int tempInt; |
scohennm | 2:7f347d6a6422 | 95 | float tempValue; |
scohennm | 3:f68e9cdfaf2d | 96 | |
scohennm | 2:7f347d6a6422 | 97 | tonePeriod = 1.0/toneFreq; |
scohennm | 2:7f347d6a6422 | 98 | soundOut.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 99 | led.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 100 | led.write(CHANNELOFF); |
scohennm | 3:f68e9cdfaf2d | 101 | outPin.write(CHANNELOFF); |
scohennm | 2:7f347d6a6422 | 102 | |
scohennm | 2:7f347d6a6422 | 103 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 104 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 105 | LCDMessNoDwell(lcdData); |
scohennm | 3:f68e9cdfaf2d | 106 | // Start data timer |
scohennm | 3:f68e9cdfaf2d | 107 | dataTimer.start(); |
scohennm | 3:f68e9cdfaf2d | 108 | dataTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 109 | |
scohennm | 2:7f347d6a6422 | 110 | while (true) { |
destradafilm | 4:de991c90af6a | 111 | |
destradafilm | 4:de991c90af6a | 112 | RtbuttonState = RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream |
destradafilm | 4:de991c90af6a | 113 | |
scohennm | 3:f68e9cdfaf2d | 114 | if (dataTimer.read_ms() > DATATIME){ // check to see if enough time has passed |
scohennm | 3:f68e9cdfaf2d | 115 | // to read the touch pad |
scohennm | 3:f68e9cdfaf2d | 116 | tempValue = tsiScaling.readPercentage(); |
scohennm | 3:f68e9cdfaf2d | 117 | if(tempValue > 0) { |
scohennm | 3:f68e9cdfaf2d | 118 | // soundOut.write(TONEON); // set duty factor to 50% |
scohennm | 3:f68e9cdfaf2d | 119 | diatonicAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 120 | lightAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 121 | } else { |
scohennm | 3:f68e9cdfaf2d | 122 | soundOut.write(TONEOFF); // set dutyfactor to 0% |
scohennm | 3:f68e9cdfaf2d | 123 | LCDMessNoDwell("SOFF"); |
scohennm | 3:f68e9cdfaf2d | 124 | } |
scohennm | 3:f68e9cdfaf2d | 125 | dataTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 126 | } |
scohennm | 2:7f347d6a6422 | 127 | } // while forever |
scohennm | 2:7f347d6a6422 | 128 | }// end main |