Krejzi_Dark

max9814_test

Sep 29th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******************************************************************************
  2.  * sound_detector_demo.ino
  3.  * Sound detector sample sketch
  4.  * Byron Jacquot @ SparkFun Electronics
  5.  * February 19, 2014
  6.  * https://github.com/sparkfun/Sound_Detector
  7.  *
  8.  * This sketch demonstrates the use of the Sparkfun Sound Detector board.
  9.  *
  10.  * The Sound Detector is a small board that combines a microphone and some
  11.  * processing circuitry.  It provides not only an audio output, but also a
  12.  * binary indication of the presence of sound and an analog representation
  13.  * of it's amplitude.  
  14.  *
  15.  * This sketch demonstrates two different modes of usage for the Sound
  16.  * Detector.  The gate output (a binary indication that is high when sound
  17.  * is present, and low when conditions are quiet) is used to fire a pin-change
  18.  * ISR, which lights an LED when the sound is present.  The envelope output
  19.  * (an analog voltage to rises to indicate the amplitude of the sound) is
  20.  * sampled in the loop(), and it prints an indication of the level to the
  21.  * serial terminal.
  22.  *
  23.  * For more details about the Sound Detector, please check the hookup guide.
  24.  *
  25.  * Connections:
  26.  * The Sound Detector is connected to the Adrduino as follows:
  27.  * (Sound Detector -> Arduino pin)
  28.  * GND → GND
  29.  * VCC → 5V
  30.  * Gate → Pin 2
  31.  * Envelope → A0
  32.  *
  33.  * Resources:
  34.  * Additional library requirements: none
  35.  *
  36.  * Development environment specifics:
  37.  * Using Arduino IDe 1.0.5
  38.  * Tested on Redboard, 3.3v/8MHz and 5v/16MHz ProMini hardware.
  39.  *
  40.  * This code is beerware; if you see me (or any other SparkFun employee) at the
  41.  * local, and you've found our code helpful, please buy us a round!
  42.  *
  43.  * Distributed as-is; no warranty is given.
  44.  ******************************************************************************/
  45.  
  46.  // Define hardware connections
  47. #define PIN_GATE_IN 2
  48. #define IRQ_GATE_IN  0
  49. #define PIN_LED_OUT 13
  50. #define PIN_ANALOG_IN A0
  51.  
  52. // soundISR()
  53. // This function is installed as an interrupt service routine for the pin
  54. // change interrupt.  When digital input 2 changes state, this routine
  55. // is called.
  56. // It queries the state of that pin, and sets the onboard LED to reflect that
  57. // pin's state.
  58. void soundISR()
  59. {
  60.   int pin_val;
  61.  
  62.   pin_val = digitalRead(PIN_GATE_IN);
  63.   digitalWrite(PIN_LED_OUT, pin_val);  
  64. }
  65.  
  66. void setup()
  67. {
  68.   Serial.begin(9600);
  69.  
  70.   //  Configure LED pin as output
  71.   pinMode(PIN_LED_OUT, OUTPUT);
  72.  
  73.   // configure input to interrupt
  74.   pinMode(PIN_GATE_IN, INPUT);
  75.   attachInterrupt(IRQ_GATE_IN, soundISR, CHANGE);
  76.  
  77.   // Display status
  78.   Serial.println("Initialized");
  79. }
  80.  
  81. void loop()
  82. {
  83.   int value;
  84.  
  85.   // Check the envelope input
  86.   value = analogRead(PIN_ANALOG_IN);
  87.  
  88.   // Convert envelope value into a message
  89.   Serial.print("Status: ");
  90.   if(value <= 10)
  91.   {
  92.     Serial.println("Quiet.");
  93.   }
  94.   else if( (value > 10) && ( value <= 30) )
  95.   {
  96.     Serial.println("Moderate.");
  97.   }
  98.   else if(value > 30)
  99.   {
  100.     Serial.println("Loud.");
  101.   }
  102.   Serial.print("value: ");
  103.   Serial.println(value);
  104.   // pause for 1 second
  105.   delay(1000);
  106. }
Add Comment
Please, Sign In to add comment