Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

CIRC-06 | Music With Piezos Netduino Plus 2 MS Port


Best Answer qsnapper, 07 January 2013 - 09:17 PM

If anyone having the same issue, the below is the updated working code for Microsoft.SPOT.Hardware.PWM

 

using System;                               //use base classes                            using System.Threading;                     //use threading classes        using Microsoft.SPOT;                       //use smart personal objects technology classesusing Microsoft.SPOT.Hardware;              //use hardware related SPOT classesusing SecretLabs.NETMF.Hardware;            //use Secret Labs hardware frameworkusing SecretLabs.NETMF.Hardware.Netduino;   //use the Netduino specific classesnamespace Piezo{    public class Program    {        //static PWM speaker = new PWM(Pins.GPIO_PIN_D9);  // Add the following line instead of this one        static PWM speaker = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D9, 100000, 0.5, false);        static char[] notes = "ccggaagffeeddc ".ToCharArray();        static int[] beats = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };        static int tempo = 300;        public static void Main()        {            speaker.Start();        // Add this            while (true)            {                for (int i = 0; i < notes.Length; i++)                {                    playNote(notes[i], beats[i] * tempo);                    playNote(' ', tempo / 2);                }            }        }        /// <summary>        /// Plays a particular tone for a particular duration        /// </summary>        /// <param name="tone">The tone to play</param>        /// <param name="duration">How long to play the tone</param>        static void playTone(int tone, int duration)        {            //speaker.SetPulse((uint)(tone * 2), (uint)tone);     //Uncomment this nad use the next two lines instead            speaker.Period = (uint)tone * 2;            speaker.Duration = (uint)tone;            Thread.Sleep(duration);                             //waits the appropriate amount of time        }        /// <summary>        /// Looks up the tone for a note based on it's letter and plays        /// it for a specific amount of time        /// </summary>        /// <param name="note">The letter of the note to play</param>        /// <param name="duration">How long to play the tone</param>        static void playNote(char note, int duration)        {            char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', ' ' };    //The array of possible notes            int[] tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 0 };//The frequency of each note in the previous array            for (int i = 0; i < 9; i++)     //Goes through each note            {                if (names[i] == note)                {                    playTone(tones[i], duration); //Plays the right note                }            }        }    }}
Go to the full post


  • Please log in to reply
4 replies to this topic

#1 Jean-Martin

Jean-Martin

    Member

  • Members
  • PipPip
  • 18 posts
  • Location[FR], Montreal, Canada

Posted 05 January 2013 - 10:41 AM

Edited: to the right Version of the script. Jean-Martin

using System;                               //use base classes                            using System.Threading;                     //use threading classes        using Microsoft.SPOT;                       //use smart personal objects technology classesusing Microsoft.SPOT.Hardware;              //use hardware related SPOT classesusing SecretLabs.NETMF.Hardware;            //use Secret Labs hardware frameworkusing SecretLabs.NETMF.Hardware.Netduino;   //use the Netduino specific classesnamespace Piezo{    public class Program    {        //static PWM speaker = new PWM(Pins.GPIO_PIN_D9);  // Add the following line instead of this one        static PWM speaker = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D9, 100000, 0.5, false);        static char[] notes = "ccggaagffeeddc ".ToCharArray();        static int[] beats = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };        static int tempo = 300;        public static void Main()        {            speaker.Start();        // Add this            while (true)            {                for (int i = 0; i < notes.Length; i++)                {                    playNote(notes[i], beats[i] * tempo);                    playNote(' ', tempo / 2);                }            }        }        /// <summary>        /// Plays a particular tone for a particular duration        /// </summary>        /// <param name="tone">The tone to play</param>        /// <param name="duration">How long to play the tone</param>        static void playTone(int tone, int duration)        {            //speaker.SetPulse((uint)(tone * 2), (uint)tone);     //Uncomment this nad use the next two lines instead            speaker.Period = (uint)tone * 2;            speaker.Duration = (uint)tone;            Thread.Sleep(duration);                             //waits the appropriate amount of time        }        /// <summary>        /// Looks up the tone for a note based on it's letter and plays        /// it for a specific amount of time        /// </summary>        /// <param name="note">The letter of the note to play</param>        /// <param name="duration">How long to play the tone</param>        static void playNote(char note, int duration)        {            char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', ' ' };    //The array of possible notes            int[] tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 0 };//The frequency of each note in the previous array            for (int i = 0; i < 9; i++)     //Goes through each note            {                if (names[i] == note)                {                    playTone(tones[i], duration); //Plays the right note                }            }        }    }}

 

 

 



Jean-Martin


#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 05 January 2013 - 01:28 PM

id change it for normal microsoft pwm class. very easy to port.



#3 qsnapper

