Devices & Components
Arduino Mega 2560 Rev3
Hardware & Tools
Soldering iron (generic)
Project description
Code
Robo-C
c_cpp
This is the code that drives the player piano
1{SONG_END, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, WHITE_KEY}, 2 }; 3 //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. 4 5 struct myNote * pSong = Elise; 6 void setup() //the setup routine runs once when the reset switch is pressed 7 // the digital pins are initialized as outputs 8 { 9 pSong = Elise; 10 11 int i = START_BLACK_KEY; //Reset ports 0 to 19 12 while(i <= END_BLACK_KEY) { 13 pinMode(i, OUTPUT); 14 digitalWrite(i, LOW); 15 i++; 16 } 17 i = START_WHITE_KEY; //Reset ports 22 to 53 18 while(i <= END_WHITE_KEY) { 19 pinMode(i, OUTPUT); 20 digitalWrite(i, LOW); 21 i++; 22 } 23 { 24 pinMode(20, OUTPUT); //Set up the ports to activate the sustain and volume relays 25 pinMode(21, OUTPUT); 26 } 27 } 28 void playTune(struct myNote * pTune) { 29 byte Port1 = pTune->Port; // Select a Port 30 int Soltime= pTune->Soltime; // Select one of 4 setting for the solenoids 31 if(Port1 == SONG_END) { // End the song when Port 255 is detected 32 pSong = pTune; 33 return; 34 } 35 if(pTune->Sustain == 1) { // Turns on the sustain feature on Port 20 36 digitalWrite(20, HIGH); 37 } 38 if(pTune->Sustain == 0) { //Turns off the sustain feature on Port 20 39 digitalWrite(20, LOW); 40 } 41 if(pTune->Volume == 1) { // Turns the volume feature to low on Port 21 42 digitalWrite(21, HIGH); 43 } 44 if(pTune->Volume == 0) { //Turns the volume feature to full on Port 21 45 digitalWrite(21, LOW); 46 } 47 int beat = pTune->Beat ; // Establishes the beat time of the song (millisecs) 48 beat = beat * beatMult[pTune->Duration]/4; // Establishes the duration of the 5 notes 49 // from Full to one Sixteenth based on the beat (millisecs) 50 int durMS = beat * Soltime/4 ; // Establishes the amount of time that the solenoids 51 // are energized .25,.50,.75 or 1.0 of the beat time (millisecs) 52 if(pTune->BlackKey == HIGH) { // the black keys are driven directly from a Mega port in this version 53 digitalWrite(5, pTune->blackStateP5); // of Robo-C. These 7 lines of code are not used. 54 digitalWrite(6, pTune->blackStateP6); 55 digitalWrite(7, pTune->blackStateP7); 56 digitalWrite(8, pTune->blackStateP8); 57 digitalWrite(9, pTune->blackStateP9); 58 } else { 59 if(Port1) { 60 digitalWrite(Port1, HIGH); // Execute command HIGH for the designated white or black note 61 } // Energize the solenoids for time duration durMs 62 } 63 struct myNote * pChordTune = pTune; 64 65 if((pChordTune->Chord == CHORD_START) || (pChordTune->Chord == CHORD_CONTINUE)) { 66 pChordTune++; // Combine individual notes into chords 67 playTune(pChordTune); 68 } 69 if((pTune->Chord == CHORD_END) || (pTune->Chord == 0)) { 70 delay (durMS); // Determines the length of time that the 71 } // Solenoid is energized. One delay per chord 72 73 if(pTune->BlackKey == HIGH) { //Turn off the black keys (ports 0 to 19) after the end of the durMS delay 74 int i = START_BLACK_KEY; 75 while(i <= END_BLACK_KEY) { 76 pinMode(i, OUTPUT); 77 digitalWrite(i, LOW); 78 i++; 79 } 80 }else { 81 int i = START_BLACK_KEY; 82 while(i <= END_BLACK_KEY) { 83 pinMode(i, OUTPUT); 84 digitalWrite(i, LOW); 85 i++; 86 } 87 i = START_WHITE_KEY; //Turn off the white keys (ports 22 to 53) after the end of the durMS delay 88 while(i <= END_WHITE_KEY) { 89 pinMode(i, OUTPUT); 90 digitalWrite(i, LOW); 91 i++; 92 } 93 } 94 // Now wait out the beat time (the duration of the note) 95 if((pTune->Chord == CHORD_END) || (pTune->Chord == 0)) { 96 pTune++; 97 pSong = pTune; 98 delay (beat-durMS); //After the completion of the note or chord, select the next note 99 100 //Serial.begin(9600); // Activate the Serial Monitor (commented out unless needed) 101 //int n = (beat-durMS); 102 //Serial.println (n) ; 103 } 104 } 105 void loop() // the loop routine runs over and over 106 { 107 if(pSong->Port == SONG_END) { 108 pSong = Elise; 109 delay(2000); 110 } 111 playTune(pSong); 112 } 113
Downloadable files
Control Panel Diagrams
Overview
Control Panel Diagrams

Wiring Chart of the Universal Shield
Overview of the point to point wiring for the Arduino shield
Wiring Chart of the Universal Shield

Circuit Diagram
Overview of the Control Panel and its Circuits
Circuit Diagram

Control Panel Diagrams
Overview
Control Panel Diagrams

Wiring Chart of the Universal Shield
Overview of the point to point wiring for the Arduino shield
Wiring Chart of the Universal Shield

Circuit Diagram
Overview of the Control Panel and its Circuits
Circuit Diagram

Comments
Only logged in users can leave comments