#include "notes.h"

const int NUM_BUTTONS = 6;
const int BUTTON_NOTES[NUM_BUTTONS] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4};
const int BUTTON_PINS[NUM_BUTTONS] =  {2, 3, 4, 5, 6, 7};
const int SPEAKER_PIN = 11;

void setup() {
  // Initialize the pin we're going to use for the speaker.
  pinMode(SPEAKER_PIN, OUTPUT);
  // Initialize each of the pins we're going to use for the buttons. Using the INPUT_PULLUP mode 
  // means that the baseline value is HIGH, and when the button is pressed the value is LOW. This
  // mode allows us to skip adding pull up or pull down resistors externally.
  for (int i = 0; i < NUM_BUTTONS; ++i) {
    pinMode(BUTTON_PINS[i], INPUT_PULLUP);
  }
}

void loop() {
  // This variable tracks which button is being actively pressed down. It has a static lifetime, 
  // meaning its value persists across invocations of loop().
  static int activeButtonIdx = -1;
  // This variable tracks whether a button is currently being pressed, we use it to know whether
  // we should reset activeButtonIdx.
  bool buttonPressed = false;
  
  // Iterate through the array of buttons. If we find a button that is being pressed, handle it 
  // accordingly. The way this is written gives precedence to the button with the lower index in
  // the array. Meaning if buttons 0 and 1 are being pressed down simultaneously, we will play 
  // button 0's tone.
  for (int i = 0; i < NUM_BUTTONS; ++i) {
    // Check if this button is currently being pressed.
    if (digitalRead(BUTTON_PINS[i]) == LOW) {
      // Check if this is a new button press.
      if (activeButtonIdx != i) {
        // Stop currently playing tone (if there is one).
        if (activeButtonIdx >= 0) {
          noTone(SPEAKER_PIN);
        }
        // Play the new tone and set activeButtonIdx.
        tone(SPEAKER_PIN, BUTTON_NOTES[i]);
        activeButtonIdx = i;
      }
      // If we've detected a button being pressed, we want to bail out of the loop regardless of 
      // whether it is a new button press. This is what gives "priority" to buttons with lower
      // indices.
      buttonPressed = true;
      break;
    }
  }

  // If we haven't detected any of our buttons being actively pressed down, we should stop playing
  // a tone and reset activeButtonIdx.
  if (!buttonPressed && activeButtonIdx >= 0) {
    noTone(SPEAKER_PIN);
    activeButtonIdx = -1;
  }
}
#include "notes.h"

const int BUTTON_PIN = 2;
const int SPEAKER_PIN = 11;

void setup() {
  // Initialize the speaker pin
  pinMode(SPEAKER_PIN, OUTPUT);
  // Initialize the button pin. Using the INPUT_PULLUP mode means that the baseline value is HIGH, 
  // and when the button is pressed the value is LOW. This mode allows us to skip adding pull up or 
  // pull down resistors externally.
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  // Check whether the button is pressed. If the value is LOW it means the button is being pressed.
  if (digitalRead(BUTTON_PIN) == LOW)
  {
    // FILL IN YOUR CODE HERE
  }
  else
  {
    // FILL IN YOUR CODE HERE
  }
}