Domenic Datti
Published © MIT

Meadow v2 - Jingle Bells with Piezo Speaker

Learn how to wire up your Meadow Micro v2.x to play (and stop playing) jingle bells at the press of a button.

BeginnerFull instructions provided35
Meadow v2 - Jingle Bells with Piezo Speaker

Things used in this project

Hardware components

Meadow F7v2 Micro Development Kit
Wilderness Labs Meadow F7v2 Micro Development Kit
×1
Buzzer, Piezo
Buzzer, Piezo
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 100 ohm
Resistor 100 ohm
×1

Story

Read more

Schematics

Fritzing diagram showing the wire up

Wiring diagram

Code

Application code

C#
using Meadow;
using Meadow.Devices;
using Meadow.Foundation;
using Meadow.Foundation.Leds;
using System;
using Meadow.Foundation.Audio;
using Meadow.Foundation.Sensors.Buttons;
using Meadow.Hardware;
using System.Threading.Tasks;

namespace MeadowApplication
{
    // Change F7MicroV2 to F7Micro for V1.x boards
    public class MeadowApp : App<F7MicroV2, MeadowApp>
    {
        RgbPwmLed onboardLed;
        private IPwmPort pwm;
        PiezoSpeaker speaker;
        PushButton button;

        const string NOTES = "eeeeeeegcdefffffeeeeddedgeeeeeeegcdefffffeeeggfdc ";
        readonly int[] BEATS = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
                                 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1};

        const int TEMPO = 300;
        bool playing = false;

        public MeadowApp()
        {
            Initialize();
            PlayNote('c', 50).Wait();
            PlayJingleBells().ConfigureAwait(false);
        }

        protected async Task PlayJingleBells()
        {
            while (true)
            {
                if (playing)
                {
                    Console.WriteLine("Playing jingle bells!");
                    for (int i = 0; i < NOTES.Length && playing; i++)
                    {
                        if (NOTES[i] == ' ')
                        {
                            await Task.Delay(BEATS[i] * TEMPO); // rest
                        }
                        else
                        {
                            await PlayNote(NOTES[i], BEATS[i] * TEMPO);
                            // pause between notes
                            await Task.Delay(BEATS[i] * TEMPO / 2);
                        }


                    }
                    Console.WriteLine("From the top!");
                }
                else
                {
                    // Slow the infinite loop down or the button interrupt breaks
                    await Task.Delay(TEMPO);
                }
            }
        }
        private async Task PlayNote(char note, int duration)
        {
            char[] names = { 'c', 'd', 'e', 'f', 'g', 'a', 'b'};
            int[] tones = { 523, 587, 659, 698, 783, 880, 987 };
            Color[] colors = { Color.Red, Color.Green, Color.White, Color.Blue, Color.Yellow, Color.Orange, Color.Purple };
            // play the tone corresponding to the note name
            for (int i = 0; i < names.Length; i++)
            {
                if (names[i] == note)
                {
                    var tone = tones[i];
                    onboardLed.SetColor(colors[i], .25f);
                    onboardLed.IsOn = true;
                    await speaker.PlayTone(tone, duration);
                    onboardLed.IsOn = false;

                    Console.WriteLine($"Playing note {note} tone {tone} for {duration}");
                }
            }
        }

        void Initialize()
        {
            Console.WriteLine("Initialize hardware...");

            onboardLed = new RgbPwmLed(device: Device,
                redPwmPin: Device.Pins.OnboardLedRed,
                greenPwmPin: Device.Pins.OnboardLedGreen,
                bluePwmPin: Device.Pins.OnboardLedBlue,
                3.3f, 3.3f, 3.3f,
                Meadow.Peripherals.Leds.IRgbLed.CommonType.CommonAnode);

            pwm = Device.CreatePwmPort(Device.Pins.D04, 100, .5f, true);
            speaker = new PiezoSpeaker(pwm);

            button = new PushButton(Device, Device.Pins.D01);

            button.Clicked += (o, e) => { playing = !playing; };
        }
    }
}

Credits

Domenic Datti

Domenic Datti

0 projects • 0 followers

Comments

Add projectSign up / Login