Diatonic scale - slider base program plays one octave and a two notes, having 440A (A4) in the center.
Fork of slider_tone_v1 by
Revision 3:f68e9cdfaf2d, committed 2015-02-18
- Comitter:
- scohennm
- Date:
- Wed Feb 18 15:27:17 2015 +0000
- Parent:
- 2:7f347d6a6422
- Commit message:
- Diatonic scale - slider base program plays one octave and a two notes, having 440A (A4) in the center.
Changed in this revision
slider_diatonic_v1.cpp | Show annotated file Show diff for this revision Revisions of this file |
slider_tone_v1.cpp | Show diff for this revision Revisions of this file |
diff -r 7f347d6a6422 -r f68e9cdfaf2d slider_diatonic_v1.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/slider_diatonic_v1.cpp Wed Feb 18 15:27:17 2015 +0000 @@ -0,0 +1,117 @@ +#include "mbed.h" +#include "SLCD.h" +#include "TSISensor.h" + + +#define CHANNELON 0 +#define CHANNELOFF 1 +#define LCDLEN 10 +#define DATATIME 100 //milli seccnds +//LCD messages + + + +// Operating parameters +#define SIDETONE 700.0 +#define TONEMIN 200.0 +#define TONEINT 800.00 // So tone max is 1000 +#define TONEON 0.50 +#define TONEOFF 0.0 +#define SPEEDAST 0 +#define TONEAST 1 +#define NUMTONES 10 +#define LEDPERIOD 0.001 +//#define PRINTDEBUG + +Serial pc(USBTX, USBRX); + +float diatonicScale[NUMTONES] = {246.94, 261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25,587.33}; +SLCD slcd; //define LCD display + +TSISensor tsiScaling; // Capacitive sensor/slider + + +PwmOut led(LED_RED); +DigitalOut outPin(PTC9); //J1-16 +PwmOut soundOut(PTA13); +Timer dataTimer; +// Global scalars +char lcdData[LCDLEN]; + +float tonePeriod; +float toneFreq = SIDETONE; + + + +void LCDMessNoDwell(char *lMess){ + slcd.Home(); + slcd.clear(); + slcd.printf(lMess); +} + +void diatonicAdjust( float scaling) { + int tempInt; + int scaleIndex; + static int oldScaleIndex = 0; +/* There appears to be a set up time for setting the PWM time period +only do a nes set up if the indext changes. +*/ + scaleIndex = (int)(NUMTONES * scaling); + if (scaleIndex != oldScaleIndex) { + toneFreq = diatonicScale[scaleIndex]; + tonePeriod = 1.0/toneFreq; + soundOut.period(tonePeriod); // adjusting period + soundOut.write(TONEON); // there is a setup time for both period and DF + oldScaleIndex = scaleIndex; + } else { + return; + } +#ifdef PRINTDEBUG + pc.printf(" %f,%d\n\r",scaling, scaleIndex ); +#endif + tempInt = (int)toneFreq; + sprintf (lcdData,"%4d",tempInt); + LCDMessNoDwell(lcdData); + return; +} +void lightAdjust( float scaling) { // Control brightness of LED + float tempDutyFactor; + + tempDutyFactor = 1.0 - scaling; //LED is a sinking connection // anode is held at 5V + led.write(tempDutyFactor); //adjusting duty factor + return; +} + +int main(){ + int tempInt; + float tempValue; + + tonePeriod = 1.0/toneFreq; + soundOut.period(tonePeriod); + led.period(tonePeriod); + led.write(CHANNELOFF); + outPin.write(CHANNELOFF); + + tempInt = (int)toneFreq; + sprintf (lcdData,"%4d",tempInt); + LCDMessNoDwell(lcdData); + // Start data timer + dataTimer.start(); + dataTimer.reset(); + + while (true) { + if (dataTimer.read_ms() > DATATIME){ // check to see if enough time has passed + // to read the touch pad + tempValue = tsiScaling.readPercentage(); + if(tempValue > 0) { + // soundOut.write(TONEON); // set duty factor to 50% + diatonicAdjust(tempValue); + lightAdjust(tempValue); + } else { + soundOut.write(TONEOFF); // set dutyfactor to 0% + LCDMessNoDwell("SOFF"); + } + dataTimer.reset(); + } + } // while forever +}// end main \ No newline at end of file
diff -r 7f347d6a6422 -r f68e9cdfaf2d slider_tone_v1.cpp --- a/slider_tone_v1.cpp Sat Feb 14 23:03:06 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -#include "mbed.h" -#include "SLCD.h" -#include "TSISensor.h" - - -#define CHANNELON 0 -#define CHANNELOFF 1 -#define LCDLEN 10 -#define DATATIME 0.1 -//LCD messages - - - -// Operating parameters -#define SIDETONE 700.0 -#define TONEMIN 200.0 -#define TONEINT 800.00 // So tone max is 1000 -#define TONEON 0.50 -#define TONEOFF 0.0 -#define SPEEDAST 0 -#define TONEAST 1 - -SLCD slcd; //define LCD display - -TSISensor tsiScaling; // Capacitive sensor/slider - -PwmOut led(LED_RED); -DigitalOut outPin(PTC9); //J1-16 -PwmOut soundOut(PTA13); -// Global scalars -char lcdData[LCDLEN]; - -float tonePeriod; -float toneFreq = SIDETONE; - - - -void LCDMessNoDwell(char *lMess){ - slcd.Home(); - slcd.clear(); - slcd.printf(lMess); -} - -void toneAdjust( float scaling) { - int tempInt; - - toneFreq = TONEMIN + scaling * TONEINT; - tonePeriod = 1.0/toneFreq; - soundOut.period(tonePeriod); // adusting period - tempInt = (int)toneFreq; - sprintf (lcdData,"%4d",tempInt); - LCDMessNoDwell(lcdData); - return; -} -void lightAdjust( float scaling) { // Control brightness of LED - float tempDutyFactor; - - tempDutyFactor = 1.0 - scaling; //LED is a sinking connection - // anode is held at 5V - led.write(tempDutyFactor); //sdjusting duty factor - return; -} -int main(){ - int tempInt; - float tempValue; - tonePeriod = 1.0/toneFreq; - soundOut.period(tonePeriod); - - - - led.write(CHANNELON); - outPin.write(CHANNELOFF); - tempInt = (int)toneFreq; - sprintf (lcdData,"%4d",tempInt); - LCDMessNoDwell(lcdData); - wait(DATATIME); - while (true) { - - tempValue = tsiScaling.readPercentage(); - if(tempValue > 0) { - soundOut.write(TONEON); // set duty factor to 505 - toneAdjust( tempValue); - lightAdjust(tempValue); - } else { - soundOut.write(TONEOFF); // set dutyfactor to 0% - LCDMessNoDwell("SOFF"); - } - wait(DATATIME); - } // while forever -}// end main \ No newline at end of file