qsnapper

    New Member

  • Members
  • Pip
  • 8 posts

Posted 07 January 2013 - 09:17 PM   Best Answer

If anyone having the same issue, the below is the updated working code for Microsoft.SPOT.Hardware.PWM

 

using System;                               //use base classes                            using System.Threading;                     //use threading classes        using Microsoft.SPOT;                       //use smart personal objects technology classesusing Microsoft.SPOT.Hardware;              //use hardware related SPOT classesusing SecretLabs.NETMF.Hardware;            //use Secret Labs hardware frameworkusing SecretLabs.NETMF.Hardware.Netduino;   //use the Netduino specific classesnamespace Piezo{    public class Program    {        //static PWM speaker = new PWM(Pins.GPIO_PIN_D9);  // Add the following line instead of this one        static PWM speaker = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D9, 100000, 0.5, false);        static char[] notes = "ccggaagffeeddc ".ToCharArray();        static int[] beats = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };        static int tempo = 300;        public static void Main()        {            speaker.Start();        // Add this            while (true)            {                for (int i = 0; i < notes.Length; i++)                {                    playNote(notes[i], beats[i] * tempo);                    playNote(' ', tempo / 2);                }            }        }        /// <summary>        /// Plays a particular tone for a particular duration        /// </summary>        /// <param name="tone">The tone to play</param>        /// <param name="duration">How long to play the tone</param>        static void playTone(int tone, int duration)        {            //speaker.SetPulse((uint)(tone * 2), (uint)tone);     //Uncomment this nad use the next two lines instead            speaker.Period = (uint)tone * 2;            speaker.Duration = (uint)tone;            Thread.Sleep(duration);                             //waits the appropriate amount of time        }        /// <summary>        /// Looks up the tone for a note based on it's letter and plays        /// it for a specific amount of time        /// </summary>        /// <param name="note">The letter of the note to play</param>        /// <param name="duration">How long to play the tone</param>        static void playNote(char note, int duration)        {            char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', ' ' };    //The array of possible notes            int[] tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 0 };//The frequency of each note in the previous array            for (int i = 0; i < 9; i++)     //Goes through each note            {                if (names[i] == note)                {                    playTone(tones[i], duration); //Plays the right note                }            }        }    }}


#4 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 07 January 2013 - 11:22 PM

good job! :)



#5 Jean-Martin

Jean-Martin

    Member

  • Members
  • PipPip
  • 18 posts
  • Location[FR], Montreal, Canada

Posted 08 January 2013 - 12:57 AM

If anyone having the same issue, the below is the updated working code for Microsoft.SPOT.Hardware.PWM

 

using System;                               //use base classes                            using System.Threading;                     //use threading classes        using Microsoft.SPOT;                       //use smart personal objects technology classesusing Microsoft.SPOT.Hardware;              //use hardware related SPOT classesusing SecretLabs.NETMF.Hardware;            //use Secret Labs hardware frameworkusing SecretLabs.NETMF.Hardware.Netduino;   //use the Netduino specific classesnamespace Piezo{    public class Program    {        //static PWM speaker = new PWM(Pins.GPIO_PIN_D9);  // Add the following line instead of this one        static PWM speaker = new Microsoft.SPOT.Hardware.PWM(PWMChannels.PWM_PIN_D9, 100000, 0.5, false);        static char[] notes = "ccggaagffeeddc ".ToCharArray();        static int[] beats = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };        static int tempo = 300;        public static void Main()        {            speaker.Start();        // Add this            while (true)            {                for (int i = 0; i < notes.Length; i++)                {                    playNote(notes[i], beats[i] * tempo);                    playNote(' ', tempo / 2);                }            }        }        /// <summary>        /// Plays a particular tone for a particular duration        /// </summary>        /// <param name="tone">The tone to play</param>        /// <param name="duration">How long to play the tone</param>        static void playTone(int tone, int duration)        {            //speaker.SetPulse((uint)(tone * 2), (uint)tone);     //Uncomment this nad use the next two lines instead            speaker.Period = (uint)tone * 2;            speaker.Duration = (uint)tone;            Thread.Sleep(duration);                             //waits the appropriate amount of time        }        /// <summary>        /// Looks up the tone for a note based on it's letter and plays        /// it for a specific amount of time        /// </summary>        /// <param name="note">The letter of the note to play</param>        /// <param name="duration">How long to play the tone</param>        static void playNote(char note, int duration)        {            char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', ' ' };    //The array of possible notes            int[] tones = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 0 };//The frequency of each note in the previous array            for (int i = 0; i < 9; i++)     //Goes through each note            {                if (names[i] == note)                {                    playTone(tones[i], duration); //Plays the right note                }            }        }    }}

Haha thank to the Chat guy !!!



Jean-Martin





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.