Diatonic scale - slider base program plays one octave and a two notes, having 440A (A4) in the center.
Fork of slider_tone_v1 by
slider_diatonic_v1.cpp@3:f68e9cdfaf2d, 2015-02-18 (annotated)
- Committer:
- scohennm
- Date:
- Wed Feb 18 15:27:17 2015 +0000
- Revision:
- 3:f68e9cdfaf2d
- Parent:
- slider_tone_v1.cpp@2:7f347d6a6422
Diatonic scale - slider base program plays one octave and a two notes, having 440A (A4) in the center.
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}; |
scohennm | 2:7f347d6a6422 | 29 | SLCD slcd; //define LCD display |
scohennm | 2:7f347d6a6422 | 30 | |
scohennm | 2:7f347d6a6422 | 31 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 2:7f347d6a6422 | 32 | |
scohennm | 3:f68e9cdfaf2d | 33 | |
scohennm | 2:7f347d6a6422 | 34 | PwmOut led(LED_RED); |
scohennm | 2:7f347d6a6422 | 35 | DigitalOut outPin(PTC9); //J1-16 |
scohennm | 2:7f347d6a6422 | 36 | PwmOut soundOut(PTA13); |
scohennm | 3:f68e9cdfaf2d | 37 | Timer dataTimer; |
scohennm | 2:7f347d6a6422 | 38 | // Global scalars |
scohennm | 2:7f347d6a6422 | 39 | char lcdData[LCDLEN]; |
scohennm | 2:7f347d6a6422 | 40 | |
scohennm | 2:7f347d6a6422 | 41 | float tonePeriod; |
scohennm | 2:7f347d6a6422 | 42 | float toneFreq = SIDETONE; |
scohennm | 2:7f347d6a6422 | 43 | |
scohennm | 2:7f347d6a6422 | 44 | |
scohennm | 2:7f347d6a6422 | 45 | |
scohennm | 2:7f347d6a6422 | 46 | void LCDMessNoDwell(char *lMess){ |
scohennm | 2:7f347d6a6422 | 47 | slcd.Home(); |
scohennm | 2:7f347d6a6422 | 48 | slcd.clear(); |
scohennm | 2:7f347d6a6422 | 49 | slcd.printf(lMess); |
scohennm | 2:7f347d6a6422 | 50 | } |
scohennm | 2:7f347d6a6422 | 51 | |
scohennm | 3:f68e9cdfaf2d | 52 | void diatonicAdjust( float scaling) { |
scohennm | 2:7f347d6a6422 | 53 | int tempInt; |
scohennm | 3:f68e9cdfaf2d | 54 | int scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 55 | static int oldScaleIndex = 0; |
scohennm | 3:f68e9cdfaf2d | 56 | /* There appears to be a set up time for setting the PWM time period |
scohennm | 3:f68e9cdfaf2d | 57 | only do a nes set up if the indext changes. |
scohennm | 3:f68e9cdfaf2d | 58 | */ |
scohennm | 3:f68e9cdfaf2d | 59 | scaleIndex = (int)(NUMTONES * scaling); |
scohennm | 3:f68e9cdfaf2d | 60 | if (scaleIndex != oldScaleIndex) { |
scohennm | 3:f68e9cdfaf2d | 61 | toneFreq = diatonicScale[scaleIndex]; |
scohennm | 3:f68e9cdfaf2d | 62 | tonePeriod = 1.0/toneFreq; |
scohennm | 3:f68e9cdfaf2d | 63 | soundOut.period(tonePeriod); // adjusting period |
scohennm | 3:f68e9cdfaf2d | 64 | soundOut.write(TONEON); // there is a setup time for both period and DF |
scohennm | 3:f68e9cdfaf2d | 65 | oldScaleIndex = scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 66 | } else { |
scohennm | 3:f68e9cdfaf2d | 67 | return; |
scohennm | 3:f68e9cdfaf2d | 68 | } |
scohennm | 3:f68e9cdfaf2d | 69 | #ifdef PRINTDEBUG |
scohennm | 3:f68e9cdfaf2d | 70 | pc.printf(" %f,%d\n\r",scaling, scaleIndex ); |
scohennm | 3:f68e9cdfaf2d | 71 | #endif |
scohennm | 2:7f347d6a6422 | 72 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 73 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 74 | LCDMessNoDwell(lcdData); |
scohennm | 2:7f347d6a6422 | 75 | return; |
scohennm | 2:7f347d6a6422 | 76 | } |
scohennm | 2:7f347d6a6422 | 77 | void lightAdjust( float scaling) { // Control brightness of LED |
scohennm | 2:7f347d6a6422 | 78 | float tempDutyFactor; |
scohennm | 2:7f347d6a6422 | 79 | |
scohennm | 3:f68e9cdfaf2d | 80 | tempDutyFactor = 1.0 - scaling; //LED is a sinking connection // anode is held at 5V |
scohennm | 3:f68e9cdfaf2d | 81 | led.write(tempDutyFactor); //adjusting duty factor |
scohennm | 2:7f347d6a6422 | 82 | return; |
scohennm | 2:7f347d6a6422 | 83 | } |
scohennm | 3:f68e9cdfaf2d | 84 | |
scohennm | 2:7f347d6a6422 | 85 | int main(){ |
scohennm | 2:7f347d6a6422 | 86 | int tempInt; |
scohennm | 2:7f347d6a6422 | 87 | float tempValue; |
scohennm | 3:f68e9cdfaf2d | 88 | |
scohennm | 2:7f347d6a6422 | 89 | tonePeriod = 1.0/toneFreq; |
scohennm | 2:7f347d6a6422 | 90 | soundOut.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 91 | led.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 92 | led.write(CHANNELOFF); |
scohennm | 3:f68e9cdfaf2d | 93 | outPin.write(CHANNELOFF); |
scohennm | 2:7f347d6a6422 | 94 | |
scohennm | 2:7f347d6a6422 | 95 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 96 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 97 | LCDMessNoDwell(lcdData); |
scohennm | 3:f68e9cdfaf2d | 98 | // Start data timer |
scohennm | 3:f68e9cdfaf2d | 99 | dataTimer.start(); |
scohennm | 3:f68e9cdfaf2d | 100 | dataTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 101 | |
scohennm | 2:7f347d6a6422 | 102 | while (true) { |
scohennm | 3:f68e9cdfaf2d | 103 | if (dataTimer.read_ms() > DATATIME){ // check to see if enough time has passed |
scohennm | 3:f68e9cdfaf2d | 104 | // to read the touch pad |
scohennm | 3:f68e9cdfaf2d | 105 | tempValue = tsiScaling.readPercentage(); |
scohennm | 3:f68e9cdfaf2d | 106 | if(tempValue > 0) { |
scohennm | 3:f68e9cdfaf2d | 107 | // soundOut.write(TONEON); // set duty factor to 50% |
scohennm | 3:f68e9cdfaf2d | 108 | diatonicAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 109 | lightAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 110 | } else { |
scohennm | 3:f68e9cdfaf2d | 111 | soundOut.write(TONEOFF); // set dutyfactor to 0% |
scohennm | 3:f68e9cdfaf2d | 112 | LCDMessNoDwell("SOFF"); |
scohennm | 3:f68e9cdfaf2d | 113 | } |
scohennm | 3:f68e9cdfaf2d | 114 | dataTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 115 | } |
scohennm | 2:7f347d6a6422 | 116 | } // while forever |
scohennm | 2:7f347d6a6422 | 117 | }// end main |