Adding music to the Arduino Sketch

Now that all the bugs are sorted out and the boolean functions are functioning it is time to add a bit of sound. Before using the computer keyboard I played around with ascending and descending tones on the piano keyboard. Digital is all well and good but the tactile interface of an analogue device is always preferable (which is what this assignment is all about). Using a scale of notes is much more pleasing to the ear than just a bunch of random sounds.



The next step is to figure out the correct code to make the piezo buzzer play approximations of the notes that I want.
Thankfully the internet is our friend here.
You Tuber, Sam, from Sam's Neat Projects has this rather wholesome and nicely illustrated video (https://www.youtube.com/watch?v=K8AnlUT0ng0)

Sam gives us the following diagram...


Using this as a guide will take away a lot of trial and error (as opposed to the time that I spent a whole week tuning PVC plumbing pipes to multiple pentatonic scales for an interactive art exhibit, but that is another story).

Now it is time to get to the computer code. After a bit of playing around (the piezo buzzer is no match for an upright piano) I have settled on a few nice sequences.

And here is the refined code....

// Punch Your Lights Out Vest Code
// on start: vest strike zones light up
// when a strike zone is activated (button press) the corresponding light goes out (and a buzzer sounds)
// when all lights are out a buzzer sounds


#include <Bounce2.h>


Bounce debouncerA = Bounce(); // Instantiate a Bounce object - ie. reads a button push
Bounce debouncerB = Bounce(); // Instantiate a Bounce object
Bounce debouncerC = Bounce(); // Instantiate a Bounce object
Bounce debouncerD = Bounce(); // Instantiate a Bounce object
Bounce debouncerE = Bounce(); // Instantiate a Bounce object
Bounce debouncerF = Bounce(); // Instantiate a Bounce object


const int ledA = 2; // BLACK these are the LED pins, constants won't change
const int ledB = 3; // WHITE
const int ledC = 4; // GREY
const int ledD = 5; // PURPLE
const int ledE = 6; // BLUE
const int ledF = 7; // GREEN


const int BUTTON_A = 8; // YELLOW these are the button pins, constants won't change
const int BUTTON_B = 9; // ORANGE
const int BUTTON_C = 10; // RED
const int BUTTON_D = 11; // BLACK
const int BUTTON_E = 12; // WHITE
const int BUTTON_F = 13; // GREY

const int buzzerPin = 14; // set the piezo pin number to analogue pin 0

bool ledAON = true;
bool ledBON = true;
bool ledCON = true;
bool ledDON = true;
bool ledEON = true;
bool ledFON = true;

bool ledAOFF = false;
bool ledBOFF = false;
bool ledCOFF = false;
bool ledDOFF = false;
bool ledEOFF = false;
bool ledFOFF = false;

#define frequency1 500 // sets the frequency #1
#define duration1 300 // sets the duration #1

#define frequency2 2000 // sets the frequency #2
#define duration2 800 // sets the duration #2

#define noteC 261 // sets the frequency (hertz) of the notes
#define noteCsharp 277
#define noteD 294
#define noteDsharp 311
#define noteE 330
#define noteF 349
#define noteFsharp 370
#define noteG 392
#define note Gsharp 415
#define noteA 440

void setup()
{

  debouncerA.attach(BUTTON_A, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerA.interval(25); // Use a debounce interval of 25 milliseconds

  debouncerB.attach(BUTTON_B, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerB.interval(25); // Use a debounce interval of 25 milliseconds

  debouncerC.attach(BUTTON_C, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerC.interval(25); // Use a debounce interval of 25 milliseconds

  debouncerD.attach(BUTTON_D, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerD.interval(25); // Use a debounce interval of 25 milliseconds

  debouncerE.attach(BUTTON_E, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerE.interval(25); // Use a debounce interval of 25 milliseconds

  debouncerF.attach(BUTTON_F, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  debouncerF.interval(25); // Use a debounce interval of 25 milliseconds

  pinMode (ledA, OUTPUT);
  pinMode (ledB, OUTPUT);
  pinMode (ledC, OUTPUT);
  pinMode (ledD, OUTPUT);
  pinMode (ledE, OUTPUT);
  pinMode (ledF, OUTPUT);

  pinMode(buzzerPin, OUTPUT);

  Serial.begin(9600);
}

void loop()
{
  debouncerA.update(); // Update the Bounce instance
  debouncerB.update(); // Update the Bounce instance
  debouncerC.update(); // Update the Bounce instance
  debouncerD.update(); // Update the Bounce instance
  debouncerE.update(); // Update the Bounce instance
  debouncerF.update(); // Update the Bounce instance


  digitalWrite (ledA, ledAON); // turn on all the LEDs
  digitalWrite (ledB, ledBON);
  digitalWrite (ledC, ledCON);
  digitalWrite (ledD, ledDON);
  digitalWrite (ledE, ledEON);
  digitalWrite (ledF, ledFON);


  if (debouncerA.fell())    // press button A
  {
    ledAON = false;
    Serial.print("hitA"); // remembers the button press
    digitalWrite (ledA, ledAOFF);
    tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
  }

  if (debouncerB.fell())    // press button B
  {
    ledBON = false;
    Serial.print("hitB"); // remembers the button press
    digitalWrite (ledB, ledBOFF);
    tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
  }

  if (debouncerC.fell())    // press button C
  {
    ledCON = false;
    Serial.print("hitC"); // remembers the button press
    digitalWrite (ledC, ledCOFF);
    tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
  }

  if (debouncerD.fell())    // press button D
  {
    ledDON = false;
    Serial.print("hitD"); // remembers the button press
    digitalWrite (ledD, ledDOFF);
    tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
  }

  if (debouncerE.fell())    // press button E
  {
    ledEON = false;
    Serial.print("hitE"); // remembers the button press
    digitalWrite (ledE, ledEOFF);
    tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
  }

  if (debouncerF.fell()) // press button F
  {
    ledFON = false;
    Serial.print("hitF"); // remembers the button press
    digitalWrite (ledF, ledFOFF);
    tone(buzzerPin, frequency1, duration1); //( pin number, frequency in hertz, duration in milliseconds);
  }

  // once all buttons have been hit once play an end of game tone then STOP all functions
  if (!ledAON && !ledBON && !ledCON && !ledDON && !ledEON && !ledFON)

  { delay (500);
    tone(buzzerPin, noteG, 300);
    delay (600);
    tone(buzzerPin, noteF, 300);
    delay (600);
    tone(buzzerPin, noteE, 300);
    delay (600);
    tone(buzzerPin, noteD, 300);
    delay (600);
    tone(buzzerPin, noteCsharp, 1000);
    delay (1000);

    while (1); // STOP hammertime
  }
}


Comments