Advertisement
GUPPYYYY

audio sensor

Oct 30th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int soundPin = A4;
  2.  
  3. int highest = 0;
  4.  
  5. int previous = 0;
  6.  
  7. void setup() {
  8.  
  9.   // put your setup code here, to run once:
  10.  
  11.   Serial.begin(9600);
  12.  
  13.   pinMode(soundPin, INPUT);
  14.  
  15. }
  16. void loop()
  17. {
  18.  
  19.   int runningTotal = 0;
  20.  
  21.   for(int i = 0; i < 100; i++){
  22.  
  23.      runningTotal += analogRead(soundPin);
  24.  delay(5);
  25.  
  26.   }
  27.  
  28.   int averageSound = runningTotal / 100;
  29.  
  30.   Serial.println(averageSound);
  31.  
  32.  
  33.  
  34.   if(averageSound > highest){
  35.  
  36.     highest = averageSound;
  37.  
  38.     previous = averageSound;
  39.  
  40.     Serial.println("Highest sound recorded!");
  41.  
  42.     return;
  43.  
  44.   }
  45. if(averageSound > previous){
  46.  
  47.     Serial.println("Higer than last time");
  48.  
  49.   }else if(averageSound < previous){
  50.  
  51.     Serial.println("Lower than last time");
  52.  
  53.   } else{
  54.  
  55.     Serial.println("Same as last time");
  56.  
  57.   }
  58.   previous = averageSound;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement