#define BASE_OCTAVE 4
#define C4 261.63
#define CS4 277.18
#define D4 293.66
#define DS4 311.13
#define E4 329.63
#define F4 349.23
#define FS4 369.99
#define G4 392.00
#define GS4 415.30
#define A4 440.00
#define AS4 466.16
#define B4 493.88
enum Notes { C, CS, D, DS, E, F, FS, G, GS, A, AS, B };
const char noteName[][3] = { "C", "CS", "D", "DS", "E", "F", "FS", "G", "GS", "A", "AS", "B" };
const float frequency[] = { C4, CS4, D4, DS4, E4, F4, FS4, G4, GS4, A4, AS4, B4 };
#define SPEAKER_PIN 8
int myNote;
int myOct;
unsigned int pitch;
// tone(pin, frequency) accetta un unsigned int come frequenza
unsigned int findFrequency (int note, int octave) {
unsigned int result;
float baseFrequency = frequency[note];
if (octave > BASE_OCTAVE)
result = baseFrequency * ((octave - BASE_OCTAVE) * 2);
else
result = baseFrequency / ((BASE_OCTAVE - octave) * 2);
return result;
}
void printNote(char* name, int octave, unsigned int freq){
Serial.print(name);
Serial.print(octave);
Serial.print( " note pitch frequency: ");
Serial.print(freq);
Serial.println("Hz");
}
void setup() {
Serial.begin(9600);
pinMode(SPEAKER_PIN, OUTPUT);
myNote = C;
myOct = 5;
pitch = findFrequency (myNote, myOct);
printNote(noteName[myNote], myOct, pitch);
tone(SPEAKER_PIN, pitch);
delay(1000);
noTone(SPEAKER_PIN);
myNote = E;
myOct = 5;
pitch = findFrequency (myNote, myOct);
printNote(noteName[myNote], myOct, pitch);
tone(SPEAKER_PIN, pitch);
delay(500);
noTone(SPEAKER_PIN);
myNote = D;
myOct = 5;
pitch = findFrequency (myNote, myOct);
printNote(noteName[myNote], myOct, pitch);
tone(SPEAKER_PIN, pitch);
delay(500);
noTone(SPEAKER_PIN);
}
void loop() {
// put your main code here, to run repeatedly:
}