Advertisement
kowalyshyn_o

Eyeglass Project_Student

Feb 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. //import libraries downloaded from Sparkfun at https://github.com/sparkfun/LilyPad_MP3_Player
  2. // 3.3V, 8Mhz, Pro-Mini
  3.  
  4. #include <SPI.h>              // To talk to the SD card and MP3 chip
  5. #include <SdFat.h>            // SD card file system
  6. #include <SFEMP3Shield.h>     // MP3 decoder chip
  7.  
  8. //DEFINE ports on MP3 Board
  9. #define T1 A0       // Trig1 analog port
  10. #define T2 A4       // Trig2 analog port
  11. #define T3 A5       // Trig3 analog port
  12.  
  13. #define MP3_ENABLE A2  // Amp enable + MIDI/MP3 mode select
  14. #define SD_SEL 9       // Chip Select for SD card
  15. #define MIN_VOL 255    // Define volume settings for MP3 Board
  16. #define MAX_VOL 0
  17.  
  18. #define REFERENCE_VOLTAGE 3.3   // Set source battery voltage for MP3 board
  19. #define SAMPLE_WINDOW 250       // Sampling time for sensors (ms)
  20. #define LIGHT_THOLD 2.0         // Threshold for light sensor
  21. #define SOUND_THOLD 1.2         // Threshold for sound sensor
  22.  
  23. //Create variables
  24. int T1_sample;      // Store sensor value from Trig1
  25. int T2_sample;      // Store sensor value from Trig2
  26. int T3_output;      // Store value to write to Trig3
  27.  
  28. byte result;                      // Store results from calls to MP3 methods, 0-255 values only
  29. char filename1[]="track001.mp3";  //filename to play, must be 8.3 format only!!
  30.  
  31. // Create objects
  32. SFEMP3Shield MP3player;
  33. SdFat sd;
  34.  
  35. void setup() {
  36.   // Start serial monitor
  37.   Serial.begin(9600);
  38.  
  39.   // Set port input and output
  40.   pinMode(T1,INPUT);   // Sensor #1
  41.   pinMode(T2,INPUT);   // Sensor #2
  42.   pinMode(T3,OUTPUT);  // LED
  43.  
  44.   // Initialize SD Card and display result
  45.   result = sd.begin(SD_SEL,SPI_HALF_SPEED);
  46.   Serial.println("SD Card Initialized (1 for success) = "+String(result));
  47.  
  48.   // Initialize MP3 board and LED in this order
  49.   pinMode(MP3_ENABLE,OUTPUT);            //Enable MP3 mode
  50.   digitalWrite(MP3_ENABLE,LOW);          //Turn off amplifier
  51.   result = MP3player.begin();            //Start board
  52.   digitalWrite(MP3_ENABLE,HIGH);         //Turn on amplifier
  53.   delay(2);
  54.   Serial.println("MP3 Enable (0 or 6 for success) = "+String(result));
  55.    
  56. } // end setup
  57.  
  58. void loop() {
  59.   int sensor1_Max = 0.0;          // Define maximum signal as 0 to start
  60.   int sensor1_Min = 1024.0;       // Define minimum signal as 1024 to start
  61.   int sensor2_Max = 0.0;          // Define maximum signal as 0 to start
  62.   int sensor2_Min = 1024.0;       // Define minimum signal as 1024 to start
  63.  
  64.   // Sample sensor1 and sensor 2
  65.   unsigned long start= millis();         // Set start of sample window
  66.  
  67.   while (millis() - start < SAMPLE_WINDOW) {
  68.    T1_sample = analogRead(T1);
  69.    T2_sample = analogRead(T2);
  70.    
  71.         if (T1_sample > sensor1_Max)
  72.           {
  73.             sensor1_Max = T1_sample;          // save just the max levels
  74.           }
  75.         else if (T1_sample < sensor1_Min)
  76.           {
  77.             sensor1_Min = T1_sample;          // save just the min levels
  78.           }
  79.  
  80.  
  81.          if (T2_sample > sensor2_Max)
  82.           {
  83.             sensor2_Max = T2_sample;          // save just the max levels
  84.           }
  85.         else if (T2_sample < sensor2_Min)
  86.           {
  87.             sensor2_Min = T2_sample;          // save just the min levels
  88.           }  
  89.   } // end while
  90.  
  91.   // Print Max volts detected from sensor 1
  92.   float sensor1_max_volt = (sensor1_Max * REFERENCE_VOLTAGE)/1024;
  93.   Serial.println("Sensor1 Volts Reading = "+String(sensor1_max_volt));
  94.  
  95.   // Print Peak voltage detected from sensor 2
  96.   float sensor2_peak_volt = (sensor2_Max - sensor2_Min) * REFERENCE_VOLTAGE/1024;
  97.   Serial.println("Sensor2 Peak Volts Reading = "+String(sensor2_peak_volt));
  98.  
  99.   delay(250);
  100. } // end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement