Components and supplies
Alphanumeric LCD, 20 x 4
YX6300 UART TTL serial MP3 music player module
Jumper wires (generic)
Connector Kit, Wire To Wire
Arduino Nano R3
SparkFun Solder-able Breadboard - Mini
Micro SD TF Card Module SPI For Arduino
Sensor Shield V5 Arduino Uno
Flash Memory Card, SD Card
Sensor Shield Arduino Nano
MB-102 MB102 power module
Arduino UNO
Tools and machines
3D Printer (generic)
Soldering iron (generic)
Laser cutter (generic)
Solder Wire, Lead Free
Project description
Code
MP3
arduino
1 2// COPYIED FROM: https://create.arduino.cc/projecthub/moreirarobotics/how-to-use-the-yx5300-mp3-module-catalex-with-arduino-171a23 3// Edited by Dror Moshe Aharoni 4 5// pins connection: 7-12 - music buttons, 5 to mp3 TX, 6 to MP3 RX 6#include <SoftwareSerial.h> 7 8#define ARDUINO_RX 5 //should connect to TX of the Serial MP3 Player module 9#define ARDUINO_TX 6 //connect to RX of the module 10 11SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); 12//#define mp3 Serial3 // Connect the MP3 Serial Player to the Arduino MEGA Serial3 (14 TX3 -> RX, 15 RX3 -> TX) 13 14static int8_t Send_buf[8] = {0}; // Buffer for Send commands. // BETTER LOCALLY 15static uint8_t ansbuf[10] = {0}; // Buffer for the answers. // BETTER LOCALLY 16 17String mp3Answer; // Answer from the MP3. 18 19String sanswer(void); 20String sbyte2hex(uint8_t b); 21 22int BUTTON_PIN; 23int last_pushed; 24int buttonState; 25int currentSong = 1; 26int pause = 0; 27 28/************ Command byte **************************/ 29#define CMD_NEXT_SONG 0X01 // Play next song. 30#define CMD_PREV_SONG 0X02 // Play previous song. 31#define CMD_PLAY_W_INDEX 0X03 32#define CMD_VOLUME_UP 0X04 33#define CMD_VOLUME_DOWN 0X05 34#define CMD_SET_VOLUME 0X06 35 36#define CMD_SNG_CYCL_PLAY 0X08 // Single Cycle Play. 37#define CMD_SEL_DEV 0X09 38#define CMD_SLEEP_MODE 0X0A 39#define CMD_WAKE_UP 0X0B 40#define CMD_RESET 0X0C 41#define CMD_PLAY 0X0D 42#define CMD_PAUSE 0X0E 43#define CMD_PLAY_FOLDER_FILE 0X0F 44 45#define CMD_STOP_PLAY 0X16 46#define CMD_FOLDER_CYCLE 0X17 47#define CMD_SHUFFLE_PLAY 0x18 // 48#define CMD_SET_SNGL_CYCL 0X19 // Set single cycle. 49 50#define CMD_SET_DAC 0X1A 51#define DAC_ON 0X00 52#define DAC_OFF 0X01 53 54#define CMD_PLAY_W_VOL 0X22 55#define CMD_PLAYING_N 0x4C 56#define CMD_QUERY_STATUS 0x42 57#define CMD_QUERY_VOLUME 0x43 58#define CMD_QUERY_FLDR_TRACKS 0x4e 59#define CMD_QUERY_TOT_TRACKS 0x48 60#define CMD_QUERY_FLDR_COUNT 0x4f 61 62/************ Opitons **************************/ 63#define DEV_TF 0X02 64 65/*********************************************************************/ 66 67 68void setup() 69{ 70 // starting the serial communication through the mp3 object 71 // The mp3 module is controlled through commands received by the Arduino serial. In this process, we used the SoftwareSerial library and emulated a serial on the Arduino digital pins. 72 // Thus, you will be able to use the Arduino to control the MP3 module through commands sent to it. 73 Serial.begin(9600); 74 mp3.begin(9600); 75 delay(500); 76 // initialization of the MP3 Card module 77 sendCommand(CMD_SEL_DEV, 0, DEV_TF); 78 delay(500); 79 80 // initialize the pushbutton pins as an pull-up input 81 // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. 82 for (BUTTON_PIN = 7; BUTTON_PIN < 13; BUTTON_PIN++){ 83 //BUTTON_PIN is the number of the pushbutton pin 84 pinMode(BUTTON_PIN, INPUT_PULLUP); 85 } 86 sendCommand(0x06, 0 , 30); 87 Serial.println("playing the greetings sound"); 88 sendCommand(0x03, 0 , 1); 89 //delay(19000); 90 delay(1000); 91 last_pushed = 1; 92} 93 94//starting loop 95void loop() { 96 //Serial.println("start loop"); 97 buttonState = HIGH; 98 BUTTON_PIN = 6; 99 // read the state of the switch/button: 100 while (buttonState == HIGH){ 101 BUTTON_PIN++; 102 if (BUTTON_PIN == 13) BUTTON_PIN = 7; 103 buttonState = digitalRead(BUTTON_PIN); 104 // print out the button's state 105 //Serial.println(BUTTON_PIN); 106 //Serial.println(buttonState); 107 } 108 109 //Serial.println("DM pushed button:"); 110 //Serial.println(BUTTON_PIN); 111 if (BUTTON_PIN == last_pushed){ 112 switch (pause) { 113 case 0: 114 sendCommand(0x0E); 115 last_pushed = BUTTON_PIN; 116 pause = 1; 117 sanswer(); 118 break; 119 case 1: 120 pause = 0; 121 sendCommand(0x03, 0, BUTTON_PIN-5); 122 if (mp3.available()) Serial.println(decodeMP3Answer()); 123 break; 124 } 125 126 } 127 128 else 129 { 130 // saving the last pushed button in order to enable music pause 131 last_pushed = BUTTON_PIN; 132 133 // playing the desired music file 134 sendCommand(0x03, 0, BUTTON_PIN-5); 135 //delay(3000); 136 137 // Check for the answer. 138 if (mp3.available()) Serial.println(decodeMP3Answer()); 139 } 140 141} 142 143 144 145/********************************************************************************/ 146/*Function sendMP3Command: seek for a 'c' command and send it to MP3 */ 147/*Parameter: c. Code for the MP3 Command, 'h' for help. */ 148/*Return: void */ 149 150void sendMP3Command(char c) { 151 152 switch (c) { 153 case '?': 154 case 'h': 155 Serial.println("HELP "); 156 Serial.println(" p = Play"); 157 Serial.println(" P = Pause"); 158 Serial.println(" > = Next"); 159 Serial.println(" < = Previous"); 160 Serial.println(" s = Stop Play"); 161 Serial.println(" + = Volume UP"); 162 Serial.println(" - = Volume DOWN"); 163 Serial.println(" c = Query current file"); 164 Serial.println(" q = Query status"); 165 Serial.println(" v = Query volume"); 166 Serial.println(" x = Query folder count"); 167 Serial.println(" t = Query total file count"); 168 Serial.println(" f = Play folder 1."); 169 Serial.println(" S = Sleep"); 170 Serial.println(" W = Wake up"); 171 Serial.println(" r = Reset"); 172 break; 173 174 case 'p': 175 Serial.println("Play "); 176 sendCommand(CMD_PLAY); 177 break; 178 179 case 'P': 180 Serial.println("Pause"); 181 sendCommand(CMD_PAUSE); 182 break; 183 184 case '>': 185 Serial.println("Next"); 186 sendCommand(CMD_NEXT_SONG); 187 sendCommand(CMD_PLAYING_N); // ask for the number of file is playing 188 break; 189 190 case '<': 191 Serial.println("Previous"); 192 sendCommand(CMD_PREV_SONG); 193 sendCommand(CMD_PLAYING_N); // ask for the number of file is playing 194 break; 195 196 case 's': 197 Serial.println("Stop Play"); 198 sendCommand(CMD_STOP_PLAY); 199 break; 200 201 case '+': 202 Serial.println("Volume Up"); 203 sendCommand(CMD_VOLUME_UP); 204 break; 205 206 case '-': 207 Serial.println("Volume Down"); 208 sendCommand(CMD_VOLUME_DOWN); 209 break; 210 211 case 'c': 212 Serial.println("Query current file"); 213 sendCommand(CMD_PLAYING_N); 214 break; 215 216 case 'q': 217 Serial.println("Query status"); 218 sendCommand(CMD_QUERY_STATUS); 219 break; 220 221 case 'v': 222 Serial.println("Query volume"); 223 sendCommand(CMD_QUERY_VOLUME); 224 break; 225 226 case 'x': 227 Serial.println("Query folder count"); 228 sendCommand(CMD_QUERY_FLDR_COUNT); 229 break; 230 231 case 't': 232 Serial.println("Query total file count"); 233 sendCommand(CMD_QUERY_TOT_TRACKS); 234 break; 235 236 case 'f': 237 Serial.println("Playing folder 1"); 238 sendCommand(CMD_FOLDER_CYCLE, 1, 0); 239 break; 240 241 case 'S': 242 Serial.println("Sleep"); 243 sendCommand(CMD_SLEEP_MODE); 244 break; 245 246 case 'W': 247 Serial.println("Wake up"); 248 sendCommand(CMD_WAKE_UP); 249 break; 250 251 case 'r': 252 Serial.println("Reset"); 253 sendCommand(CMD_RESET); 254 break; 255 } 256} 257 258/********************************************************************************/ 259/*Function decodeMP3Answer: Decode MP3 answer. */ 260/*Parameter:-void */ 261/*Return: The */ 262 263String decodeMP3Answer() { 264 String decodedMP3Answer = ""; 265 266 decodedMP3Answer += sanswer(); 267 268 switch (ansbuf[3]) { 269 case 0x3A: 270 decodedMP3Answer += " -> Memory card inserted."; 271 break; 272 273 case 0x3D: 274 decodedMP3Answer += " -> Completed play num " + String(ansbuf[6], DEC); 275 //sendCommand(CMD_NEXT_SONG); 276 //sendCommand(CMD_PLAYING_N); // ask for the number of file is playing 277 break; 278 279 case 0x40: 280 decodedMP3Answer += " -> Error"; 281 break; 282 283 case 0x41: 284 decodedMP3Answer += " -> Data recived correctly. "; 285 break; 286 287 case 0x42: 288 decodedMP3Answer += " -> Status playing: " + String(ansbuf[6], DEC); 289 break; 290 291 case 0x48: 292 decodedMP3Answer += " -> File count: " + String(ansbuf[6], DEC); 293 break; 294 295 case 0x4C: 296 decodedMP3Answer += " -> Playing: " + String(ansbuf[6], DEC); 297 break; 298 299 case 0x4E: 300 decodedMP3Answer += " -> Folder file count: " + String(ansbuf[6], DEC); 301 break; 302 303 case 0x4F: 304 decodedMP3Answer += " -> Folder count: " + String(ansbuf[6], DEC); 305 break; 306 } 307 308 return decodedMP3Answer; 309} 310 311/********************************************************************************/ 312/*Function: Send command to the MP3 */ 313/*Parameter: byte command */ 314/*Parameter: byte dat1 parameter for the command */ 315/*Parameter: byte dat2 parameter for the command */ 316 317void sendCommand(byte command){ 318 sendCommand(command, 0, 0); 319} 320 321void sendCommand(byte command, byte dat1, byte dat2){ 322 delay(20); 323 Send_buf[0] = 0x7E; // 324 Send_buf[1] = 0xFF; // 325 Send_buf[2] = 0x06; // Len 326 Send_buf[3] = command; // 327 Send_buf[4] = 0x01; // 0x00 NO, 0x01 feedback 328 Send_buf[5] = dat1; // datah 329 Send_buf[6] = dat2; // datal 330 Send_buf[7] = 0xEF; // 331 Serial.print("Sending: "); 332 for (uint8_t i = 0; i < 8; i++) 333 { 334 mp3.write(Send_buf[i]) ; 335 Serial.print(sbyte2hex(Send_buf[i])); 336 } 337 Serial.println(); 338} 339 340 341 342/********************************************************************************/ 343/*Function: sbyte2hex. Returns a byte data in HEX format. */ 344/*Parameter:- uint8_t b. Byte to convert to HEX. */ 345/*Return: String */ 346 347 348String sbyte2hex(uint8_t b) 349{ 350 String shex; 351 352 shex = "0X"; 353 354 if (b < 16) shex += "0"; 355 shex += String(b, HEX); 356 shex += " "; 357 return shex; 358} 359 360 361/********************************************************************************/ 362/*Function: shex2int. Returns a int from an HEX string. */ 363/*Parameter: s. char *s to convert to HEX. */ 364/*Parameter: n. char *s' length. */ 365/*Return: int */ 366 367int shex2int(char *s, int n){ 368 int r = 0; 369 for (int i=0; i<n; i++){ 370 if(s[i]>='0' && s[i]<='9'){ 371 r *= 16; 372 r +=s[i]-'0'; 373 }else if(s[i]>='A' && s[i]<='F'){ 374 r *= 16; 375 r += (s[i] - 'A') + 10; 376 } 377 } 378 return r; 379} 380 381 382/********************************************************************************/ 383/*Function: sanswer. Returns a String answer from mp3 UART module. */ 384/*Parameter:- uint8_t b. void. */ 385/*Return: String. If the answer is well formated answer. */ 386 387String sanswer(void) 388{ 389 uint8_t i = 0; 390 String mp3answer = ""; 391 392 // Get only 10 Bytes 393 while (mp3.available() && (i < 10)) 394 { 395 uint8_t b = mp3.read(); 396 ansbuf[i] = b; 397 i++; 398 399 mp3answer += sbyte2hex(b); 400 } 401 402 // if the answer format is correct. 403 if ((ansbuf[0] == 0x7E) && (ansbuf[9] == 0xEF)) 404 { 405 return mp3answer; 406 } 407 408 return "???: " + mp3answer; 409} 410
MP3
arduino
1 2// COPYIED FROM: https://create.arduino.cc/projecthub/moreirarobotics/how-to-use-the-yx5300-mp3-module-catalex-with-arduino-171a23 3// Edited by Dror Moshe Aharoni 4 5// pins connection: 7-12 - music buttons, 5 to mp3 TX, 6 to MP3 RX 6#include <SoftwareSerial.h> 7 8#define ARDUINO_RX 5 //should connect to TX of the Serial MP3 Player module 9#define ARDUINO_TX 6 //connect to RX of the module 10 11SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX); 12//#define mp3 Serial3 // Connect the MP3 Serial Player to the Arduino MEGA Serial3 (14 TX3 -> RX, 15 RX3 -> TX) 13 14static int8_t Send_buf[8] = {0}; // Buffer for Send commands. // BETTER LOCALLY 15static uint8_t ansbuf[10] = {0}; // Buffer for the answers. // BETTER LOCALLY 16 17String mp3Answer; // Answer from the MP3. 18 19String sanswer(void); 20String sbyte2hex(uint8_t b); 21 22int BUTTON_PIN; 23int last_pushed; 24int buttonState; 25int currentSong = 1; 26int pause = 0; 27 28/************ Command byte **************************/ 29#define CMD_NEXT_SONG 0X01 // Play next song. 30#define CMD_PREV_SONG 0X02 // Play previous song. 31#define CMD_PLAY_W_INDEX 0X03 32#define CMD_VOLUME_UP 0X04 33#define CMD_VOLUME_DOWN 0X05 34#define CMD_SET_VOLUME 0X06 35 36#define CMD_SNG_CYCL_PLAY 0X08 // Single Cycle Play. 37#define CMD_SEL_DEV 0X09 38#define CMD_SLEEP_MODE 0X0A 39#define CMD_WAKE_UP 0X0B 40#define CMD_RESET 0X0C 41#define CMD_PLAY 0X0D 42#define CMD_PAUSE 0X0E 43#define CMD_PLAY_FOLDER_FILE 0X0F 44 45#define CMD_STOP_PLAY 0X16 46#define CMD_FOLDER_CYCLE 0X17 47#define CMD_SHUFFLE_PLAY 0x18 // 48#define CMD_SET_SNGL_CYCL 0X19 // Set single cycle. 49 50#define CMD_SET_DAC 0X1A 51#define DAC_ON 0X00 52#define DAC_OFF 0X01 53 54#define CMD_PLAY_W_VOL 0X22 55#define CMD_PLAYING_N 0x4C 56#define CMD_QUERY_STATUS 0x42 57#define CMD_QUERY_VOLUME 0x43 58#define CMD_QUERY_FLDR_TRACKS 0x4e 59#define CMD_QUERY_TOT_TRACKS 0x48 60#define CMD_QUERY_FLDR_COUNT 0x4f 61 62/************ Opitons **************************/ 63#define DEV_TF 0X02 64 65/*********************************************************************/ 66 67 68void setup() 69{ 70 // starting the serial communication through the mp3 object 71 // The mp3 module is controlled through commands received by the Arduino serial. In this process, we used the SoftwareSerial library and emulated a serial on the Arduino digital pins. 72 // Thus, you will be able to use the Arduino to control the MP3 module through commands sent to it. 73 Serial.begin(9600); 74 mp3.begin(9600); 75 delay(500); 76 // initialization of the MP3 Card module 77 sendCommand(CMD_SEL_DEV, 0, DEV_TF); 78 delay(500); 79 80 // initialize the pushbutton pins as an pull-up input 81 // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. 82 for (BUTTON_PIN = 7; BUTTON_PIN < 13; BUTTON_PIN++){ 83 //BUTTON_PIN is the number of the pushbutton pin 84 pinMode(BUTTON_PIN, INPUT_PULLUP); 85 } 86 sendCommand(0x06, 0 , 30); 87 Serial.println("playing the greetings sound"); 88 sendCommand(0x03, 0 , 1); 89 //delay(19000); 90 delay(1000); 91 last_pushed = 1; 92} 93 94//starting loop 95void loop() { 96 //Serial.println("start loop"); 97 buttonState = HIGH; 98 BUTTON_PIN = 6; 99 // read the state of the switch/button: 100 while (buttonState == HIGH){ 101 BUTTON_PIN++; 102 if (BUTTON_PIN == 13) BUTTON_PIN = 7; 103 buttonState = digitalRead(BUTTON_PIN); 104 // print out the button's state 105 //Serial.println(BUTTON_PIN); 106 //Serial.println(buttonState); 107 } 108 109 //Serial.println("DM pushed button:"); 110 //Serial.println(BUTTON_PIN); 111 if (BUTTON_PIN == last_pushed){ 112 switch (pause) { 113 case 0: 114 sendCommand(0x0E); 115 last_pushed = BUTTON_PIN; 116 pause = 1; 117 sanswer(); 118 break; 119 case 1: 120 pause = 0; 121 sendCommand(0x03, 0, BUTTON_PIN-5); 122 if (mp3.available()) Serial.println(decodeMP3Answer()); 123 break; 124 } 125 126 } 127 128 else 129 { 130 // saving the last pushed button in order to enable music pause 131 last_pushed = BUTTON_PIN; 132 133 // playing the desired music file 134 sendCommand(0x03, 0, BUTTON_PIN-5); 135 //delay(3000); 136 137 // Check for the answer. 138 if (mp3.available()) Serial.println(decodeMP3Answer()); 139 } 140 141} 142 143 144 145/********************************************************************************/ 146/*Function sendMP3Command: seek for a 'c' command and send it to MP3 */ 147/*Parameter: c. Code for the MP3 Command, 'h' for help. */ 148/*Return: void */ 149 150void sendMP3Command(char c) { 151 152 switch (c) { 153 case '?': 154 case 'h': 155 Serial.println("HELP "); 156 Serial.println(" p = Play"); 157 Serial.println(" P = Pause"); 158 Serial.println(" > = Next"); 159 Serial.println(" < = Previous"); 160 Serial.println(" s = Stop Play"); 161 Serial.println(" + = Volume UP"); 162 Serial.println(" - = Volume DOWN"); 163 Serial.println(" c = Query current file"); 164 Serial.println(" q = Query status"); 165 Serial.println(" v = Query volume"); 166 Serial.println(" x = Query folder count"); 167 Serial.println(" t = Query total file count"); 168 Serial.println(" f = Play folder 1."); 169 Serial.println(" S = Sleep"); 170 Serial.println(" W = Wake up"); 171 Serial.println(" r = Reset"); 172 break; 173 174 case 'p': 175 Serial.println("Play "); 176 sendCommand(CMD_PLAY); 177 break; 178 179 case 'P': 180 Serial.println("Pause"); 181 sendCommand(CMD_PAUSE); 182 break; 183 184 case '>': 185 Serial.println("Next"); 186 sendCommand(CMD_NEXT_SONG); 187 sendCommand(CMD_PLAYING_N); // ask for the number of file is playing 188 break; 189 190 case '<': 191 Serial.println("Previous"); 192 sendCommand(CMD_PREV_SONG); 193 sendCommand(CMD_PLAYING_N); // ask for the number of file is playing 194 break; 195 196 case 's': 197 Serial.println("Stop Play"); 198 sendCommand(CMD_STOP_PLAY); 199 break; 200 201 case '+': 202 Serial.println("Volume Up"); 203 sendCommand(CMD_VOLUME_UP); 204 break; 205 206 case '-': 207 Serial.println("Volume Down"); 208 sendCommand(CMD_VOLUME_DOWN); 209 break; 210 211 case 'c': 212 Serial.println("Query current file"); 213 sendCommand(CMD_PLAYING_N); 214 break; 215 216 case 'q': 217 Serial.println("Query status"); 218 sendCommand(CMD_QUERY_STATUS); 219 break; 220 221 case 'v': 222 Serial.println("Query volume"); 223 sendCommand(CMD_QUERY_VOLUME); 224 break; 225 226 case 'x': 227 Serial.println("Query folder count"); 228 sendCommand(CMD_QUERY_FLDR_COUNT); 229 break; 230 231 case 't': 232 Serial.println("Query total file count"); 233 sendCommand(CMD_QUERY_TOT_TRACKS); 234 break; 235 236 case 'f': 237 Serial.println("Playing folder 1"); 238 sendCommand(CMD_FOLDER_CYCLE, 1, 0); 239 break; 240 241 case 'S': 242 Serial.println("Sleep"); 243 sendCommand(CMD_SLEEP_MODE); 244 break; 245 246 case 'W': 247 Serial.println("Wake up"); 248 sendCommand(CMD_WAKE_UP); 249 break; 250 251 case 'r': 252 Serial.println("Reset"); 253 sendCommand(CMD_RESET); 254 break; 255 } 256} 257 258/********************************************************************************/ 259/*Function decodeMP3Answer: Decode MP3 answer. */ 260/*Parameter:-void */ 261/*Return: The */ 262 263String decodeMP3Answer() { 264 String decodedMP3Answer = ""; 265 266 decodedMP3Answer += sanswer(); 267 268 switch (ansbuf[3]) { 269 case 0x3A: 270 decodedMP3Answer += " -> Memory card inserted."; 271 break; 272 273 case 0x3D: 274 decodedMP3Answer += " -> Completed play num " + String(ansbuf[6], DEC); 275 //sendCommand(CMD_NEXT_SONG); 276 //sendCommand(CMD_PLAYING_N); // ask for the number of file is playing 277 break; 278 279 case 0x40: 280 decodedMP3Answer += " -> Error"; 281 break; 282 283 case 0x41: 284 decodedMP3Answer += " -> Data recived correctly. "; 285 break; 286 287 case 0x42: 288 decodedMP3Answer += " -> Status playing: " + String(ansbuf[6], DEC); 289 break; 290 291 case 0x48: 292 decodedMP3Answer += " -> File count: " + String(ansbuf[6], DEC); 293 break; 294 295 case 0x4C: 296 decodedMP3Answer += " -> Playing: " + String(ansbuf[6], DEC); 297 break; 298 299 case 0x4E: 300 decodedMP3Answer += " -> Folder file count: " + String(ansbuf[6], DEC); 301 break; 302 303 case 0x4F: 304 decodedMP3Answer += " -> Folder count: " + String(ansbuf[6], DEC); 305 break; 306 } 307 308 return decodedMP3Answer; 309} 310 311/********************************************************************************/ 312/*Function: Send command to the MP3 */ 313/*Parameter: byte command */ 314/*Parameter: byte dat1 parameter for the command */ 315/*Parameter: byte dat2 parameter for the command */ 316 317void sendCommand(byte command){ 318 sendCommand(command, 0, 0); 319} 320 321void sendCommand(byte command, byte dat1, byte dat2){ 322 delay(20); 323 Send_buf[0] = 0x7E; // 324 Send_buf[1] = 0xFF; // 325 Send_buf[2] = 0x06; // Len 326 Send_buf[3] = command; // 327 Send_buf[4] = 0x01; // 0x00 NO, 0x01 feedback 328 Send_buf[5] = dat1; // datah 329 Send_buf[6] = dat2; // datal 330 Send_buf[7] = 0xEF; // 331 Serial.print("Sending: "); 332 for (uint8_t i = 0; i < 8; i++) 333 { 334 mp3.write(Send_buf[i]) ; 335 Serial.print(sbyte2hex(Send_buf[i])); 336 } 337 Serial.println(); 338} 339 340 341 342/********************************************************************************/ 343/*Function: sbyte2hex. Returns a byte data in HEX format. */ 344/*Parameter:- uint8_t b. Byte to convert to HEX. */ 345/*Return: String */ 346 347 348String sbyte2hex(uint8_t b) 349{ 350 String shex; 351 352 shex = "0X"; 353 354 if (b < 16) shex += "0"; 355 shex += String(b, HEX); 356 shex += " "; 357 return shex; 358} 359 360 361/********************************************************************************/ 362/*Function: shex2int. Returns a int from an HEX string. */ 363/*Parameter: s. char *s to convert to HEX. */ 364/*Parameter: n. char *s' length. */ 365/*Return: int */ 366 367int shex2int(char *s, int n){ 368 int r = 0; 369 for (int i=0; i<n; i++){ 370 if(s[i]>='0' && s[i]<='9'){ 371 r *= 16; 372 r +=s[i]-'0'; 373 }else if(s[i]>='A' && s[i]<='F'){ 374 r *= 16; 375 r += (s[i] - 'A') + 10; 376 } 377 } 378 return r; 379} 380 381 382/********************************************************************************/ 383/*Function: sanswer. Returns a String answer from mp3 UART module. */ 384/*Parameter:- uint8_t b. void. */ 385/*Return: String. If the answer is well formated answer. */ 386 387String sanswer(void) 388{ 389 uint8_t i = 0; 390 String mp3answer = ""; 391 392 // Get only 10 Bytes 393 while (mp3.available() && (i < 10)) 394 { 395 uint8_t b = mp3.read(); 396 ansbuf[i] = b; 397 i++; 398 399 mp3answer += sbyte2hex(b); 400 } 401 402 // if the answer format is correct. 403 if ((ansbuf[0] == 0x7E) && (ansbuf[9] == 0xEF)) 404 { 405 return mp3answer; 406 } 407 408 return "???: " + mp3answer; 409} 410
LCD
arduino
powering the LCD by the intended buttons
1 2//BUG - the screen freezes after several seconds 3//Compatible with the Arduino IDE 1.0 4//Library version:1.1 5// this code is a DM consul - 1 button shows on LCD 5 random D20 result , 2nd button shows a random speel (from SD card), 3rd button shows a random monster (from SD card). 6// 4 - random DM decision, 5 -random magic item, 6 -random NPC (race + trait), 7 - random gold treasure (1-500) 7#include <Wire.h> 8#include <LiquidCrystal_I2C.h> 9#include <SPI.h> 10#include <SD.h> 11File myFile; 12int LCD_colums = 20; 13int LCD_rows = 4; 14LiquidCrystal_I2C lcd(0x3F,LCD_colums,LCD_rows); // set the LCD address to 0x3F for a 20 chars and 4 line display 15int n=0; 16int rnd=0; 17int DLY=0; 18// defining string arrays of races 19char *race[22] = {"Dwarf", "Elf", "Halfling" , "Human" , "Gnome" ,"Half-orc" , "Dragonborn", "Tiefling", "Aasimar", "Warforged", 20 "Yuan-ti-Pureblood", "Triton", "Goliath", "Tabaxi", "Half-Elf" , "LizardFolk", "Genasi" , "Aarakocra" ,"Bugbear" , "Kenku", "Githyanki", "Tortle"}; 21 22 23// spell char per row = 34 24//int LineLength = 34; 25// constants won't change. They're used here to set pin numbers: 26const int BUTTON_PIN_D20 = 7; // the number of the d20 pushbutton pin 27const int BUTTON_PIN_SPELL = 6; // the number of the spell pushbutton pin 28const int BUTTON_PIN_MONSTER = 8; // the number of the monster pushbutton pin 29const int BUTTON_PIN_DM = 9; // the number of the DM decision pushbutton pin 30const int BUTTON_PIN_item = 5; // the number of the Magic items pushbutton pin 31const int BUTTON_PIN_NPC = 4; // the number of the NPC characters pushbutton pin 32const int BUTTON_PIN_gold = 3; // the number of the NPC characters pushbutton pin 33 34 35// Variables will change: 36 37int currentStateD20; // the current reading from the input pin 38int currentStateSPELL; // the current reading from the input pin 39int currentStateMONSTER; 40int currentStateDM; // the current reading from the input pin 41int currentStateitem; // the current reading from the input pin 42int currentStateNPC; // the current reading from the input pin 43int currentStategold; // the current reading from the input pin 44 45void setup() 46{ 47 // initialize serial communication at 9600 bits per second: 48 Serial.begin(9600); 49 lcd.init(); // initialize the lcd 50 // Print a message to the LCD. 51 lcd.backlight(); 52 lcd.setCursor(0,1); 53 lcd.print(F("Hello, DM Yoni Zabow")); 54 // initialize the pushbutton pin as an pull-up input 55 // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. 56 pinMode(BUTTON_PIN_D20, INPUT_PULLUP); 57 pinMode(BUTTON_PIN_SPELL, INPUT_PULLUP); 58 pinMode(BUTTON_PIN_MONSTER, INPUT_PULLUP); 59 pinMode(BUTTON_PIN_DM, INPUT_PULLUP); 60 pinMode(BUTTON_PIN_item, INPUT_PULLUP); 61 pinMode(BUTTON_PIN_NPC, INPUT_PULLUP); 62 pinMode(BUTTON_PIN_gold, INPUT_PULLUP); 63} 64 65// this function finds the needed line (lineNumber) in a txt file from sd card (indecated by filenmber) and shows it on the lcd screen 66void printLineN(unsigned int lineNumber, unsigned int filenumber ){ 67 char rnd_string[LCD_colums+1] = ""; 68 rnd_string[1] = 0; 69 char cr; 70 // SD card initialization 71 while (!Serial) { 72 ; // wait for serial port to connect. Needed for native USB port only 73 } 74 Serial.print(F("Initializing SD card...")); 75 if (!SD.begin(10)) { 76 Serial.println(F("initialization failed!")); 77 while (1); 78 } 79 Serial.println(F("initialization done.")); 80 81 // finding the correct file to open 82 switch (filenumber) { 83 case 1: {myFile = SD.open("monsters.txt"); 84 break;} 85 case 2: {myFile = SD.open("spells.txt"); 86 break;} 87 case 3: {myFile = SD.open("DMD.txt"); 88 break;} 89 case 4: {myFile = SD.open("item.txt"); 90 break;} 91 case 5: {myFile = SD.open("NPCtrait.txt"); 92 break;} 93 } 94 myFile.seek(0); 95 Serial.println(F("file opened sucssesfuly.")); 96 97 // seeking the right line in the file 98 for(unsigned int i = 0; i < (lineNumber -1);){ 99 cr = myFile.read(); 100 if(cr == '\n'){ 101 i++; 102 } 103 } 104 int j=0; //string index 105 cr = ' '; // initialization of cr to get inside while loop 106 107 //Now we are at the right line 108 while(cr != '\n'){ 109 cr = myFile.read(); 110 Serial.println(cr); 111 rnd_string[j] = cr; 112 j++; 113 //if string is bigger than LCD max columns than print to LCD and move one row down 114 if (j == LCD_colums) { 115 if (rnd_string[j] == '\n') 116 rnd_string[j-1] = 0; 117 else if (rnd_string[j] == '\r') 118 rnd_string[j-1] = 0; 119 lcd.setCursor(0,1); 120 //print first row 121 lcd.print(rnd_string); 122 // zero the string and the counter 123 rnd_string[0] = 0; 124 j=0; 125 } 126 } 127 //closing the string without \ 128\ which adds unwanted char in the end 129 rnd_string[j-2] = 0; 130 lcd.setCursor(0,2); 131 //printing the string to LCD 132 lcd.print(rnd_string); 133 // close the file: 134 myFile.close(); 135} 136 137void loop() 138{ 139 if (DLY == 7000){ 140 DLY = 0; 141 lcd.clear(); 142 lcd.setCursor(0,1); 143 lcd.print(F("Hello, DM Yoni Zabow")); 144 } 145 // read the state of the switch/button: 146 currentStateD20 = digitalRead(BUTTON_PIN_D20); 147 if(currentStateD20 == LOW) 148 { 149 lcd.clear(); 150 lcd.setCursor(1,0); 151 lcd.print(F("5 X D20 roll")); 152 lcd.setCursor(0,2); 153 lcd.print(random(1, 21)); 154 lcd.setCursor(3,2); 155 lcd.print(random(1, 21)); 156 lcd.setCursor(6,2); 157 lcd.print(random(1, 21)); 158 lcd.setCursor(9,2); 159 lcd.print(random(1, 21)); 160 lcd.setCursor(12,2); 161 lcd.print(random(1, 21)); 162 DLY = 0; 163 } 164 // read the state of the switch/button: 165 currentStateSPELL = digitalRead(BUTTON_PIN_SPELL); 166 if(currentStateSPELL == LOW) 167 { 168 lcd.clear(); 169 lcd.setCursor(0,0); 170 lcd.print(F("RND Spell Name&LVL:")); 171 rnd = random(1, 460); 172 Serial.println(rnd); 173 printLineN(rnd,2); 174 DLY = 0; 175 } 176 currentStateMONSTER = digitalRead(BUTTON_PIN_MONSTER); 177 if(currentStateMONSTER == LOW) 178 { 179 lcd.clear(); 180 lcd.setCursor(0,0); 181 lcd.print(F("Monster Name|AC|HP:")); 182 rnd = random(1, 507); 183 Serial.println(rnd); 184 printLineN(rnd,1); 185 DLY = 0; 186 } 187 currentStateDM = digitalRead(BUTTON_PIN_DM); 188 if(currentStateDM == LOW) 189 { 190 lcd.clear(); 191 lcd.setCursor(0,0); 192 lcd.print(F("RND DM Decision:")); 193 rnd = random(1, 25); 194 Serial.println(rnd); 195 printLineN(rnd,3); 196 DLY = 0; 197 } 198 currentStateitem = digitalRead(BUTTON_PIN_item); 199 if(currentStateitem == LOW) 200 { 201 lcd.clear(); 202 lcd.setCursor(0,0); 203 lcd.print(F("RND Magic item:")); 204 rnd = random(1, 240); 205 Serial.println(rnd); 206 printLineN(rnd,4); 207 DLY = 0; 208 } 209 currentStateNPC = digitalRead(BUTTON_PIN_NPC); 210 if(currentStateNPC == LOW) 211 { 212 lcd.clear(); 213 lcd.setCursor(0,0); 214 lcd.print(F("RND NPC Trait:")); 215 rnd = random(1, 1389); 216 Serial.println(rnd); 217 printLineN(rnd,5); 218 lcd.setCursor(0,3); 219 lcd.print("Race: "); 220 lcd.setCursor(6,3); 221 rnd = random(0, 21); 222 lcd.print(race[rnd]); 223 DLY = 0; 224 } 225 currentStategold = digitalRead(BUTTON_PIN_gold); 226 if(currentStategold == LOW) 227 { 228 lcd.clear(); 229 lcd.setCursor(0,0); 230 lcd.print(F("RND Gold treature:")); 231 rnd = random(1, 501); 232 Serial.println(rnd); 233 lcd.setCursor(0,2); 234 lcd.print(rnd); 235 DLY = 0; 236 } 237 DLY++; 238 Serial.println(DLY); 239} 240
Downloadable files
complete circuit
This is the complete circuit sketch. (I used a BlueTooth module to represent the XY6300 UART TTL MP3 module) The power module is connected to the Vin and GND of both Arduinos. The buttons arranged in one row for simplicity,
complete circuit
complete circuit
This is the complete circuit sketch. (I used a BlueTooth module to represent the XY6300 UART TTL MP3 module) The power module is connected to the Vin and GND of both Arduinos. The buttons arranged in one row for simplicity,
complete circuit
Documentation
Buttons
STL files for all the buttons I 3D printed (the button with the dragon for the RND monster has 2 vertions)
Buttons
front panel
Front panel
front panel
front panel
Front panel
front panel
Buttons
STL files for all the buttons I 3D printed (the button with the dragon for the RND monster has 2 vertions)
Buttons
Comments
Only logged in users can leave comments
droraha
0 Followers
•0 Projects
Table of contents
Intro
2
0