John's code modified for envelope shaping and different colours

Dependencies:   mbed

Fork of Morse-buddy-JohnF by Tony Abbey

Revision:
2:260128ae5746
Parent:
1:ba9c803c7596
Child:
3:cbd036b6942b
--- a/Morse_Code.cpp	Sat Jun 22 23:13:14 2013 +0000
+++ b/Morse_Code.cpp	Thu Jun 27 10:52:29 2013 +0000
@@ -2,7 +2,7 @@
 //==============================================================
 //  Morse Code  June 30, 2011  John H. Fisher - K5JHF
 //==============================================================
-// mods by Tony Abbey for envelope shaping - 22 June 2013
+// mods by Tony Abbey for envelope shaping and coloured LEDs - 27 June 2013
 
 #include "mbed.h"
 
@@ -12,38 +12,38 @@
 
 const float sine_wave [ 32 ]    =   {
     
-    sin (  1 * step ) / 2.0 + 0.5 ,
-    sin (  2 * step ) / 2.0 + 0.5 ,
-    sin (  3 * step ) / 2.0 + 0.5 ,
-    sin (  4 * step ) / 2.0 + 0.5 ,
-    sin (  5 * step ) / 2.0 + 0.5 ,
-    sin (  6 * step ) / 2.0 + 0.5 ,
-    sin (  7 * step ) / 2.0 + 0.5 ,
-    sin (  8 * step ) / 2.0 + 0.5 ,
-    sin (  9 * step ) / 2.0 + 0.5 ,
-    sin ( 10 * step ) / 2.0 + 0.5 ,
-    sin ( 11 * step ) / 2.0 + 0.5 ,
-    sin ( 12 * step ) / 2.0 + 0.5 ,
-    sin ( 13 * step ) / 2.0 + 0.5 ,
-    sin ( 14 * step ) / 2.0 + 0.5 ,
-    sin ( 15 * step ) / 2.0 + 0.5 ,
-    sin ( 16 * step ) / 2.0 + 0.5 ,
-    sin ( 17 * step ) / 2.0 + 0.5 ,
-    sin ( 18 * step ) / 2.0 + 0.5 ,
-    sin ( 19 * step ) / 2.0 + 0.5 ,
-    sin ( 20 * step ) / 2.0 + 0.5 ,
-    sin ( 21 * step ) / 2.0 + 0.5 ,
-    sin ( 22 * step ) / 2.0 + 0.5 ,
-    sin ( 23 * step ) / 2.0 + 0.5 ,
-    sin ( 24 * step ) / 2.0 + 0.5 ,
-    sin ( 25 * step ) / 2.0 + 0.5 ,
-    sin ( 26 * step ) / 2.0 + 0.5 ,
-    sin ( 27 * step ) / 2.0 + 0.5 ,
-    sin ( 28 * step ) / 2.0 + 0.5 ,
-    sin ( 29 * step ) / 2.0 + 0.5 ,
-    sin ( 30 * step ) / 2.0 + 0.5 ,
-    sin ( 31 * step ) / 2.0 + 0.5 ,
-    sin (  0 * step ) / 2.0 + 0.5 
+    sin (  1 * step ) / 2.5 ,
+    sin (  2 * step ) / 2.5 ,
+    sin (  3 * step ) / 2.5 ,
+    sin (  4 * step ) / 2.5 ,
+    sin (  5 * step ) / 2.5 ,
+    sin (  6 * step ) / 2.5 ,
+    sin (  7 * step ) / 2.5 ,
+    sin (  8 * step ) / 2.5 ,
+    sin (  9 * step ) / 2.5 ,
+    sin ( 10 * step ) / 2.5 ,
+    sin ( 11 * step ) / 2.5 ,
+    sin ( 12 * step ) / 2.5 ,
+    sin ( 13 * step ) / 2.5 ,
+    sin ( 14 * step ) / 2.5 ,
+    sin ( 15 * step ) / 2.5 ,
+    sin ( 16 * step ) / 2.5 ,
+    sin ( 17 * step ) / 2.5 ,
+    sin ( 18 * step ) / 2.5 ,
+    sin ( 19 * step ) / 2.5 ,
+    sin ( 20 * step ) / 2.5 ,
+    sin ( 21 * step ) / 2.5 ,
+    sin ( 22 * step ) / 2.5 ,
+    sin ( 23 * step ) / 2.5 ,
+    sin ( 24 * step ) / 2.5 ,
+    sin ( 25 * step ) / 2.5 ,
+    sin ( 26 * step ) / 2.5 ,
+    sin ( 27 * step ) / 2.5 ,
+    sin ( 28 * step ) / 2.5 ,
+    sin ( 29 * step ) / 2.5 ,
+    sin ( 30 * step ) / 2.5 ,
+    sin ( 31 * step ) / 2.5 ,
+    sin (  0 * step ) / 2.5 
 
     };
 
@@ -51,9 +51,8 @@
 
         Morse_Code::Morse_Code  (   float  WPM_Character,
                                     float  WPM_Speed,
-                                    int    Tone_Frequency,
-                                    char   LED_Pin,
-                                    char   Speaker_Pin  )  {
+                                    int    Tone_Frequency,  
+                                    char   colour )  {
    
             _WPM_Character  =  WPM_Character;
 
@@ -61,9 +60,7 @@
            
             _Tone_Frequency =  Tone_Frequency;
             
-            _LED_Pin        =  LED_Pin;
-            
-            _Speaker_Pin    =  Speaker_Pin;
+            _colour            =  colour;
 
             Calculate_Morse_Timing ( _WPM_Speed, _WPM_Character );
   
@@ -552,11 +549,14 @@
 
 void  Morse_Code::dit  ( )  {
   
-    led =   on;
+    if ( _colour == 1 ) blue =   on;
+    if ( _colour == 2 ) red = on;
+    if ( _colour == 3 ) green = on;
+    if ( _colour == 4 ) blue = on red = on green = on ;
 
     Tone ( _Tone_Frequency, u, 1.0 );
 
-    led =   off;
+    blue = off red = off green = off ;
   
    }
   
@@ -564,11 +564,14 @@
 
 void  Morse_Code::dah  ( )  {
 
-    led =   on;
+    if ( _colour == 1 ) blue = on;
+    if ( _colour == 2 ) red = on;
+    if ( _colour == 3 ) green = on;
+    if ( _colour == 4 ) blue = on red = on green = on ;
 
     Tone ( _Tone_Frequency, u, 3.0 );
 
-    led =   off;
+       blue = off red = off green = off ;
   
     }
   
@@ -592,13 +595,13 @@
     
         for ( i = 0; i < 32; i++ )  {
 
-            piezo  =   sine_wave [ i ] * amplitude;
+            piezo  =   sine_wave [ i ] * amplitude + 0.4;
     
             for ( delay = 0; delay < freq_factor; delay++ );  
 
             } t = timer.read() ;
-             if ( t < duration/7 ) amplitude = t * 7 / duration ;
-             if ( t/multiplier > duration * 0.85 ) amplitude = ( ( duration - t/multiplier ) / duration  ) * 8.5 ;
+             if ( t < duration/5 ) amplitude = t * 5 / duration ;
+             if ( t/multiplier > duration * 0.8 ) amplitude = ( ( duration - t/multiplier ) / duration  ) * 8 ;
 
         } while ( timer.read() < duration * multiplier );