Components and supplies
1
Magnetic USB Cable
1
Passive buzzer
1
DFPlayer - A Mini MP3 Player
1
Micro SD Card Any size
1
Arduino Nano
1
jumper wires for arduino
1
Resistor 1K ohm
Tools and machines
1
Electric drill
1
Hot glue gun (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
V1.0
js
V1.0
1#include "pitches.h" 2#include <DFPlayerMini_Fast.h> 3#include <SoftwareSerial.h> 4#include <Keypad.h> 5 6const int buttonPin1 = A2; 7const int buttonPin2 = A4; 8int randomInt1; 9int randomInt2; 10int randomInt3; 11int randomInt4; 12int randomInt5; 13int randomInt6; 14int randomInt7; 15int randomInt8; 16int randomInt9; 17int randomIntFolder; 18int randomIntTrack; 19const byte ROWS = 4; //four rows 20const byte COLS = 3; //three columns 21char keys[ROWS][COLS] = { 22 {'4','5','6'}, 23 {'1','2','3'}, 24 {'*','0','#'}, 25 {'7','8','9'} 26}; 27 28byte rowPins[ROWS] = { 8,7,6,5 }; //connect to the row pinouts of the keypad 29byte colPins[COLS] = { 2,3,4, }; //connect to the column pinouts of the keypad 30 31Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 32 33#define RX_PIN 11 // RX-Pin des DFPlayer Mini 34#define TX_PIN 10 // TX-Pin des DFPlayer Mini 35 36SoftwareSerial mySerial(RX_PIN, TX_PIN); // SoftwareSerial-Objekt erstellen 37DFPlayerMini_Fast player; 38 39 40// notes in the melody: 41int melody[] = { 42 NOTE_E5, 0, NOTE_F5, NOTE_G5, 0, NOTE_C6,0, 43 NOTE_D5, 0, NOTE_E5, NOTE_F5, 0, 44 NOTE_G5, 0, NOTE_A5, NOTE_B5, 0, NOTE_F6,0, 45 NOTE_A5, 0, NOTE_B5, NOTE_C6, 0, NOTE_D6, 0, NOTE_E6, 0, 46 NOTE_E5, 0, NOTE_F5, NOTE_G5, 0, NOTE_C6,0, 47 NOTE_D6, 0, NOTE_E6, NOTE_F6, 0, 48 NOTE_G5, 0, NOTE_G5, NOTE_E6, 0, NOTE_D6, 0, 49 NOTE_G5, NOTE_E6, 0, NOTE_D6, 0, 50 NOTE_G5, NOTE_F6, 0, NOTE_E6, 0, 51 NOTE_D6, NOTE_C6, 0, 52}; 53 54// note durations: 4 = quarter note, 8 = eighth note, etc.: 55int noteDurations[] = { 56 8, 8, 8, 8, 8, 2, 4, 57 8, 8, 8, 2, 4, 58 8, 8, 8, 8, 8, 2, 4, 59 8, 8, 8, 4, 8, 4, 8, 4, 8, 60 8, 8, 8, 8, 8, 2, 4, 61 8, 8, 8, 2, 4, 62 8, 8, 8, 4, 8, 8, 8, 63 8, 4, 8, 8, 8, 64 8, 4, 8, 8, 8, 65 8, 2, 2, 66}; 67 68 69void setup() { 70 pinMode(buttonPin1, INPUT_PULLUP); 71 pinMode(buttonPin2, INPUT_PULLUP); 72 randomSeed(analogRead(0)); 73 Serial.begin(9600); 74 mySerial.begin(9600); // Starte die Kommunikation mit dem DFPlayer Mini 75 76 77if (!player.begin(mySerial)) { // Überprüfe, ob die Kommunikation erfolgreich ist 78 Serial.println(F("Fehler beim Initialisieren des DFPlayer Mini!")); 79 while (true); 80 } 81 Serial.println(F("DFPlayer Mini erfolgreich initialisiert.")); 82 player.volume(10); 83} 84 85void loop() { 86int buttonState1 = digitalRead(buttonPin1); 87int buttonState2 = digitalRead(buttonPin2); 88 89if (buttonState2 == 0){ 90delay(50); 91// spiele Indy 92 for (int thisNote = 0; thisNote <60; thisNote++) { 93 int noteDuration = 1000 / noteDurations[thisNote]; 94 tone(12, melody[thisNote], noteDuration); 95 int pauseBetweenNotes = noteDuration * 1.30; 96 delay(pauseBetweenNotes); 97 noTone(12); 98 } 99 delay(50); 100 if (buttonState1 == 0) { 101delay(50); 102randomIntFolder = random(1,9); 103randomIntTrack = random(1,5); 104Serial.println(randomIntFolder); 105Serial.println(randomIntTrack); 106delay(600); 107player.playFolder(randomIntFolder, randomIntTrack); 108delay(20000); 109if (buttonState1 == 1) { 110 player.stop(); 111} 112Serial.println(buttonState2); 113player.stop(); 114 } 115} 116 117 char key = keypad.getKey(); 118 119 switch(key) { 120 case '1': 121 Serial.println("Taste 1 wurde gedrückt."); 122 delay(50); 123 Serial.println(buttonState1); 124 if (buttonState1 == 0) { 125 randomInt1 = random(1, 5); 126 Serial.println("Random 1"); 127 Serial.println(randomInt1); 128 player.playFromMP3Folder(1); 129 delay(2000); 130 player.playFolder(1,randomInt1); 131 delay(2000); 132 } 133 break; 134 case '2': 135 Serial.println("Taste 2 wurde gedrückt."); 136 delay(50); 137 if (buttonState1 == 0) { 138 randomInt2 = random(1, 7); 139 Serial.println("Random 2"); 140 Serial.println(randomInt2); 141 player.playFromMP3Folder(2); 142 delay(2000); 143 player.playFolder(2,randomInt2); 144 delay(2000); 145 } 146 break; 147 case '3': 148 Serial.println("Taste 3 wurde gedrückt."); 149 delay(50); 150 if (buttonState1 == 0) { 151 randomInt3 = random(1, 8); 152 Serial.println("Random 3"); 153 Serial.println(randomInt3); 154 player.playFromMP3Folder(3); 155 delay(2000); 156 player.playFolder(3,randomInt3); 157 delay(2000); 158 } 159 break; 160 case '4': 161 Serial.println("Taste 4 wurde gedrückt."); 162 delay(50); 163 if (buttonState1 == 0) { 164 randomInt4 = random(1, 5); 165 Serial.println("Random 4"); 166 Serial.println(randomInt4); 167 player.playFromMP3Folder(4); 168 delay(2000); 169 player.playFolder(4,randomInt4); 170 delay(2000); 171 } 172 break; 173 case '5': 174 Serial.println("Taste 5 wurde gedrückt."); 175 delay(50); 176 if (buttonState1 == 0) { 177 randomInt5 = random(1, 3); 178 Serial.println("Random 5"); 179 Serial.println(randomInt5); 180 player.playFromMP3Folder(5); 181 delay(2000); 182 player.playFolder(5,randomInt5); 183 delay(2000); 184 } 185 break; 186 case '6': 187 Serial.println("Taste 6 wurde gedrückt."); 188 delay(50); 189 if (buttonState1 == 0) { 190 randomInt6 = random(1, 5); 191 Serial.println("Random 6"); 192 Serial.println(randomInt6); 193 player.playFromMP3Folder(6); 194 delay(2000); 195 player.playFolder(6,randomInt6); 196 delay(2000); 197 } 198 break; 199 case '7': 200 Serial.println("Taste 7 wurde gedrückt."); 201 delay(50); 202 if (buttonState1 == 0) { 203 randomInt7 = random(1, 6); 204 Serial.println("Random 7"); 205 Serial.println(randomInt7); 206 player.playFromMP3Folder(7); 207 delay(2000); 208 player.playFolder(7,randomInt7); 209 delay(2000); 210 } 211 break; 212 case '8': 213 Serial.println("Taste 8 wurde gedrückt."); 214 delay(50); 215 if (buttonState1 == 0) { 216 randomInt8 = random(1, 3); 217 Serial.println("Random 8"); 218 Serial.println(randomInt8); 219 player.playFromMP3Folder(8); 220 delay(2000); 221 player.playFolder(8,randomInt8); 222 delay(2000); 223 } 224 break; 225 case '9': 226 Serial.println("Taste 9 wurde gedrückt."); 227 delay(50); 228 if (buttonState1 == 0) { 229 randomInt1 = random(1, 6); 230 Serial.println("Random 9"); 231 Serial.println(randomInt9); 232 player.playFromMP3Folder(9); 233 delay(2000); 234 player.playFolder(9,randomInt9); 235 delay(2000); 236 } 237 break; 238 } 239 }
Comments
Only logged in users can leave comments