Components and supplies
Arduino Uno Rev3
PAM8403 amplifier module
Speaker 3W 4 ohms
LDR module
Laser Diode Red 650nm 5mW
KY-040 Rotary Encoder
16x2 LCD display with I²C interface
Tools and machines
jig saw
Drilling Machine
Soldering kit
Apps and platforms
Arduino IDE 1.8
Project description
Code
keyless musical instrument code
c
1//FOR LCD 2#include <LiquidCrystal_I2C.h> 3LiquidCrystal_I2C lcd(0x27,16,2); 4 5 //FOR ROTARY ENCODER 6 #define inputCLK 3 7 #define inputDT 4 8 int counter = 0; 9 int currentStateCLK; 10 int previousStateCLK; 11 12void setup() { 13 Serial.begin(9600); 14 //FOR LCD 15 lcd.init(); 16 lcd.clear(); 17 lcd.backlight(); 18 lcd.setCursor(1,0); 19 lcd.print("KEYLESS MUSIC"); 20 lcd.setCursor(3,1); 21 lcd.print("INSTRUMENT"); 22 delay(1000); 23 lcd.clear(); 24 lcd.setCursor(4,0); 25 lcd.print("MADE BY"); 26 lcd.setCursor(5,1); 27 lcd.print("ASBRO"); 28 delay(2000); 29 lcd.clear(); 30 lcd.setCursor(1,0); 31 lcd.print("MUSIC NOTE:"); 32 //LDR 33 pinMode(13, INPUT); 34 pinMode(12, INPUT); 35 pinMode(11, INPUT); 36 pinMode(10, INPUT); 37 pinMode(9, INPUT); 38 pinMode(8, INPUT); 39 pinMode(7, INPUT); 40 pinMode(6, INPUT); 41 //BUZZER 42 pinMode(5, OUTPUT); 43 //ROTARY ENCODER 44 pinMode (inputCLK,INPUT); 45 pinMode (inputDT,INPUT); 46 previousStateCLK = digitalRead(inputCLK); 47 48} 49 50void loop() { 51 52 53 currentStateCLK = digitalRead(inputCLK); 54 if (currentStateCLK != previousStateCLK){ 55 56 if (digitalRead(inputDT) != currentStateCLK) 57 counter --; 58 else 59 counter ++; 60 61 if(counter==0){ 62 lcd.setCursor(3,1); 63 lcd.print("OCTAVE 4"); 64 } 65 else if(counter==1){ 66 lcd.setCursor(3,1); 67 lcd.print("OCTAVE 5"); 68 } 69 else if(counter==2){ 70 lcd.setCursor(3,1); 71 lcd.print("OCTAVE 6"); 72 } 73 else if(counter==3){ 74 lcd.setCursor(3,1); 75 lcd.print("OCTAVE 7"); 76 } 77 else if(counter==-1){ 78 lcd.setCursor(3,1); 79 lcd.print("OCTAVE 3"); 80 } 81 else if(counter<=-2){ 82 lcd.setCursor(3,1); 83 lcd.print(" "); 84 } 85 else if(counter>=4){ 86 lcd.setCursor(3,1); 87 lcd.print(" "); 88 } 89 90 Serial.print(" -- Value: "); 91 Serial.println(counter); 92 } 93 previousStateCLK = currentStateCLK; 94 95 96 97 while(digitalRead(13)==HIGH){ 98 if(counter==0){ 99 lcd.setCursor(13,0); 100 lcd.print("C4"); 101 tone(5,262); 102 } 103 else if(counter==1) { 104 lcd.setCursor(13,0); 105 lcd.print("C5"); 106 tone(5,523); 107 } 108 else if(counter==2) { 109 lcd.setCursor(13,0); 110 lcd.print("C6"); 111 tone(5,1046); 112 } 113 else if(counter==3) { 114 lcd.setCursor(13,0); 115 lcd.print("C7"); 116 tone(5,2093); 117 } 118 else if(counter==-1) { 119 lcd.setCursor(13,0); 120 lcd.print("C3"); 121 tone(5,131); 122 } 123 } 124 while(digitalRead(7) == HIGH) { 125 if(counter==0) { 126 lcd.setCursor(13,0); 127 lcd.print("D4"); 128 tone(5,294); 129 } 130 else if(counter==1) { 131 lcd.setCursor(13,0); 132 lcd.print("D5"); 133 tone(5,587); 134 } 135 else if(counter==2) { 136 lcd.setCursor(13,0); 137 lcd.print("D6"); 138 tone(5,1175); 139 } 140 else if(counter==3) { 141 lcd.setCursor(13,0); 142 lcd.print("D7"); 143 tone(5,2349); 144 } 145 else if(counter==-1) { 146 lcd.setCursor(13,0); 147 lcd.print("D3"); 148 tone(5,147); 149 } 150 } 151 while(digitalRead(8) == HIGH) { 152 if(counter==0) { 153 lcd.setCursor(13,0); 154 lcd.print("E4"); 155 tone(5,330); 156 } 157 else if(counter==1) { 158 lcd.setCursor(13,0); 159 lcd.print("E5"); 160 tone(5,659); 161 } 162 else if(counter==2) { 163 lcd.setCursor(13,0); 164 lcd.print("E6"); 165 tone(5,1318); 166 } 167 else if(counter==3) { 168 lcd.setCursor(13,0); 169 lcd.print("E7"); 170 tone(5,2637); 171 } 172 else if(counter==-1) { 173 lcd.setCursor(13,0); 174 lcd.print("E3"); 175 tone(5,165); 176 } 177 } 178 while(digitalRead(9) == HIGH) { 179 if(counter==0) { 180 lcd.setCursor(13,0); 181 lcd.print("F4"); 182 tone(5,349); 183 } 184 else if(counter==1) { 185 lcd.setCursor(13,0); 186 lcd.print("F5"); 187 tone(5,698); 188 } 189 else if(counter==2) { 190 lcd.setCursor(13,0); 191 lcd.print("F6"); 192 tone(5,1397); 193 } 194 else if(counter==3) { 195 lcd.setCursor(13,0); 196 lcd.print("F7"); 197 tone(5,2793); 198 } 199 else if(counter==-1) { 200 lcd.setCursor(13,0); 201 lcd.print("F3"); 202 tone(5,175); 203 } 204 } 205 while(digitalRead(10) == HIGH) { 206 if(counter==0) { 207 lcd.setCursor(13,0); 208 lcd.print("G4"); 209 tone(5,392); 210 } 211 else if(counter==1) { 212 lcd.setCursor(13,0); 213 lcd.print("G5"); 214 tone(5,784); 215 } 216 else if(counter==2) { 217 lcd.setCursor(13,0); 218 lcd.print("G6"); 219 tone(5,1568); 220 } 221 else if(counter==3) { 222 lcd.setCursor(13,0); 223 lcd.print("G7"); 224 tone(5,3136); 225 } 226 else if(counter==-1) { 227 lcd.setCursor(13,0); 228 lcd.print("G3"); 229 tone(5,196); 230 } 231 } 232 while(digitalRead(11) == HIGH) { 233 if(counter==0) { 234 lcd.setCursor(13,0); 235 lcd.print("A4"); 236 tone(5,440); 237 } 238 else if(counter==1) { 239 lcd.setCursor(13,0); 240 lcd.print("A5"); 241 tone(5,880); 242 } 243 else if(counter==2) { 244 lcd.setCursor(13,0); 245 lcd.print("A6"); 246 tone(5,1760); 247 } 248 else if(counter==3) { 249 lcd.setCursor(13,0); 250 lcd.print("A7"); 251 tone(5,3520); 252 } 253 else if(counter==-1) { 254 lcd.setCursor(13,0); 255 lcd.print("A3"); 256 tone(5,220); 257 } 258 } 259 while(digitalRead(12) == HIGH) { 260 if(counter==0) { 261 lcd.setCursor(13,0); 262 lcd.print("B4"); 263 tone(5,494); 264 } 265 else if(counter==1) { 266 lcd.setCursor(13,0); 267 lcd.print("B5"); 268 tone(5,988); 269 } 270 else if(counter==2) { 271 lcd.setCursor(13,0); 272 lcd.print("B6"); 273 tone(5,1975); 274 } 275 else if(counter==3) { 276 lcd.setCursor(13,0); 277 lcd.print("B7"); 278 tone(5,3951); 279 } 280 else if(counter==-1) { 281 lcd.setCursor(13,0); 282 lcd.print("B3"); 283 tone(5,247); 284 } 285 } 286 while(digitalRead(6) == HIGH) { 287 if(counter==0) { 288 lcd.setCursor(13,0); 289 lcd.print("C5"); 290 tone(5,523); 291 } 292 else if(counter==1) { 293 lcd.setCursor(13,0); 294 lcd.print("C6"); 295 tone(5,1046); 296 } 297 else if(counter==2) { 298 lcd.setCursor(13,0); 299 lcd.print("C7"); 300 tone(5,2093); 301 } 302 else if(counter==3) { 303 lcd.setCursor(13,0); 304 lcd.print("C8"); 305 tone(5,4186); 306 } 307 else if(counter==-1) { 308 lcd.setCursor(13,0); 309 lcd.print("C4"); 310 tone(5,262); 311 } 312 } 313 noTone(5); 314 315}
Downloadable files
Circuit diagram
circuit.jpg
Comments
Only logged in users can leave comments