unkwntech

Old McDonald Shield Base

Apr 8th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System.Threading;
  2. using NetduinoGo;
  3. using PWM = Microsoft.SPOT.Hardware.PWM;
  4.  
  5. namespace NetduinoGo_ShieldBase
  6. {
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             ShieldBase sb = new ShieldBase((GoBus.GoSocket)4);
  12.  
  13.             PWM speaker = new PWM(sb.PWMChannels.PWM_3, 50, .5, false);
  14.  
  15.             System.Collections.Hashtable scale = new System.Collections.Hashtable
  16.                                                      {
  17.                                                          {"c", 1915u},
  18.                                                          {"d", 1700u},
  19.                                                          {"e", 1519u},
  20.                                                          {"f", 1432u},
  21.                                                          {"g", 1275u},
  22.                                                          {"a", 1136u},
  23.                                                          {"b", 1014u},
  24.                                                          {"C", 956u},
  25.                                                          {"D", 851u},
  26.                                                          {"E", 758u},
  27.                                                          {"h", 0u}
  28.                                                      };
  29.  
  30.             int beatsPerMinute = 90;
  31.             int beatTimeInMilliseconds =
  32.             60000 / beatsPerMinute; // 60,000 milliseconds per minute
  33.             int pauseTimeInMilliseconds = (int)(beatTimeInMilliseconds * 0.1);
  34.  
  35.             // define the song (letter of note followed by length of note)
  36.             string song = "C1C1C1g1a1a1g2E1E1D1D1C2";
  37.  
  38.             for (int i = 0; i < song.Length; i += 2)
  39.             {
  40.                 // extract each note and its length in beats
  41.                 string note = song.Substring(i, 1);
  42.                 int beatCount = int.Parse(song.Substring(i + 1, 1));
  43.                 uint noteDuration = (uint)scale[note];
  44.  
  45.                 speaker.Duration = noteDuration;
  46.                 speaker.Period = noteDuration*2;
  47.  
  48.                 speaker.Start();
  49.  
  50.                 Thread.Sleep(beatTimeInMilliseconds * beatCount - pauseTimeInMilliseconds);
  51.  
  52.                 speaker.Stop();
  53.  
  54.                 Thread.Sleep(pauseTimeInMilliseconds);
  55.             }
  56.  
  57.             Thread.Sleep(Timeout.Infinite);
  58.         }
  59.  
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment