# 寶貝潛能中心 爬行地墊2.0 [專題網站](https://jamesorry.github.io/APP_Team_2/) ## 音樂地墊 ### 材料 * Arduino UNO板 * [DFPlayer Mini MP3 播放模組](https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299) * 喇叭 * 輕觸開關 * 1K電阻x12 ### 參考電路 ![](https://i.imgur.com/tMCApg2.png) ![](https://i.imgur.com/Eu3Klfs.jpg) 整合電路 ![](https://i.imgur.com/UUE1OwP.png) ### 程式 ``` cpp= #include "Arduino.h" #include "SoftwareSerial.h" // 採用SoftwareSerial程式庫 #include "DFRobotDFPlayerMini.h" // 採用DFRobotDFPlayerMini程式庫 SoftwareSerial mySoftwareSerial(2, 3); // mySoftwareSerial(RX, TX), 宣告軟體序列傳輸埠 // 用來與DFPlayerMini通訊用 DFRobotDFPlayerMini myDFPlayer; //宣告MP3 Player void printDetail(uint8_t type, int value); //印出詳情 int value = 0; // the setup function runs once when you press reset or power the board void setup() { pinMode( 7 , INPUT); Serial.begin(115200); // 定義Serial傳輸速率115200bps mySoftwareSerial.begin(9600); // 定義mySoftwareSerial傳輸速率9600bps, DFPlayerMini的通訊速率為9600bps. delay(3000); Serial.println(F("DFRobot DFPlayer Mini Demo")); // 印出DFRobot DFPlayer Mini Demo字串到Serial通訊埠 Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)")); // 以下用法依此類通 if (!myDFPlayer.begin(mySoftwareSerial)) // 如果DFPlayer Mini回應不正確. { //Use softwareSerial to communicate with mp3. // 印出下面3行字串 Serial.println(F("Unable to begin:")); // 然後程式卡死. Serial.println(F("1.Please recheck the connection!")); Serial.println(F("2.Please insert the SD card!")); while (true); } Serial.println(F("DFPlayer Mini online.")); // 如果DFPlayer Mini回應正確.印出"DFPlayer Mini online." myDFPlayer.setTimeOut(500); // 設定通訊逾時為500ms //----Set volume---- myDFPlayer.volume(25); // 設定音量, 範圍0~30. //myDFPlayer.volumeUp(); // 增加音量 //myDFPlayer.volumeDown(); // 降低音量 //----Set different EQ---- // 設定EQ(等化器 Equalizer) //myDFPlayer.EQ(DFPLAYER_EQ_NORMAL); myDFPlayer.EQ(DFPLAYER_EQ_POP); // myDFPlayer.EQ(DFPLAYER_EQ_ROCK); // 我比較喜歡搖滾模式 // myDFPlayer.EQ(DFPLAYER_EQ_JAZZ); // myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC); // myDFPlayer.EQ(DFPLAYER_EQ_BASS); //----Set device we use SD as default---- // 設定SD卡 // myDFPlayer.outputDevice(DFPLAYER_DEVICE_U_DISK); myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); // myDFPlayer.outputDevice(DFPLAYER_DEVICE_AUX); // myDFPlayer.outputDevice(DFPLAYER_DEVICE_SLEEP); // myDFPlayer.outputDevice(DFPLAYER_DEVICE_FLASH); //----Mp3 control---- // 設定MP3參數 // myDFPlayer.sleep(); //sleep // myDFPlayer.reset(); //Reset the module myDFPlayer.enableDAC(); //Enable On-chip DAC // myDFPlayer.disableDAC(); //Disable On-chip DAC // myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15 //----Mp3 play---- // 設定MP3播放參數 myDFPlayer.play(1); // 播放第1首音樂 // 在SD卡裡放置MP3的目錄,然後放置名為1的MP3即可 // SD\MP3\1.mp3 // myDFPlayer.play(2); // 即是撥放SD\MP3\2.mp3這首歌 // 接下來以此類推 //myDFPlayer.pause(); //pause the mp3 //myDFPlayer.start(); //myDFPlayer.next(); //Play next mp3 //myDFPlayer.previous(); //Play previous mp3 //myDFPlayer.loop(1); //Loop the first mp3 //myDFPlayer.pause(); //pause the mp3 //myDFPlayer.start(); //start the mp3 from the pause //myDFPlayer.playFolder(15, 4); //play specific mp3 in SD:/15/004.mp3; Folder Name(1~99); File Name(1~255) //myDFPlayer.enableLoopAll(); //loop all mp3 files. //myDFPlayer.disableLoopAll(); //stop loop all mp3 files. //myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535) //myDFPlayer.advertise(3); //advertise specific mp3 in SD:/ADVERT/0003.mp3; File Name(0~65535) //myDFPlayer.stopAdvertise(); //stop advertise //myDFPlayer.playLargeFolder(2, 999); //play specific mp3 in SD:/02/004.mp3; Folder Name(1~10); File Name(1~1000) //myDFPlayer.loopFolder(5); //loop all mp3 files in folder SD:/05. myDFPlayer.randomAll(); //Random play all the mp3. //myDFPlayer.enableLoop(); //enable loop. //myDFPlayer.disableLoop(); //disable loop. //----Read imformation---- // Serial.print("State: "); // Serial.println(myDFPlayer.readState()); //read mp3 state // Serial.print("Volume: "); // Serial.println(myDFPlayer.readVolume()); //read current volume // Serial.print("EQ: "); // Serial.println(myDFPlayer.readEQ()); //read EQ setting // Serial.print("File Counts: "); // Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card // Serial.print("CurrentFileNumber: "); //// Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number // Serial.print("FileCountsInFolder: "); // Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read fill counts in folder SD:/03 } void loop() { if (myDFPlayer.available()) // 監視MP3有沒有回應 { // 有的話印出詳情 printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states. } value = analogRead(A1); if (value > 925 && value < 1000) { Serial.println("Button1"); myDFPlayer.next(); } else if (value > 915 && value < 925) { Serial.println("Button2"); myDFPlayer.next(); } else if (value > 880 && value < 915) { Serial.println("Button3"); myDFPlayer.next(); } else if (value > 865 && value < 880) { Serial.println("Button4"); myDFPlayer.next(); } else if (value > 830 && value < 865) { Serial.println("Button5"); myDFPlayer.next(); } else if (value > 780 && value < 830) { Serial.println("Button6"); myDFPlayer.next(); } else if (value > 700 && value < 780) { Serial.println("Button7"); myDFPlayer.next(); } else if (value > 580 && value < 700) { Serial.println("Button8"); myDFPlayer.next(); } else if (value > 50 && value < 580) { Serial.println("Button9"); myDFPlayer.next(); } else if (value < 50 && value > 0) { Serial.println("Button10"); myDFPlayer.next(); } delay(250); } void printDetail(uint8_t type, int value) { switch (type) { case TimeOut: Serial.println(F("Time Out!")); break; case WrongStack: Serial.println(F("Stack Wrong!")); break; case DFPlayerCardInserted: Serial.println(F("Card Inserted!")); break; case DFPlayerCardRemoved: Serial.println(F("Card Removed!")); break; case DFPlayerCardOnline: Serial.println(F("Card Online!")); break; case DFPlayerPlayFinished: Serial.print(F("Number:")); Serial.print(value); Serial.println(F(" Play Finished!")); break; case DFPlayerError: Serial.print(F("DFPlayerError:")); switch (value) { case Busy: Serial.println(F("Card not found")); break; case Sleeping: Serial.println(F("Sleeping")); break; case SerialWrongStack: Serial.println(F("Get Wrong Stack")); break; case CheckSumNotMatch: Serial.println(F("Check Sum Not Match")); break; case FileIndexOut: Serial.println(F("File Index Out of Bound")); break; case FileMismatch: Serial.println(F("Cannot Find File")); break; case Advertise: Serial.println(F("In Advertise")); break; default: break; } break; default: break; } } ``` ## LED閃爍地墊 ### 材料 * [WS2812B RGB LED](https://www.ruten.com.tw/item/show?21850231509219) * Arduino UNO板 ### 參考電路 ![](https://i.imgur.com/40ZnIVn.png) ### 程式 ``` cpp= #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> // Required for 16 MHz Adafruit Trinket #endif #include "Arduino.h" #define LED_PIN 6 #define LED_COUNT 55 Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); bool _ABVAR_1_BTS= false ; bool _ABVAR_2_BTL= false ; int _ABVAR_3_count = 1 ; void setup() { #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) clock_prescale_set(clock_div_1); #endif // END of Trinket-specific code. pinMode( 7 , INPUT); Serial.begin(9600); strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) strip.show(); // Turn OFF all pixels ASAP strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255) } void loop() { // Fill along the length of the strip in various colors... colorWipe(strip.Color(255, 0, 0), 50); // Red colorWipe(strip.Color( 0, 255, 0), 50); // Green colorWipe(strip.Color( 0, 0, 255), 50); // Blue // Do a theater marquee effect in various colors... theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness theaterChase(strip.Color(127, 0, 0), 50); // Red, half brightness theaterChase(strip.Color( 0, 0, 127), 50); // Blue, half brightness rainbow(10); // Flowing rainbow cycle along the whole strip //rainbow2(10); theaterChaseRainbow(100); } void colorWipe(uint32_t color, int wait) { for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... strip.setPixelColor(i, color); // Set pixel's color (in RAM) strip.show(); // Update strip to match delay(wait); // Pause for a moment } } void theaterChase(uint32_t color, int wait) { for(int a=0; a<10; a++) { // Repeat 10 times... for(int b=0; b<3; b++) { // 'b' counts from 0 to 2... strip.clear(); // Set all pixels in RAM to 0 (off) // 'c' counts up from 'b' to end of strip in steps of 3... for(int c=b; c<strip.numPixels(); c += 3) { strip.setPixelColor(c, color); // Set pixel 'c' to value 'color' } strip.show(); // Update strip with new contents delay(wait); // Pause for a moment } } } void rainbow(int wait) { for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) { for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... // Offset pixel hue by an amount to make one full revolution of the // color wheel (range of 65536) along the length of the strip // (strip.numPixels() steps): int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels()); // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or // optionally add saturation and value (brightness) (each 0 to 255). // Here we're using just the single-argument hue variant. The result // is passed through strip.gamma32() to provide 'truer' colors // before assigning to each pixel: strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue))); } strip.show(); // Update strip with new contents delay(wait); // Pause for a moment } } void theaterChaseRainbow(int wait) { int firstPixelHue = 0; // First pixel starts at red (hue 0) for(int a=0; a<30; a++) { // Repeat 30 times... for(int b=0; b<3; b++) { // 'b' counts from 0 to 2... strip.clear(); // Set all pixels in RAM to 0 (off) // 'c' counts up from 'b' to end of strip in increments of 3... for(int c=b; c<strip.numPixels(); c += 3) { // hue of pixel 'c' is offset by an amount to make one full // revolution of the color wheel (range 65536) along the length // of the strip (strip.numPixels() steps): int hue = firstPixelHue + c * 65536L / strip.numPixels(); uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB strip.setPixelColor(c, color); // Set pixel 'c' to value 'color' } strip.show(); // Update strip with new contents delay(wait); // Pause for a moment firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames } } } ``` ###### tags: CYCU APP class final project