Components and supplies
1
Alligator Clips
1
9V Battery Clip
1
Speaker: 0.25W, 8 ohms
1
Battery, 9 V
1
Arduino Mega 2560
Apps and platforms
1
Arduino IDE
Project description
Code
also wire diagram
c_cpp
1ile:///Users/sam/Documents/Arduino/music_PROcreater/Fantastic%20Jofo-Crift.pd
code for ide
c_cpp
copy and put in ide
1/* 2 * Copyright 2018 Code and Make (codeandmake.com) 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * SOFTWARE. 21 */ 22 23/* 24 * A simple project that demonstrates how to make music using an Arduino. 25 * 26 * This code accompanies the following tutorial: https://youtu.be/Z1YvIFUIhLs 27 */ 28 29// Pin to which an 8 Ohm speaker is connected (use a 150 - 220 Ohm resistor) 30#define speakerPin 2 31 32// Tempo (beats per minute) 33#define bpm 96 34 35// Gap between notes. This is expressed as percentage of time between 2 beats. 36#define noteGapPercentage 10 37 38/* 39 * 2D array containing the notes to be played 40 * A note comprises of two values: 41 * * The first value determines the frequency of the note. 42 * This is expressed as the number of a key on an 88-key piano (1 - 88) 43 * A number outside this range can be used (e.g. 0) to create a gap 44 * * The second value determines the duration: 45 * * 1 represents a whole note (spans 4 beats) 46 * * 2 represents a half note (spans 2 beats) 47 * * 4 represents a quarter note (spans 1 beat) 48 * * 8 represents an eighth note (spans 0.5 beat) 49 * etc. 50 */ 51uint8_t notes[][2] = { 52 53 {35,12},{49,12},{53,12},{51,12},{0,12},{43,12},{35,12},{49,12}, 54 {53,12},{35,12},{49,12},{53,12},{51,12},{0,12},{43,12}, 55 {40,8},{30,8},{50,8},{20,8},{60,8},{40,8},{30,8},{50,8},{20,8},{60,8}, 56 {0,12},{43,12},{0,4} ,{35,12},{49,12},{53,12},{51,12},{0,12}, 57 {43,12},{0,10},{51,12},{0,12},{43,12},{0,10},{35,12},{49,12}, 58 {53,12},{51,12},{0,4},{35,12},{49,12},{53,12},{51,12},{0,12}, 59 {43,12},{0,5},{40,12},{35,12},{32,12},{30,8},{31,8},{32,8},{33,8},{34,8},{35,8},{36,8}, 60 {37,8},{38,8},{39,8},{40,8},{30,8},{50,8},{20,8},{60,8},{10,8}, 61 {30,8},{40,8},{50,8},{30,8},{35,8},{40,8},{60,8},{40,8}, {45,8}, 62 {35,12},{49,12},{53,12},{51,12},{0,12},{43,12},{35,12},{49,12}, 63 {53,12},{35,12},{49,12},{53,12},{40,8},{30,8},{50,8},{20,8},{60,8}, 64 {53,12},{35,12},{49,12},{53,12},{51,12},{0,12},{43,12},{51,12}, 65 {40,8},{30,8},{50,8},{20,8},{60,8},{40,8},{30,8},{50,8},{20,8},{60,8}, 66 {0,12},{43,12},{0,4} ,{35,12},{49,12},{53,12},{51,12},{0,12}, 67 {43,12},{0,10},{51,12},{0,12},{43,12},{0,10},{35,12},{49,12}, 68 {53,12}, {50,8},{40,8},{60,8},{30,8},{40,8},{30,8},{50,8},{20,8},{60,8}, 69 70 71}; 72 73// Time between two beats in microseconds (equal to length of a quarter note) 74#define beatDuration (80.0 / bpm) * 1000000L 75 76// Time of the gap between two notes in microseconds 77#define noteGap beatDuration * (noteGapPercentage / 1000.0) 78 79void setup() { 80 // Set the speakerPin as an output 81 pinMode(speakerPin, OUTPUT); 82 83 // Iterate over the notes array 84 for(int i = 0; i < (sizeof(notes) / sizeof(*notes)); i++) { 85 // pass the key number and note type 86 playNote(notes[i][0], notes[i][1]); 87 } 88} 89 90/* 91 * Plays an individual note. 92 * 93 * keyNumber - The key number (1 - 88) 94 * noteType - The note type (1, 2, 4, 8, etc.) 95 */ 96void playNote(uint8_t keyNumber, uint8_t noteType) { 97 long halfPeriod = getPeriodForKey(keyNumber) / 2; 98 long noteDuration = beatDuration * (4.0 / noteType); 99 long elapsed = 0; 100 101 // While we have a note to play 102 while(halfPeriod > 0 && elapsed < (noteDuration - noteGap)) { 103 // Set speakerPin high for half of the period 104 digitalWrite(speakerPin, HIGH); 105 wait(halfPeriod); 106 107 // Set speakerPin low for half of the period 108 digitalWrite(speakerPin, LOW); 109 wait(halfPeriod); 110 111 // Update the amount of time that has elapsed 112 elapsed += halfPeriod * 2; 113 } 114 115 /* 116 * Gap between notes. Calculated using 'elapsed' to minimise timing errors 117 * and ensure that the correct gap occurs whenever getPeriodForKey() returns 118 * zero. 119 */ 120 wait(noteDuration - elapsed); 121} 122 123/* 124 * Returns the period for a key or zero for key numbers outside the range of 1 - 125 * 88. 126 * 127 * keyNumber - The key number (1 - 88) 128 */ 129long getPeriodForKey(uint8_t keyNumber) { 130 // If the key is between 1 and 88 131 if(keyNumber >= 1 && keyNumber <= 88) { 132 // Return the period (one second divided by the frequency of the key) 133 return 1000000L / (pow(2.0, (keyNumber - 49.0) / 12.0) * 440.0); 134 } 135 136 // Otherwise return zero 137 return 0; 138} 139 140/* 141 * Delay for a number of microseconds. This is necessary because 142 * delayMicroseconds() has an upper limit. 143 * 144 * us - The delay in microseconds 145 */ 146void wait(long us) { 147 // First delay for the number of whole milliseconds using delay() 148 delay(us / 1000); 149 // Then delay for the remainder of microseconds using delayMicroseconds() 150 delayMicroseconds(us % 1000); 151} 152 153void loop() { 154 // Not used. Music will play once. 155 156 157 158 159 //#1 is A and A2 is A sharp so on till the eleventh note exsample:1down 2up or 1 white 2 black ect 160}
wire diagram
c_cpp
press start simulation and you can also see the code unde the code bar
1ttps://www.tinkercad.com/things/3G6pP9Z5Dkm-fantastic-jofo-crif
Comments
Only logged in users can leave comments