נגן המוזיקה
כניסת הטרנסמיט - בשביל שהנגן יוכל להחזיר האם הוא מנגן כעת.
MP3 mp3(7, 6); // Rx, Tx
MP3 -> object, mp3 - the instance of the object.
הווליום נע בין 0 ל30.
הנגן - יש דילאי של 50 מילי שניות כדי לוודא שהפקודה של הסיפרייה מתחילה לפעול (ספציפית לסיפרייה הזו).
אחרי שמפעילים שיר, הוא ינגן עד שהשיר יסתיים או עד שיהיה stop. לכן, כדי לנגן שיר בדיוק שנייה אחת לדגומא, נוסיף דילאי אחרי פקודת תחילת השיר ואחר כך נוסיף stop.
פקודת הפעלת השיר:
mp3.playWithFileName(02, 03);
02 זה שם התיקייה.
03 זה השיר בתוך התיקייה.
חייב תיקיות של 01, 02, וכו.
אפשר להוסיף שם -
01_example
גם לשירים, העיקר שיהיה את התחילית של המספר.
הנגן מקבל את פקודת הפעלת השיר והשיר מתנגן באופן עצמאי, כלומר, הנגן מקבל את הפקודה ואז אין צורך לחכות לסוף השיר.זה אומר שאני יכולה להפעיל שיר ואז לעשות דברים אחרים בארדואינו. השיר יפסיק מעצמו או שנעשה לו stop.
תזכורת - פינים אנלוגיים - טווח של 0 עד 1023, הם רק Input (בניגוד לדיגיטליים שהם גם input וגם output.
פקודת map - ממפה את הטווח (כנראה פונקציית האש) - לדוגמא, הפינים האנלוגיים הינם בין 0 ל-1023 אבל הווליום הוא נע מ 0 עד 30. לכן, נשתמש ב- map, שימפה את הטווח של הפינים לטווח של הווליום.
map(value, fromLow, fromHigh, toLow, toHigh)
כפתור -
סימטרי בין צידי הכפתורים.
הכפתור ישב על ההפרדה בין שני צידי המטריצה כאשר 2 רגליים (מאותו הצד) נמצאים באותו הצד של המטריצה).
הפין יהיה מסוג INPUT_PULLUP.
בשיעור הבא נסביר מה זה אומר.
תזכורת מהשיעור הקודם:
פעולה פשוטה של הנגן:
#include <SoftwareSerial.h>
#include "RedMP3.h"
// Initialize MP3 module with software serial on pins 7 (Rx) and 6 (Tx)
MP3 mp3(7, 6);
void setup() {
Serial.begin(9600); // Start serial communication for debugging
delay(50); // Allow time for MP3 module to initialize
mp3.setVolume(25); // Set volume level (range: 0-30)
// verify that the setVolume command will update the command
delay(50);
// Play a specific song from folder "02", file "003.mp3"
mp3.playWithFileName(02, 01);
delay(2000); // Wait for 3 seconds while the song plays
mp3.stopPlay(); // Stop playback
delay(50);
}
כפתור:
רגל אחת של הכפתור תהיה ב GND והשנייה תהיה בפין דיגיטלי. (הכפתור הוא סימטרי וזה לא משנה לאיזה רגל מחברים מה העיקר שיהיו באותו הצד).
לארדואינו יש נגד פנימי וכשאנחנו משתמשים בכפתור אנחנו משתמשים בנגד הפנימי. (אל הנגד מגיע 5V של הארדואינו והוא מוריד את המתח אחריו כדי שיהיה מתח קטן מ5V כדי שהפין לא ישרף). מצב הפין הוא INPUT_PULLUP. זה אומר שהפין שלנו תמיד יהיה ב HIGH וכאשר נלחץ על הכפתור נחבר אותו לGND - LOW.
=> בלחיצה על הכפתור, מעבירים את הפין ל LOW => הזרם זורם לGND => הכפתור עובד!
הקוד הבא משלב את הכפתור, ובלולאה אנחנו מעבירים שיר בכל לחיצה על הכפתור.
#include <SoftwareSerial.h>
#include "RedMP3.h"
#define BUTTON_PIN 4
#define VOLUME_PIN A0
// Initialize MP3 module with software serial on pins 7 (Rx) and 6 (Tx)
MP3 mp3(7, 6);
int songNum = 0;
int volume;
void setup() {
Serial.begin(9600); // Start serial communication for debugging
delay(50); // Allow time for MP3 module to initialize
pinMode(BUTTON_PIN, INPUT_PULLUP);
mp3.setVolume(20); // Set volume level (range: 0-30)
// verify that the setVolume command will update the command
delay(50);
// Play a specific song from folder "02", file "003.mp3"
// mp3.playWithFileName(02, 01);
delay(2000); // Wait for 3 seconds while the song plays
mp3.stopPlay(); // Stop playback
delay(50);
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
songNum++;
mp3.playWithFileName(02, songNum);
delay(500);
}
}
הקוד כולל את הפוטנציומטר (מחברים את כל הרגליים באותו הצד של הפוטנציומטר).
#include <SoftwareSerial.h>
#include "RedMP3.h"
#define BUTTON_PIN 4
#define VOLUME_PIN A0
// Initialize MP3 module with software serial on pins 7 (Rx) and 6 (Tx)
MP3 mp3(7, 6);
int songNum = 0;
int volume;
void setup() {
Serial.begin(9600); // Start serial communication for debugging
delay(50); // Allow time for MP3 module to initialize
pinMode(BUTTON_PIN, INPUT_PULLUP);
mp3.setVolume(20); // Set volume level (range: 0-30)
// verify that the setVolume command will update the command
delay(50);
// Play a specific song from folder "02", file "003.mp3"
// mp3.playWithFileName(02, 01);
delay(2000); // Wait for 3 seconds while the song plays
mp3.stopPlay(); // Stop playback
delay(50);
}
void loop() {
volume = map(analogRead(VOLUME_PIN), 0, 1023, 10, 30); // 10 -> 30 dont really hear somethings so we'll go from 10 to 30
mp3.setVolume(volume); // Set volume level (range: 0-30)
// verify that the setVolume command will update the command
delay(50);
if (digitalRead(BUTTON_PIN) == LOW) {
songNum++;
mp3.playWithFileName(02, songNum);
delay(500);
}
}
משימת כיתה -
נרצה שבעזרת הפוטנציומטר יעביר גם את השיר או יעצור.
#include <SoftwareSerial.h>
#include "RedMP3.h"
#define BUTTON_PIN 4
#define VOLUME_PIN A0
// Initialize MP3 module with software serial on pins 7 (Rx) and 6 (Tx)
MP3 mp3(7, 6);
int songNum = 0;
int volume;
const int MAX_VALUE = 30;
const int MIN_VALUE = 0;
void setup() {
Serial.begin(9600); // Start serial communication for debugging
delay(50); // Allow time for MP3 module to initialize
pinMode(BUTTON_PIN, INPUT_PULLUP);
mp3.setVolume(20); // Set volume level (range: 0-30)
// verify that the setVolume command will update the command
delay(50);
// Play a specific song from folder "02", file "003.mp3"
// mp3.playWithFileName(02, 01);
delay(2000); // Wait for 3 seconds while the song plays
mp3.stopPlay(); // Stop playback
delay(50);
}
void loop() {
volume = map(analogRead(VOLUME_PIN), 0, 1023, 10, 30); // 10 -> 30 dont really hear somethings so we'll go from 10 to 30
mp3.setVolume(volume);
delay(50);
if (volume == MAX_VALUE) {
songNum++;
mp3.playWithFileName(02, songNum);
delay(500);
}
else if(volume == MIN_VALUE) {
mp3.stopPlay();
delay(50);
}
Serial.print("volume = ");
Serial.print(volume);
Serial.print("songNum = ");
Serial.println(songNum);
}