My_Homework

Dependencies:   SLCD TSI mbed

Fork of slider_diatonic_v1 by Vishal Bharam

Committer:
vbharam
Date:
Sun Feb 22 07:57:45 2015 +0000
Revision:
4:d59d946649b8
Parent:
3:f68e9cdfaf2d
Added button interrupt to change from one octave to other

Who changed what in which revision?

UserRevisionLine numberNew 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
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
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};
vbharam 4:d59d946649b8 29 float diatonicScaleOctave[NUMTONES] = {659.25, 698.46, 783.99, 880.00, 987.77, 1046.50, 1174.66, 1318.51, 1396.91, 1567.98 };
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
vbharam 4:d59d946649b8 34 InterruptIn mybutton(PTC3); //push botton with internal pullup
scohennm 2:7f347d6a6422 35 PwmOut led(LED_RED);
scohennm 2:7f347d6a6422 36 DigitalOut outPin(PTC9); //J1-16
scohennm 2:7f347d6a6422 37 PwmOut soundOut(PTA13);
scohennm 3:f68e9cdfaf2d 38 Timer dataTimer;
scohennm 2:7f347d6a6422 39 // Global scalars
scohennm 2:7f347d6a6422 40 char lcdData[LCDLEN];
scohennm 2:7f347d6a6422 41
scohennm 2:7f347d6a6422 42 float tonePeriod;
scohennm 2:7f347d6a6422 43 float toneFreq = SIDETONE;
scohennm 2:7f347d6a6422 44
vbharam 4:d59d946649b8 45 // Int used to switch between different octave
vbharam 4:d59d946649b8 46 int relayState = CHANNELOFF;
scohennm 2:7f347d6a6422 47
scohennm 2:7f347d6a6422 48
vbharam 4:d59d946649b8 49 void pressed() // button intterupt
vbharam 4:d59d946649b8 50 {
vbharam 4:d59d946649b8 51 relayState = !relayState;
vbharam 4:d59d946649b8 52 }
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
scohennm 3:f68e9cdfaf2d 60 void diatonicAdjust( float scaling) {
scohennm 2:7f347d6a6422 61 int tempInt;
scohennm 3:f68e9cdfaf2d 62 int scaleIndex;
scohennm 3:f68e9cdfaf2d 63 static int oldScaleIndex = 0;
scohennm 3:f68e9cdfaf2d 64 /* There appears to be a set up time for setting the PWM time period
scohennm 3:f68e9cdfaf2d 65 only do a nes set up if the indext changes.
scohennm 3:f68e9cdfaf2d 66 */
scohennm 3:f68e9cdfaf2d 67 scaleIndex = (int)(NUMTONES * scaling);
scohennm 3:f68e9cdfaf2d 68 if (scaleIndex != oldScaleIndex) {
vbharam 4:d59d946649b8 69 if (relayState == CHANNELOFF)
vbharam 4:d59d946649b8 70 toneFreq = diatonicScale[scaleIndex];
vbharam 4:d59d946649b8 71 else
vbharam 4:d59d946649b8 72 toneFreq = diatonicScaleOctave[scaleIndex];
scohennm 3:f68e9cdfaf2d 73 tonePeriod = 1.0/toneFreq;
scohennm 3:f68e9cdfaf2d 74 soundOut.period(tonePeriod); // adjusting period
scohennm 3:f68e9cdfaf2d 75 soundOut.write(TONEON); // there is a setup time for both period and DF
scohennm 3:f68e9cdfaf2d 76 oldScaleIndex = scaleIndex;
scohennm 3:f68e9cdfaf2d 77 } else {
scohennm 3:f68e9cdfaf2d 78 return;
scohennm 3:f68e9cdfaf2d 79 }
scohennm 3:f68e9cdfaf2d 80 #ifdef PRINTDEBUG
scohennm 3:f68e9cdfaf2d 81 pc.printf(" %f,%d\n\r",scaling, scaleIndex );
scohennm 3:f68e9cdfaf2d 82 #endif
scohennm 2:7f347d6a6422 83 tempInt = (int)toneFreq;
scohennm 2:7f347d6a6422 84 sprintf (lcdData,"%4d",tempInt);
scohennm 2:7f347d6a6422 85 LCDMessNoDwell(lcdData);
scohennm 2:7f347d6a6422 86 return;
scohennm 2:7f347d6a6422 87 }
vbharam 4:d59d946649b8 88
vbharam 4:d59d946649b8 89
vbharam 4:d59d946649b8 90
scohennm 2:7f347d6a6422 91 void lightAdjust( float scaling) { // Control brightness of LED
scohennm 2:7f347d6a6422 92 float tempDutyFactor;
scohennm 2:7f347d6a6422 93
scohennm 3:f68e9cdfaf2d 94 tempDutyFactor = 1.0 - scaling; //LED is a sinking connection // anode is held at 5V
scohennm 3:f68e9cdfaf2d 95 led.write(tempDutyFactor); //adjusting duty factor
scohennm 2:7f347d6a6422 96 return;
scohennm 2:7f347d6a6422 97 }
scohennm 3:f68e9cdfaf2d 98
scohennm 2:7f347d6a6422 99 int main(){
scohennm 2:7f347d6a6422 100 int tempInt;
vbharam 4:d59d946649b8 101 float tempValue;
scohennm 2:7f347d6a6422 102 tonePeriod = 1.0/toneFreq;
scohennm 2:7f347d6a6422 103 soundOut.period(tonePeriod);
scohennm 3:f68e9cdfaf2d 104 led.period(tonePeriod);
scohennm 3:f68e9cdfaf2d 105 led.write(CHANNELOFF);
scohennm 3:f68e9cdfaf2d 106 outPin.write(CHANNELOFF);
vbharam 4:d59d946649b8 107 // set up interrrupts to be used later for taps
vbharam 4:d59d946649b8 108 mybutton.fall(&pressed);
scohennm 2:7f347d6a6422 109
scohennm 2:7f347d6a6422 110 tempInt = (int)toneFreq;
scohennm 2:7f347d6a6422 111 sprintf (lcdData,"%4d",tempInt);
scohennm 2:7f347d6a6422 112 LCDMessNoDwell(lcdData);
scohennm 3:f68e9cdfaf2d 113 // Start data timer
scohennm 3:f68e9cdfaf2d 114 dataTimer.start();
scohennm 3:f68e9cdfaf2d 115 dataTimer.reset();
scohennm 3:f68e9cdfaf2d 116
scohennm 2:7f347d6a6422 117 while (true) {
scohennm 3:f68e9cdfaf2d 118 if (dataTimer.read_ms() > DATATIME){ // check to see if enough time has passed
scohennm 3:f68e9cdfaf2d 119 // to read the touch pad
scohennm 3:f68e9cdfaf2d 120 tempValue = tsiScaling.readPercentage();
scohennm 3:f68e9cdfaf2d 121 if(tempValue > 0) {
scohennm 3:f68e9cdfaf2d 122 // soundOut.write(TONEON); // set duty factor to 50%
scohennm 3:f68e9cdfaf2d 123 diatonicAdjust(tempValue);
scohennm 3:f68e9cdfaf2d 124 lightAdjust(tempValue);
scohennm 3:f68e9cdfaf2d 125 } else {
scohennm 3:f68e9cdfaf2d 126 soundOut.write(TONEOFF); // set dutyfactor to 0%
scohennm 3:f68e9cdfaf2d 127 LCDMessNoDwell("SOFF");
scohennm 3:f68e9cdfaf2d 128 }
scohennm 3:f68e9cdfaf2d 129 dataTimer.reset();
scohennm 3:f68e9cdfaf2d 130 }
scohennm 2:7f347d6a6422 131 } // while forever
scohennm 2:7f347d6a6422 132 }// end main