Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. /**
  2. * Find out if any audio is playing on a Line level cable
  3. *
  4. * A very simple way to find out if any audio is currently
  5. * playing, by reading the voltage from the cable. Line level
  6. * audio signals are much lower than 5V, so turn up the volume.
  7. *
  8. * Connect Ground and 1 Stero channel to the Arduino's GND and A0 pins.
  9. */
  10.  
  11. // These constants won't change. They're used to give names
  12. // to the pins used:
  13. const int analogInPin = A0;
  14.  
  15. int sensorValue = 0;
  16. int sum;
  17. int average;
  18.  
  19. byte buffer[20];
  20. int bufferPosition = 0;
  21.  
  22. void setup() {
  23. Serial.begin(9600);
  24. }
  25.  
  26. void loop() {
  27. // read the analog in value:
  28. sensorValue = analogRead(analogInPin);
  29.  
  30. // Write the current value into the current
  31. // ring buffer position
  32. buffer[bufferPosition] = sensorValue;
  33.  
  34. bufferPosition++;
  35. if (bufferPosition > 20) bufferPosition = 0;
  36.  
  37. // Calculate the average from the last 20 readings
  38. sum = 0;
  39. for (int i = 0; i < 20; i++) {
  40. sum += buffer[i];
  41. }
  42. average = sum / 20;
  43.  
  44. // print the results to the serial monitor:
  45. Serial.print("sensor = " );
  46. Serial.print(sensorValue);
  47. Serial.print("\t avg = ");
  48. Serial.print(average);
  49. if (average > 0) {
  50. Serial.print("\tAudio is playing");
  51. }
  52. Serial.println();
  53.  
  54. // wait 2 milliseconds before the next loop
  55. // for the analog-to-digital converter to settle
  56. // after the last reading:
  57. delay(2);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement