Music channel /radio without electromagnetic wave
This is a music box offering 10 songs/channels to choose from.
Devices & Components
1
Arduino Uno Rev3
1
Piezo Buzzer
1
10kOhm potentiometer
Hardware & Tools
1
Breadboard, Solderless
Software & Tools
Arduino IDE
Project description
Code
music channel
cpp
1char noteNames[]={'C','D','E','F','G','a','b'}; 2unsigned int frequencies[]={262,294,330,349,392,440,494}; 3const byte notecount=sizeof(noteNames); 4const int speakerpin=9; 5const int channelpin=A0; 6char score_1[]="CCGGaaGFFEEDDC GGFFEEDGGFFEED CCGGaaGFFEEDDC";//"Twinkle, twinkle little star" 7const byte scorelen_1=sizeof(score_1); 8char score_2[]="EDCDEEEDDDEGG EDCDEEEEDDEDC";//"mary had a little lamb" 9const byte scorelen_2=sizeof(score_2); 10char score_3[]="CEFGCEFGCEFGECED EEDCCEGGGFEFGECDC";//"When The Saints Go Marching" 11const byte scorelen_3=sizeof(score_3); 12char score_4[]="GCDEEEDECCCDEFaaGFE CDEFaaGFECCDEFDDEC";//"You Are My Sunshine" 13const byte scorelen_4=sizeof(score_4); 14char score_5[]="GCDECEDCDGbDFDbCDC GCDECEDCDGbDFDbCDC ";//"Down In The Valley" 15const byte scorelen_5=sizeof(score_5); 16char score_6[]="CDECCDECEFGEFG GaGFECGaGFECCGCCGC";//"Frere Jacques" 17const byte scorelen_6=sizeof(score_6); 18char score_7[]="GCCDEEDCaGGCbCDEFD DEFGFEDCEDCaGGaCC";//"The Water Is Wide " 19const byte scorelen_7=sizeof(score_7); 20char score_8[]="GCECEDCaGGCECEDG EGEGECGaCCaG GCECEDC";//"amazing Grace" 21const byte scorelen_8=sizeof(score_8); 22char score_9[]="CCDECEDCCDECb CCDEFEDCbGabCC abaGabCGaGFEG abaGabCaGCbDCC";//"Yankee Doodle" 23const byte scorelen_9=sizeof(score_9); 24char score_10[]="EEGEEGEGCbaaG DEFDDEFDFbaGbC CCaFGECFGFEDC";//"Brahms Lullaby" 25const byte scorelen_10=sizeof(score_10); 26int duration=333; 27int channel=0; 28int oldchannel=0; 29 30 31 32 33void setup() { 34 // put your setup code here, to run once: 35pinMode (speakerpin, OUTPUT); 36pinMode (channelpin, INPUT); 37Serial.begin(9600); 38} 39 40void loop() { 41 // put your main code here, to run repeatedly: 42int potreading=analogRead(channelpin); 43if (potreading>900) 44 { 45 channel=10; 46 } 47 else if (potreading>800) 48 { 49 channel=9; 50 } 51 else if (potreading>700) 52 { 53 channel= 8 ; 54 } 55 else if (potreading>600) 56 { 57 channel=7; 58 } 59 else if (potreading>500) 60 { 61 channel=6; 62 } 63 else if (potreading>400) 64 { 65 channel= 5; 66 } 67 else if (potreading>300) 68 { 69 channel=4; 70 } 71 else if (potreading>200) 72 { 73 channel=3; 74 } 75 else if (potreading>100) 76 { 77 channel=2; 78 } 79 else if (potreading>0) 80 { 81 channel=1; 82 } 83 else 84 {channel=0;} 85 86 87Serial.println(channel); 88if (channel==1){ 89 for (int i=0;i< scorelen_1; i++) 90 { 91 //int duration=333; 92 93 playnote(score_1[i],duration); 94 } 95} 96 97 else if (channel==2) 98 { 99 for (int i=0;i< scorelen_2; i++) 100 { 101 //int duration=333; 102 103 playnote(score_2[i],duration); 104 } 105 } 106 else if (channel==3) 107 { 108 for (int i=0;i< scorelen_3; i++) 109 { 110 //int duration=333; 111 112 playnote(score_3[i],duration); 113 } 114 } 115 else if (channel==4) 116 { 117 for (int i=0;i< scorelen_4; i++) 118 119 //int duration=333; 120 121 playnote(score_4[i],duration); 122 } 123 else if (channel==5) 124 { 125 for (int i=0;i< scorelen_5; i++) 126 { 127 //int duration=333; 128 129 playnote(score_5[i],duration); 130 } 131 } 132 else if (channel==6) 133 { 134 for (int i=0;i< scorelen_6; i++) 135 { 136 //int duration=333; 137 138 playnote(score_6[i],duration); 139 } 140 } 141 else if (channel==7) 142 { 143 for (int i=0;i< scorelen_2; i++) 144 { 145 //int duration=333; 146 147 playnote(score_2[i],duration); 148 } 149 } 150 else if (channel==8) 151 { 152 for (int i=0;i< scorelen_8; i++) 153 { 154 //int duration=333; 155 156 playnote(score_8[i],duration); 157 } 158 } 159 else if (channel==9) 160 { 161 for (int i=0;i< scorelen_9; i++) 162 { 163 //int duration=333; 164 165 playnote(score_9[i],duration); 166 } 167 } 168 else if (channel==10) 169 { 170 for (int i=0;i< scorelen_10; i++) 171 { 172 //int duration=333; 173 174 playnote(score_10[i],duration); 175 } 176 } 177 178 179 delay(4000); 180 181 } 182void playnote (char note, int duration) 183{ 184for (int i =0; i< notecount; i++) 185{ 186 if (noteNames[i]==note) 187 tone(speakerpin,frequencies[i],duration); 188 189} 190 191delay(duration); 192 193 194}
Documentation
the circuit
the circuit.rtf
Comments
Only logged in users can leave comments