#include "pitches.h"
#define SPEAKER_PIN 8
#define SW 3
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5,
NOTE_B4, NOTE_A4, NOTE_G4, NOTE_F4,
NOTE_E4, NOTE_D4, NOTE_C4
};
void setup()
{
pinMode(SW, INPUT_PULLUP);
for (uint8_t i=0;i<8;i++)
{
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop()
{
int pitch=0;
if(digitalRead(SW)==0)
{
for(uint8_t i=0;i<15;i++)
{
tone(8,buttonTones[i]);
delay(200);
}
}
for(uint8_t i=0;i<8;i++)
{
if(digitalRead(buttonPins[i])==0)
{
pitch=buttonTones[i];
}
}
if (pitch)
{
tone(SPEAKER_PIN,pitch);
}
else
{
noTone(SPEAKER_PIN);
}
}