Last active
July 30, 2019 01:16
-
-
Save futureshocked/e8981596258c97cca2d4829ba943d0d5 to your computer and use it in GitHub Desktop.
Grove For Busy People - Project 5 - loud noise detector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 11.40 - Project 5 - Grove loud noise detector | |
| This sketch implements a loud noise detector. It uses a simple soundsensor to pick up | |
| noise. It calculates the volume of the noise, and if it is above a certain threshold, | |
| it switches on an LED and increases the loud noise counter by 1. | |
| The threshold is configurable via a potentiometer. | |
| The sketch displays its operation parameters on an LCD screen. | |
| Components | |
| ---------- | |
| - Grove Base Shield | |
| - An Arduino Uno compatible board (such as Arduino/Genuino Uno or Seeeduino) | |
| - Grove LCD RGB Backlight module | |
| - Grove potentiometer | |
| - Grove LED kit with one LED | |
| - Grove sound sensor | |
| - Four Grove cables | |
| Arduino IDE | |
| ---------- | |
| - Wire.h (part of the Arduino IDE) | |
| - rgb_lcd.h (Get it from https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight) | |
| Connections | |
| ---------- | |
| Use a Grove cable to connect the LED module to Grove connector D4. | |
| Use a Grove cable to connect the LCD module to Grove connector I2C. | |
| Use a Grove cable to connect the sound sensor to Grove connector AQ. | |
| Other information | |
| ---------- | |
| - Use this sketch along side the video lecture 11.40 of Grove For Busy People | |
| - Grove LCD RGB Backlight: https://txplo.re/0c9fb | |
| - Grove sound sensor module: https://txplo.re/e171e | |
| - Grove LED module: https://txplo.re/afed7 | |
| Github Gist | |
| ----------- | |
| <script src="https://gist.github.com/futureshocked/e8981596258c97cca2d4829ba943d0d5.js"></script> | |
| https://gist.github.com/futureshocked/e8981596258c97cca2d4829ba943d0d5 | |
| For course information, please go to https://techexplorations.com/product/grove-for-busy-people/ | |
| Created on July 10 2019 by Peter Dalmaris | |
| */ | |
| #include <Wire.h> | |
| #include "rgb_lcd.h" | |
| rgb_lcd lcd; | |
| const int soundSensorPin = 0; // Analog pin 0 is available through Grove connector A0 | |
| const int potPin = 1; // Analog pin 1 1s available through Grove connector Al | |
| const int ledPin = 4; | |
| int noise_threshold; // Get this from the potentiometer in Grove connector A1 | |
| int noise_window = 1000; // The number of milliseconds within which noise counting takes place | |
| unsigned long last_noise_millis; // Use this variable to determine if the peak noise is | |
| // within the counting window. | |
| unsigned long new_noise_millis = 0; | |
| int noise_counter = 0; // This variable keeps the total noises within the current noise period | |
| byte armsDown[8] = { | |
| 0b00100, | |
| 0b01010, | |
| 0b00100, | |
| 0b00100, | |
| 0b01110, | |
| 0b10101, | |
| 0b00100, | |
| 0b01010 | |
| }; | |
| byte armsUp[8] = { | |
| 0b00100, | |
| 0b01010, | |
| 0b00100, | |
| 0b10101, | |
| 0b01110, | |
| 0b00100, | |
| 0b00100, | |
| 0b01010 | |
| }; | |
| void setup() | |
| { | |
| Serial.begin(9600); | |
| lcd. begin(16, 2); | |
| lcd.createChar(0, armsDown); // create a new cnaracter | |
| lcd.createChar(1, armsUp); // create a new cnaracter | |
| lcd.setCursor(16, 2); | |
| lcd.clear(); | |
| lcd.write("Make noise!"); | |
| lcd.setCursor(8, 1); | |
| lcd.write((unsigned char)0); | |
| last_noise_millis = millis(); | |
| } | |
| void loop() | |
| { | |
| noise_threshold = analogRead(potPin) ; | |
| display_threshold(); | |
| long loudness = listen(); | |
| if (determine_noise(loudness)) | |
| { | |
| new_noise_millis = millis(); | |
| delay(20); // Debouncing to prevent incorrect noise detection | |
| if (new_noise_millis - last_noise_millis < noise_window) | |
| noise_counter++; | |
| else | |
| noise_counter = 1; // This is a new noise sequence | |
| last_noise_millis = new_noise_millis; | |
| display_noise_count(); | |
| noiseping(loudness); // Turn on the LED | |
| } | |
| else | |
| { | |
| if (millis() - new_noise_millis > noise_window) // Leave the LED on for at least noise_window millis | |
| notnoiseping(); | |
| } | |
| } | |
| long listen() { | |
| long sum = 0; | |
| for (int i = 0; i < 32; i++) | |
| sum += analogRead(soundSensorPin); | |
| sum >>= 5; // The >> operator is “bitshift right’. It will shift the value stored in sum by 5 bits to the right. | |
| // This scales down the sum value. | |
| // The max possible value is 32 * 1023 = 32736. | |
| // Why shift to the right by 5 bits, and not, say, 6 or 7? | |
| // With bithshift right by 5 bits, this becomes 1023, which fits nicely within the 10-bit limit of | |
| // the Arduino ADC converter. | |
| Serial.println(sum); | |
| delay(10); | |
| return sum; | |
| } | |
| bool determine_noise(long loudness) | |
| { | |
| lcd.setCursor(5, 1); | |
| lcd.write(" "); | |
| lcd.setCursor(5, 1); | |
| lcd.print(loudness); | |
| if (loudness > noise_threshold ) | |
| return true; | |
| else | |
| return false; | |
| } | |
| void noiseping(long loudness) | |
| { | |
| lcd.setCursor(9, 1); | |
| lcd.write((unsigned char)1); // Hands up | |
| digitalWrite(ledPin, HIGH); | |
| lcd.setCursor(0, 1); | |
| lcd.print(" "); // Clear the loudness value | |
| lcd.setCursor(0, 1); | |
| lcd.print(loudness); // Show loudness value | |
| } | |
| void notnoiseping() | |
| { | |
| lcd.setCursor(9, 1); | |
| lcd.write((unsigned char)0); // Hands down | |
| digitalWrite(ledPin, LOW); | |
| lcd.setCursor(0, 1); | |
| lcd.print(" "); // Clear the loudness value | |
| } | |
| void display_noise_count( ) { | |
| lcd.setCursor(12, 1); | |
| lcd.print(" "); | |
| lcd.setCursor(12, 1); | |
| lcd.print(noise_counter); // Print the threshold value | |
| lcd.setCursor(12, 0); | |
| lcd.print(" "); | |
| lcd.setCursor(12, 0); | |
| lcd.print(noise_threshold); | |
| } | |
| void display_threshold() { | |
| // Print the threshold value | |
| lcd.setCursor(12, 9); | |
| lcd.print(" "); | |
| lcd.setCursor(12, 9); | |
| lcd.print(noise_threshold); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment