Hardware Reference
In-Depth Information
#define NOTE_G 98
#define NOTE_A 110
#define NOTE_B 123
//Volatile variables can change inside interrupts
volatile int key = NOTE_C;
volatile int octave_multiplier = 1;
void setup()
{
//Set up serial
Serial.begin(9600);
pinMode (SPEAKER, OUTPUT);
//The pin is inverted, so we want to look at the rising edge
attachInterrupt(BUTTON_INT, changeKey, RISING);
//Set up timer interrupt
Timer1.initialize(500000); // (.5 seconds)
Timer1.attachInterrupt(changePitch); //Runs "changePitch" on each
//timer interupt
}
void changeKey()
{
octave_multiplier = 1;
if (key == NOTE_C)
key = NOTE_D;
else if (key == NOTE_D)
key = NOTE_E;
else if (key == NOTE_E)
key = NOTE_F;
else if (key == NOTE_F)
key = NOTE_G;
else if (key == NOTE_G)
key = NOTE_A;
else if (key == NOTE_A)
key = NOTE_B;
else if (key == NOTE_B)
key = NOTE_C;
}
//Timer interrupt function
void changePitch()
{
octave_multiplier = octave_multiplier * 2;
if (octave_multiplier > 16) octave_multiplier = 1;
tone(SPEAKER,key*octave_multiplier);
}
void loop()
Search WWH ::




Custom Search