/* Robot Xylophone by Neil Fraser for Beverly June 25 2019 */ // Button pin. const int buttonPin = 13; // Pin numbers of each note. const int notePins[] = {11, 10, 9, 8, 7, 6, 5, 4}; // Total number of notes (length of notePins arry). const int noteCount = 8; // Strike time for hammer in ms. const int strikeMs = 10; // Key for Beverly's Xylophone // Index Note Colour // 0 C Blue // 1 D Green // 2 E Yellow // 3 F Orange // 4 G Red // 5 A Purple // 6 B White // 7 C Blue // Twinkle Twinkle Little Star (a.k.a Baa, Baa, Black Sheep or Alphabet song) const int twinkleNotes[] = { 0,0,4,4,5,5,4, 3,3,2,2,1,1,0, 4,4,3,3,2,2,1, 4,4,3,3,2,2,1, 0,0,4,4,5,5,4, 3,3,2,2,1,1,0, -1 }; const int twinkleTimes[] = { 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, 1,1,1,1,1,1,2, -1 }; const int twinkleNoteLength = 500; // Mary Had a Little Lamb const int lambNotes[] = { 2,1,0,1, 2,2,2, 1,1,1, 2,4,4, // or ...2,2,2 2,1,0,1, 2,2,2,2, 1,1,2,1, 0, -1 }; const int lambTimes[] = { 1,1,1,1, 1,1,2, 1,1,2, 1,1,2, 1,1,1,1, 1,1,1,1, 1,1,1,1, 4, -1 }; const int lambNoteLength = 300; // Frère Jacques const int jacquesNotes[] = { 0,1,2,0, 0,1,2,0, 2,3,4, 2,3,4, 4,5,4,3,2,0, 4,5,4,3,2,0, 0,4,0, 0,4,0, -1 }; const int jacquesTimes[] = { 2,2,2,2, 2,2,2,2, 2,2,4, 2,2,4, 1,1,1,1,2,2, 1,1,1,1,2,2, 2,2,4, 2,2,4, -1 }; const int jacquesNoteLength = 300; // Ode to Joy const int odeNotes[] = { 2,2,3,4, 4,3,2,1, 0,0,1,2, 2,1,1, 2,2,3,4, 4,3,2,1, 0,0,1,2, 1,0,0, 1,1,2,0, 1,2,3,2,0, 1,2,3,2,1, 0,1,4, 2,2,3,4, 4,3,2,1, 0,0,1,2, 1,0,0, -1 }; const int odeTimes[] = { 2,2,2,2, 2,2,2,2, 2,2,2,2, 3,1,4, 2,2,2,2, 2,2,2,2, 2,2,2,2, 3,1,4, 2,2,2,2, 2,1,1,2,2, 2,1,1,2,2, 2,2,4, 2,2,2,2, 2,2,2,2, 2,2,2,2, 3,1,4, -1 }; const int odeNoteLength = 200; // Cripple Creek const int crippleCreekNotes[] = { 7,7,7,4,2,3,5,4, 7,7,7,4,2,1,0,0, 7,7,7,4,2,3,5,4, 2,2,2,1,0,4,5,7, 2,2,2,1,0,2,4,4, 2,2,2,1,0,4,5,7, 2,2,2,1,0,2,4,4, 2,2,2,1,0,4,5,7, -1 }; const int crippleCreekTimes[] = { 2,1,1,2,2,2,2,4, 2,1,1,2,2,2,2,4, 2,1,1,2,2,2,2,4, 2,1,1,2,2,2,2,4, 2,1,1,2,2,2,2,4, 2,1,1,2,2,2,2,4, 2,1,1,2,2,2,2,4, 2,1,1,2,2,2,2,4, -1 }; const int crippleCreekNoteLength = 100; // Initialization function. void setup() { digitalWrite(LED_BUILTIN, LOW); // Initialize each note pin as an ouput and set to high (off). for (int i = 0; i < noteCount; i++) { int pin = notePins[i]; pinMode(pin, OUTPUT); digitalWrite(pin, HIGH); } // Initialize the button pin as an input. pinMode(buttonPin, INPUT); // Play diagnostic scale. wait(); for (int n = 0; n < noteCount; n++) { play(n, 50); play(n, 50); } } // Play one note (n = 0-7) for a time (milliseconds). void play(int n, int t) { int pin = notePins[n]; digitalWrite(pin, LOW); delay(strikeMs); digitalWrite(pin, HIGH); delay(t - strikeMs); } // Play a tune. void playTune(const int tuneNotes[], const int tuneTimes[], int tuneNoteLength) { int i = 0; while(tuneNotes[i] != -1 && tuneTimes[i] != -1) { play(tuneNotes[i], tuneTimes[i] * tuneNoteLength); i++; } } // Wait until the button is pushed. void wait() { while(digitalRead(buttonPin) == LOW) {} delay(1000); } // Play one tune after another, waiting for a button-push between each. void loop() { wait(); playTune(twinkleNotes, twinkleTimes, twinkleNoteLength); wait(); playTune(lambNotes, lambTimes, lambNoteLength); wait(); playTune(jacquesNotes, jacquesTimes, jacquesNoteLength); wait(); playTune(odeNotes, odeTimes, odeNoteLength); wait(); playTune(crippleCreekNotes, crippleCreekTimes, crippleCreekNoteLength); }