maystone

Dr. Hallard's Device

Oct 3rd, 2018
55
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Adafruit_VS1053.h>
  3. #include <SD.h>
  4. //#include <Servo.h>
  5.  
  6. //Servo myservo;
  7. #define SHIELD_RESET  -1
  8. #define SHIELD_CS   7
  9. #define SHIELD_DCS  6
  10. #define CARDCS 4
  11. #define DREQ 3
  12. Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  13. int staticOn = 1;
  14. int pos = 0;
  15. const int buttonPin = 2;
  16. const int lucidcode1 = 7; //23
  17. const int lucidcode2 = 12; //D1
  18. const int lucidcode3 = 6; //Green
  19. const int infilcode1 = 14; //35
  20. const int infilcode2 = 8; //B6
  21. const int infilcode3 = 0; //Red
  22. const int infilcode4 = 42; //80
  23. const int infilcode5 = 17; //A33
  24. const int infilcode6 = 10;//Purple
  25. int buttonState = 0;
  26. boolean lucidwin = false;
  27. boolean infilwin = false;
  28. unsigned long startTime = 0;
  29. unsigned long onTime = 1000;
  30. int relay = 1;
  31. int whiteled = 8;
  32. int bluePin = 9;
  33. int greenPin = 10;
  34. int redPin = 5;
  35. int outputValue1 = 0;
  36. int sensorValue1 = 0;
  37. int outputValue2 = 0;
  38. int sensorValue2 = 0;
  39. int outputValue3 = 0;
  40. int sensorValue3 = 0;
  41. int flipmode = 0;
  42. int infilcount = 0;
  43.  
  44. void setup() {
  45.   //Serial.begin(9600);
  46.   Serial.println("Adafruit VS1053 Library Test");
  47. if (! musicPlayer.begin()) { // initialise the music player
  48.    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
  49.    //while (1);
  50.   }
  51.   Serial.println(F("VS1053 found"));
  52.  
  53. if (!SD.begin(CARDCS)) {
  54.   Serial.println(F("SD failed, or not present"));
  55.   //while (1);  // don't do anything more
  56.   }
  57.  
  58.   Serial.println("SD OK!");
  59. printDirectory(SD.open("/"), 0);
  60.   // Set volume for left, right channels. lower numbers == louder volume!
  61. musicPlayer.setVolume(35,35);
  62.  
  63. if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
  64.     Serial.println(F("DREQ pin is not an interrupt pin"));
  65.    
  66. //musicPlayer.sineTest(0x44, 100);  // Make a tone to indicate VS1053 is working
  67.  
  68. //  myservo.attach(8);
  69.   pinMode(whiteled, OUTPUT);
  70.   pinMode(redPin, OUTPUT);
  71.   pinMode(greenPin, OUTPUT);
  72.   pinMode(bluePin, OUTPUT);
  73.   pinMode(buttonPin, INPUT);
  74.   pinMode(relay, OUTPUT);
  75.   digitalWrite(relay, LOW);
  76.  
  77. }
  78.  
  79. void setColor(int red, int green, int blue)
  80. {
  81.   #ifdef COMMON_ANODE
  82.     red = 255 - red;
  83.     green = 255 - green;
  84.     blue = 255 - blue;
  85.   #endif
  86.   analogWrite(redPin, red);
  87.   analogWrite(greenPin, green);
  88.   analogWrite(bluePin, blue);  
  89. }
  90.  
  91. void printDirectory(File dir, int numTabs) {
  92.    while(true) {
  93.      
  94.      File entry =  dir.openNextFile();
  95.      if (! entry) {
  96.        // no more files
  97.        //Serial.println("**nomorefiles**");
  98.        break;
  99.      }
  100.      for (uint8_t i=0; i<numTabs; i++) {
  101.        Serial.print('\t');
  102.      }
  103.      Serial.print(entry.name());
  104.      if (entry.isDirectory()) {
  105.        Serial.println("/");
  106.        printDirectory(entry, numTabs+1);
  107.      } else {
  108.        // files have sizes, directories do not
  109.        Serial.print("\t\t");
  110.        Serial.println(entry.size(), DEC);
  111.      }
  112.      entry.close();
  113.    }
  114. }
  115.  
  116. void NoWinners(){
  117.  
  118. lucidwin = false;
  119. infilwin = false;
  120. }
  121. void LucidWinner(){
  122.   delay(400);
  123.   Serial.println("DREAM TRANSMISSION ACTIVATED");
  124.   musicPlayer.stopPlaying();
  125.   Serial.println("Playing lswin.mp3");
  126.   musicPlayer.startPlayingFile("lswin.mp3");
  127.   digitalWrite(relay, HIGH);
  128.   setColor(0, 0, 0);
  129.   delay(200);
  130.   setColor(0, 0, 255);
  131.   delay(200);
  132.   setColor(0, 0, 0);
  133.   delay(200);
  134.   setColor(0, 0, 255);
  135.   delay(200);
  136.   setColor(0, 0, 0);
  137.   delay(200);
  138.   setColor(0, 0, 255);
  139.   delay(200);
  140.   setColor(0, 0, 0);
  141.   delay(200);
  142.   setColor(0, 0, 255);
  143.   delay(200);
  144.   setColor(0, 0, 0);
  145.   delay(200);
  146.   setColor(0, 0, 255);
  147.   delay(200);
  148.   setColor(0, 0, 0);
  149.   delay(200);
  150.   setColor(0, 0, 255);
  151.   delay(40000);
  152.   musicPlayer.stopPlaying();
  153.   Serial.println("lssong.mp3");
  154.   musicPlayer.startPlayingFile("lssong.mp3");
  155.   delay (177000);
  156.   //for (pos = 0; pos <= 180; pos += 1) {
  157.   //  myservo.write(pos);
  158.   //  delay(25);
  159.   //}
  160. }
  161.    
  162.  
  163. void LucidCode1Entered(){
  164.   delay(1000);
  165.     setColor(0, 0, 255);
  166.   musicPlayer.stopPlaying();
  167.   Serial.println("send.mp3");
  168.   musicPlayer.startPlayingFile("send.mp3");
  169.   //musicPlayer.sineTest(0x44, 50);
  170.     Serial.println("LUCID 1 CODE HAS BEEN ENTERED");
  171.     delay(1000);
  172.     setColor(0, 0, 0);
  173.   musicPlayer.stopPlaying();
  174.     delay(800);
  175. }
  176.  
  177. void LucidCode2Entered(){
  178.   delay(1000);
  179.     setColor(0, 0, 255);
  180.   musicPlayer.stopPlaying();
  181.   Serial.println("Playing send.mp3");
  182.   musicPlayer.startPlayingFile("send.mp3");
  183.   //musicPlayer.sineTest(0x22, 50);
  184.   Serial.println("LUCID 2 CODE HAS BEEN ENTERED");
  185.     delay(1000);
  186.     setColor(0, 0, 0);
  187.     musicPlayer.stopPlaying();
  188.   delay(800);
  189. }
  190.  
  191. void LucidCode3Entered(){
  192.   delay(1000);
  193.     setColor(0, 0, 255);
  194.   musicPlayer.stopPlaying();
  195.   Serial.println("Playing send.mp3");
  196.   musicPlayer.startPlayingFile("send.mp3");
  197.   //musicPlayer.sineTest(0x10, 50);
  198.   Serial.println("LUCID 3 CODE HAS BEEN ENTERED");
  199.     delay(1000);
  200.     setColor(0, 0, 0);
  201.   musicPlayer.stopPlaying();
  202.     delay(800);
  203. }
  204. void InfilWin(){
  205.   delay(400);
  206.   setColor(0, 0, 0);
  207.   delay(200);
  208.   setColor(255, 0, 0);
  209.     delay(200);
  210.     setColor(0, 0, 0);
  211.     delay(200);
  212.     setColor(255, 0, 0);
  213.     delay(200);
  214.     setColor(0, 0, 0);
  215.     delay(200);
  216.     setColor(255, 0, 0);
  217.   delay(200);
  218.   setColor(0, 0, 0);
  219.   delay(200);
  220.   setColor(255, 0, 0);
  221.   delay(200);
  222.   setColor(0, 0, 0);
  223.   delay(200);
  224.   setColor(255, 0, 0);
  225.   delay(200);
  226.   setColor(0, 0, 0);
  227.   delay(200);
  228.   setColor(255, 0, 0);
  229.     Serial.println("TRANSMISSION DEVICE DEACTIVATED!");
  230.   musicPlayer.stopPlaying();
  231.   Serial.println("Playing infwin.mp3");
  232.   musicPlayer.startPlayingFile("infwin.mp3");
  233.   delay(39000);
  234.   Serial.println("Playing insong.mp3");
  235.   musicPlayer.startPlayingFile("insong.mp3");
  236.   delay (73500);
  237.   setColor(0, 0, 0);
  238. }
  239.  
  240. void InfilCode1Entered(){
  241.   delay(1000);
  242.     setColor(255, 0, 0);
  243.   musicPlayer.stopPlaying();
  244.   Serial.println("Playing send.mp3");
  245.   musicPlayer.startPlayingFile("send.mp3");
  246.     Serial.println("DESTRUCTION CODE 1 HAS BEEN ENTERED");
  247.     delay(1000);
  248.     setColor(0, 0, 0);
  249.   musicPlayer.stopPlaying();
  250.     delay(800);
  251. }
  252.  
  253. void InfilCode2Entered(){
  254.   delay(1000);
  255.     setColor(255, 0, 0);
  256.   musicPlayer.stopPlaying();
  257.   Serial.println("Playing send.mp3");
  258.   musicPlayer.startPlayingFile("send.mp3");
  259.   Serial.println("DESTRUCTION CODE 2 HAS BEEN ENTERED");
  260.     delay(1000);
  261.     setColor(0, 0, 0);
  262.     musicPlayer.stopPlaying();
  263.     delay(800);
  264. }
  265.  
  266. void InfilCode3Entered(){
  267.   delay(1000);
  268.     setColor(255, 0, 0);
  269.   musicPlayer.stopPlaying();
  270.   Serial.println("Playing send_switch.mp3");
  271.   musicPlayer.startPlayingFile("send.mp3");
  272.   Serial.println("DESTRUCTION CODE 3 HAS BEEN ENTERED");
  273.     delay(1000);
  274.     setColor(0, 0, 0);
  275.   musicPlayer.stopPlaying();
  276.     delay(800);
  277. }
  278.  
  279. void InfilCode4Entered(){
  280.   delay(1000);
  281.   setColor(255, 0, 0);
  282.   musicPlayer.stopPlaying();
  283.   Serial.println("Playing send_switch.mp3");
  284.   musicPlayer.startPlayingFile("send.mp3");
  285.   Serial.println("DESTRUCTION CODE 4 HAS BEEN ENTERED");
  286.   delay(1000);
  287.   setColor(0, 0, 0);
  288.   musicPlayer.stopPlaying();
  289.   delay(800);
  290. }
  291.  
  292. void InfilCode5Entered(){
  293.   delay(1000);
  294.   setColor(255, 0, 0);
  295.   musicPlayer.stopPlaying();
  296.   Serial.println("Playing send.mp3");
  297.   musicPlayer.startPlayingFile("send.mp3");
  298.   Serial.println("DESTRUCTION CODE 5 HAS BEEN ENTERED");
  299.   delay(1000);
  300.   setColor(0, 0, 0);
  301.   musicPlayer.stopPlaying();
  302.   delay(800);
  303. }
  304.  
  305. void InfilCode6Entered(){
  306.   delay(1000);
  307.   setColor(255, 0, 0);
  308.   musicPlayer.stopPlaying();
  309.   Serial.println("Playing send.mp3");
  310.   musicPlayer.startPlayingFile("send.mp3");
  311.   Serial.println("DESTRUCTION CODE 6 HAS BEEN ENTERED");
  312.   delay(1000);
  313.   setColor(0, 0, 0);
  314.   musicPlayer.stopPlaying();
  315.   delay(800);
  316. }
  317.  
  318. void loop() {
  319.   if (staticOn == 1) {
  320.     Serial.println("Playing static_1.mp3");
  321.     musicPlayer.startPlayingFile("static_1.mp3");
  322.     staticOn = 0; //flip the flag so it doesn't start playing over again
  323.   }
  324.   while (musicPlayer.playingMusic) {
  325.        // file is now playing in the 'background'
  326.  
  327.   //setColor(255, 255, 0);  // yellow  
  328.   digitalWrite(whiteled, HIGH);
  329.   int buttonState = digitalRead(buttonPin);
  330.   sensorValue1 = analogRead(A0);
  331.   outputValue1 = map(sensorValue1, 0, 1023, 0, 45);
  332.   sensorValue2 = analogRead(A1);
  333.   outputValue2 = map(sensorValue2, 0, 1023, 30, 0);
  334.   sensorValue3 = analogRead(A3);
  335.   outputValue3 = map(sensorValue3, 0, 1023, 10, 0);
  336.  
  337.   if (buttonState == 1 && flipmode == 0) {
  338.   setColor(0, 0, 0);  // yellow
  339.   //Serial.println("No Winners");  
  340.   }
  341.   if (buttonState == 0) {
  342.   setColor(255, 255, 0);  // yellow  
  343.   }
  344.  
  345.   if (outputValue1 != infilcode1 && outputValue2 != infilcode2 && outputValue3 != infilcode3 && outputValue1 != lucidcode1 && outputValue2 != lucidcode2 && outputValue3 != lucidcode3 && buttonState == HIGH) {
  346.     NoWinners();
  347.   }
  348.  
  349.   if (outputValue1 == lucidcode1 && outputValue2 == lucidcode2 && outputValue3 == lucidcode3 && buttonState == HIGH && flipmode == 0) {
  350.     lucidwin = true;
  351.     startTime = millis();
  352.   }
  353.     else {
  354.   NoWinners();
  355.     }
  356.  
  357.   if (lucidwin && millis() - startTime < onTime) {
  358.   LucidWinner();
  359.     //while(1);
  360.   }
  361.  
  362.   if (outputValue1 == lucidcode1 && buttonState == HIGH && flipmode == 0) {
  363.     LucidCode1Entered();
  364.   }
  365.  
  366.   if (outputValue2 == lucidcode2 && buttonState == HIGH && flipmode == 0) {
  367.     LucidCode2Entered();
  368.   }
  369.  
  370.   if (outputValue3 == lucidcode3 && buttonState == HIGH && flipmode == 0) {
  371.     LucidCode3Entered();
  372.   }
  373.  
  374.   if (outputValue1 == infilcode1){
  375.   infilcount++;
  376.   }
  377.   if (outputValue2 == infilcode2){
  378.   infilcount++;
  379.   }
  380.   if (outputValue3 == infilcode3){
  381.   infilcount++;
  382.   }  
  383.   if (outputValue1 == infilcode4){
  384.   infilcount++;
  385.   }
  386.   if (outputValue2 == infilcode5){
  387.   infilcount++;
  388.   }  
  389.   if (outputValue3 == infilcode6){
  390.   infilcount++;
  391.   }
  392.  Serial.println(infilcount);
  393.    
  394.   if (infilcount >= 3 && buttonState == HIGH && flipmode == 0) {
  395.     infilwin = true;
  396.     startTime = millis();
  397.   }
  398.     else {
  399.     NoWinners();
  400.     }
  401.  
  402.   if (infilwin && millis() - startTime < onTime) {
  403.     InfilWin();
  404.   while(1);
  405.   }
  406.  
  407.   if (outputValue1 == infilcode1 && buttonState == HIGH && flipmode == 0) {
  408.     InfilCode1Entered();
  409.   }
  410.  
  411.   if (outputValue2 == infilcode2 && buttonState == HIGH && flipmode == 0) {
  412.     InfilCode2Entered();
  413.   }
  414.  
  415.   if (outputValue3 == infilcode3 && buttonState == HIGH && flipmode == 0) {
  416.   InfilCode3Entered();
  417.   }
  418.  
  419.   if (outputValue1 == infilcode4 && buttonState == HIGH && flipmode == 0) {
  420.   InfilCode4Entered();
  421.   }
  422.  
  423.   if (outputValue2 == infilcode5 && buttonState == HIGH && flipmode == 0) {
  424.   InfilCode5Entered();
  425.   }
  426.  
  427.   if (outputValue3 == infilcode6 && buttonState == HIGH && flipmode == 0) {
  428.   InfilCode6Entered();
  429.   }
  430.   Serial.print(outputValue1);
  431.   Serial.print(", ");
  432.   Serial.print(outputValue2);
  433.   Serial.print(", ");
  434.   Serial.print(outputValue3);
  435.   Serial.print(", ");
  436.   Serial.println(buttonState);
  437.   delay(120);
  438.  
  439.   infilcount = 0;
  440.  
  441.   if (buttonState == HIGH) {  
  442.   flipmode=1;
  443.   }
  444.  
  445.   if (buttonState == LOW) {  
  446.   flipmode=0;
  447.   }
  448.  
  449.   if(musicPlayer.stopped()) {
  450.     Serial.println("End of file.\n");
  451.     staticOn=1; //reset counter so static will start again
  452.     }
  453.   }
  454. }
RAW Paste Data