Added button interrupt to change from one octave to other

Dependencies:   SLCD TSI mbed

Fork of slider_diatonic_v1 by Stanley Cohen

Files at this revision

API Documentation at this revision

Comitter:
vbharam
Date:
Wed Feb 25 18:30:23 2015 +0000
Parent:
4:d59d946649b8
Commit message:
Added timer interrupt;

Changed in this revision

slider_diatonic_v1.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/slider_diatonic_v1.cpp	Sun Feb 22 07:57:45 2015 +0000
+++ b/slider_diatonic_v1.cpp	Wed Feb 25 18:30:23 2015 +0000
@@ -2,11 +2,11 @@
 #include "SLCD.h"
 #include "TSISensor.h"
 
-
 #define CHANNELON   0
 #define CHANNELOFF  1
 #define LCDLEN      10
-#define DATATIME    100 //milli seccnds
+#define DATATIME    50 //milli seccnds
+#define PUSHTIME    300 //milli seccnds
 //LCD messages
 
 #define TONEARRAY 
@@ -20,22 +20,28 @@
 #define SPEEDAST    0
 #define TONEAST     1
 #define NUMTONES    10
+#define NUMOCTS     2
 #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};
-float diatonicScaleOctave[NUMTONES] = {659.25, 698.46, 783.99, 880.00, 987.77, 1046.50, 1174.66, 1318.51, 1396.91, 1567.98 };
+// Array of array
+float diatonicScale[NUMOCTS][NUMTONES] = {{246.94, 261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25,587.33}, {659.25, 698.46, 783.99, 880.00, 987.77, 1046.50, 1174.66, 1318.51, 1396.91, 1567.98 }};
 SLCD slcd; //define LCD display
 
 TSISensor tsiScaling; // Capacitive sensor/slider
 
-InterruptIn mybutton(PTC3);  //push botton with internal pullup
+//InterruptIn mybutton(PTC3);  //push botton with internal pullup
 PwmOut led(LED_RED);
 DigitalOut outPin(PTC9); //J1-16
+// Right Switch set as a Digital Input
+DigitalIn buttonState(PTC3);
 PwmOut soundOut(PTA13);
+
+// Two timer Interrupts
 Timer dataTimer;
+Timer buttonTimer;
 // Global scalars
 char lcdData[LCDLEN];
 
@@ -43,13 +49,7 @@
 float toneFreq = SIDETONE;
 
 // Int used to switch between different octave 
-int relayState = CHANNELOFF;
-
-
-void pressed() // button intterupt
-{
-    relayState = !relayState;
-}
+int octaveState = 0;
 
 void LCDMessNoDwell(char *lMess){
         slcd.Home();
@@ -57,7 +57,9 @@
         slcd.printf(lMess);
 } 
   
-void diatonicAdjust( float scaling) {
+void diatonicAdjust( float scaling, int octaveState) {
+    // octaveState here works as the index of array diatonicScale
+    
     int tempInt;
     int scaleIndex;
     static int oldScaleIndex = 0;
@@ -66,10 +68,7 @@
 */
     scaleIndex = (int)(NUMTONES * scaling);
     if (scaleIndex != oldScaleIndex) {
-        if (relayState == CHANNELOFF)
-            toneFreq = diatonicScale[scaleIndex];
-        else 
-            toneFreq = diatonicScaleOctave[scaleIndex];
+        toneFreq = diatonicScale[octaveState][scaleIndex];
         tonePeriod = 1.0/toneFreq;  
         soundOut.period(tonePeriod); // adjusting period
         soundOut.write(TONEON); // there is a setup time for both period and DF
@@ -105,7 +104,7 @@
     led.write(CHANNELOFF);
     outPin.write(CHANNELOFF);
     // set up interrrupts to be used later for taps
-    mybutton.fall(&pressed);
+//    mybutton.fall(&pressed);
     
     tempInt = (int)toneFreq;
     sprintf (lcdData,"%4d",tempInt);
@@ -113,14 +112,31 @@
    // Start data timer
     dataTimer.start();
     dataTimer.reset(); 
+    
+    // Start button timer
+    buttonTimer.start();
+    buttonTimer.reset(); 
      
     while (true) {
+        //Button and data Timers are checked at different times
+        if (buttonTimer.read_ms() > PUSHTIME){ // check to see if enough time has passed
+                                            // to button interrupt
+            // If the pushbutton is pressed:                                            
+            if (buttonState == CHANNELON){
+                octaveState++;
+                octaveState = octaveState % NUMOCTS;
+                sprintf (lcdData,"%4d",octaveState);
+                LCDMessNoDwell(lcdData);
+            }
+            buttonTimer.reset();            
+        }                                            
+        
         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);
+                 soundOut.write(TONEON); // set duty factor to 50%
+                diatonicAdjust(tempValue, octaveState);
                 lightAdjust(tempValue);
             } else { 
                 soundOut.write(TONEOFF); // set dutyfactor to 0%