#include "pitches.h"
#define SPEAKER_PIN 8
int btnPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int btnTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int n = sizeof(btnPins) / sizeof(btnPins[0]);
void setup() {
for(int i=0;i<n;i++){
pinMode(btnPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:2
int pitch = 0;
for (int i = 0; i < n; i++) {
if (digitalRead(btnPins[i]) == LOW) {
pitch = btnTones[i];
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
}