Advertisement
Guest User

Untitled

a guest
Dec 27th, 2015
1,664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. /*
  2.     This example plays a random file (001 to 010) forever
  3.     It uses the playFileAndWait() command so no extra code is needed, it will play another file as soon as the previous one finishes
  4.     If it doesn't work, try reversing the RX/TX wires as that's probably going to be the most common mistake
  5.     Also check that the player's BUSY line is connected to Arduino pin 3
  6.     Finally check that the player has a speaker connected as well as ground and VCC
  7. */
  8.  
  9. #include "SoftwareSerial.h"
  10. #include "MP3FLASH16P.h"
  11. MP3FLASH16P myPlayer;
  12.  
  13. #define PIN_voice_LED     6
  14. #define PIN_pulse_LED     5
  15. #define PIN_sound         A4
  16. #define PIN_trigger       A3
  17. #define number_of_sounds  5
  18.  
  19. void setup() {
  20.     myPlayer.init(2); // Init the player with the busy pin connected to Arduino pin 3
  21.     pinMode(PIN_voice_LED, OUTPUT);
  22.     pinMode(PIN_pulse_LED, OUTPUT);
  23.     pinMode(PIN_sound, INPUT);
  24.     pinMode(PIN_trigger, INPUT);
  25.     randomSeed(analogRead(0));
  26.     Serial.begin(9600);
  27. }
  28.  
  29. int voiceBrightness;
  30. int pulseBrightness;
  31. void loop() {
  32.     // Checking for trigger going LOW
  33.     if(digitalRead(PIN_trigger) == LOW){
  34.         if(!myPlayer.isBusy()){
  35.             myPlayer.playFile(random(1, number_of_sounds+1));
  36.             delay(100);
  37.         }
  38.     }
  39.    
  40.     // Voice light
  41.     if(myPlayer.isBusy()){
  42.         // Sets the brightness of the light based on the loudness of the voice
  43.         voiceBrightness = constrain(map(analogRead(PIN_sound), 700, 1024, 0, 255), 0, 255);
  44.         analogWrite(PIN_voice_LED, voiceBrightness);
  45.     }else{
  46.         // No voice playing, light is off
  47.         analogWrite(PIN_voice_LED, 0);
  48.     }
  49.    
  50.     // Slow pulsing light on the side of BB-8's head
  51.     pulseBrightness = 170+(sin(millis()/400.00)*80);
  52.     analogWrite(PIN_pulse_LED, pulseBrightness);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement