Netcamprojects
Published © GPL3+

Arduino Powered Player Pianos and Player Xylophones

Code your favorite music and play it on a Casio keyboard or on one of the two xylophones described in the project.

IntermediateFull instructions provided13,792
Arduino Powered Player Pianos and Player Xylophones

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Wiring Chart of the Universal Shield

Overview of the point to point wiring for the Arduino shield

Circuit Diagram

Overview of the Control Panel and its Circuits

Control Panel Diagrams

Overview

Code

Robo-C

C/C++
This is the code that drives the player piano
{SONG_END, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, WHITE_KEY},               
          };
   //This the end of the coding of the scales and of the song. We will now generate the first note as explained in the comments.

    struct  myNote * pSong = Elise;                     
    void setup()                                                 //the setup routine runs once when the reset switch is pressed
                                                                        // the digital pins are initialized as outputs 
        {
     pSong = Elise;
  
     int i = START_BLACK_KEY;                 //Reset ports 0 to 19
     while(i <= END_BLACK_KEY)  {
     pinMode(i, OUTPUT);
     digitalWrite(i, LOW);
      i++;
           }
     i = START_WHITE_KEY;                      //Reset ports 22 to 53
     while(i <= END_WHITE_KEY)  {
     pinMode(i, OUTPUT);
     digitalWrite(i, LOW);
     i++;
	}
    {
     pinMode(20, OUTPUT);                             //Set up the ports to activate the sustain and volume relays
     pinMode(21, OUTPUT);
      }
    } 
     void playTune(struct myNote * pTune) {
     byte Port1 = pTune->Port;                          // Select a Port
     int Soltime= pTune->Soltime;                    // Select one of 4 setting for the solenoids                                       
     if(Port1 == SONG_END) {                        // End the song when Port 255 is detected
     pSong = pTune;
     return;
      }
             if(pTune->Sustain == 1) {                            // Turns on the sustain feature on Port 20
      digitalWrite(20, HIGH);
      }
            if(pTune->Sustain == 0) {                             //Turns off the sustain feature on Port 20
      digitalWrite(20, LOW);
      } 
         if(pTune->Volume == 1) {                               // Turns the volume feature to low on Port 21
      digitalWrite(21, HIGH);
      }
           if(pTune->Volume == 0) {                            //Turns the volume feature to full on Port 21
      digitalWrite(21, LOW);
      }
            int beat = pTune->Beat ;                              // Establishes the beat time of the song (millisecs)
      beat = beat * beatMult[pTune->Duration]/4;    // Establishes the duration of the 5 notes
                                                                                // from Full to one Sixteenth based on the beat (millisecs)
      int durMS =  beat  * Soltime/4 ;                        // Establishes the amount of time that the solenoids
                                                                                 // are energized .25,.50,.75 or 1.0 of the beat time (millisecs)
      if(pTune->BlackKey == HIGH) {                     // the black keys are driven directly from a Mega port in this version
      digitalWrite(5, pTune->blackStateP5);             // of Robo-C. These 7 lines of code are not used.
      digitalWrite(6, pTune->blackStateP6);
      digitalWrite(7, pTune->blackStateP7);
      digitalWrite(8, pTune->blackStateP8);
      digitalWrite(9, pTune->blackStateP9);    
     } else { 
       if(Port1) {
       digitalWrite(Port1, HIGH);                               // Execute command HIGH for the designated white or black note
             }                                                                   // Energize the solenoids for time duration durMs 
      }     
     struct myNote * pChordTune = pTune;
     
     if((pChordTune->Chord == CHORD_START) || (pChordTune->Chord == CHORD_CONTINUE)) {
        pChordTune++;                                                  // Combine individual notes into chords
        playTune(pChordTune);
        }
              if((pTune->Chord == CHORD_END) || (pTune->Chord == 0)) {  
      delay (durMS);                                                    // Determines the length of time that the 
      }                                                                           // Solenoid is energized. One delay per chord 
    
      if(pTune->BlackKey == HIGH) {                     //Turn off the black keys (ports 0 to 19) after the end of the durMS delay
      int i = START_BLACK_KEY;
      while(i <= END_BLACK_KEY)  {
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);
         i++;
            }
      }else {
         int i = START_BLACK_KEY;                                 
     while(i <= END_BLACK_KEY)  {
     pinMode(i, OUTPUT);
     digitalWrite(i, LOW);
      i++;
        }
            i = START_WHITE_KEY;                        //Turn off the white keys (ports 22 to 53) after the end of the durMS delay
      while(i <= END_WHITE_KEY)  {
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);
      i++;
       }
     } 
     // Now wait out the beat time (the duration of the note)  
                	      if((pTune->Chord == CHORD_END) || (pTune->Chord == 0)) {
      pTune++;
      pSong = pTune;   
      delay (beat-durMS);                                         //After the completion of the note or chord, select the next note       
        
     //Serial.begin(9600);                                          // Activate the Serial Monitor (commented out unless needed)
     //int n = (beat-durMS);
     //Serial.println (n) ;
       }
     }     
      void loop() // the loop routine runs over and over 
     {      
	if(pSong->Port == SONG_END) {
		pSong = Elise;
		delay(2000);
	}
       playTune(pSong);  
      }           

Credits

Netcamprojects

Netcamprojects

0 projects • 9 followers

Comments