Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If you found this code useful, please subscribe to my channel: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
- //It took several hours for me to prepare everything, but subscription is just a click for you. Thank you!
- //This code belongs to the following tutorial video: https://youtu.be/lGR8yVVkEL0
- //TRANSMITTER
- #include <RH_ASK.h> //RadioHead Amplitude Shift Keying Library
- #include <SPI.h> //SPI Library
- #include <Wire.h> //Wire Library (I2C)
- //ASCII-based OLED library
- //Its huge advantage that it is _not_ graphical, so it is using much less memory
- #include "SSD1306Ascii.h"
- #include "SSD1306AsciiAvrI2c.h"
- // 0X3C+SA0 - 0x3C or 0x3D
- #define I2C_ADDRESS 0x3C //Address
- #define RST_PIN -1 //For OLED with no reset pin
- SSD1306AsciiAvrI2c display; //Instantiating a display
- RH_ASK rf_driver; //Instatiating a rf_driver for the transmitter
- int soundLevel = 0; //variable for the microphone's output
- int triggerLevel = 0;
- int AVGsoundLevel = 0;
- int messageNumber = 0; //1 = triggered, 0 = reset (receiver will wait for a new trigger).
- bool triggered = false; //we change this based on the status
- //PINS
- int microphonePin = A6;
- int redLED = 5; // On, when triggered (Triggered)
- int greenLED = 6; //on when not triggered (Waiting)
- int calibratePin = 2; //to save the triggel level
- void setup()
- {
- //Initializing the display
- #if RST_PIN >= 0
- display.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
- #else // RST_PIN >= 0
- display.begin(&Adafruit128x64, I2C_ADDRESS);
- #endif // RST_PIN >= 0
- display.setFont(System5x7); //font type
- display.set1X(); //font size (small)
- display.clear();
- //-----------------------------------------------------
- // Initialize ASK Object
- rf_driver.init();
- //-----------------------------------------------------
- //Pins. By default, green will be on.
- pinMode(calibratePin, INPUT_PULLUP); //use the internal pullup
- pinMode(redLED, OUTPUT);
- pinMode(greenLED, OUTPUT);
- digitalWrite(redLED, LOW);
- digitalWrite(greenLED, HIGH);
- attachInterrupt(digitalPinToInterrupt(calibratePin), triggerSoundLevel, RISING); //L -> H, button for calibration
- }
- void loop()
- {
- PrintScreen(); //Print the info on the screen
- MeasureSound(); //Measure the sound
- }
- void PrintScreen()
- {
- display.clear(); //clear display
- display.setCursor(0, 0); //start in the 0 position
- //----------------------------
- display.print("Level: ");
- display.println(AVGsoundLevel);
- display.println();
- //----------------------------
- display.print("Trigger level: ");
- display.println(triggerLevel);
- //----------------------------
- display.println();
- display.print("Status: ");
- if(triggered == false)
- {
- //This gets printed if we do not cross the trigger value
- display.println("Waiting");
- messageNumber = 0; //sends a 0 to the receiver
- SendMessage(messageNumber);
- digitalWrite(redLED, LOW); //only green is on while waiting
- digitalWrite(greenLED, HIGH);
- }
- else
- { //triggered == true
- //This gets printed if we cross the trigger value
- display.println("Triggered");
- messageNumber = 1; //sends a 0 to the receiver
- SendMessage(messageNumber);
- digitalWrite(redLED, HIGH); //red is on when triggered and green is off
- digitalWrite(greenLED, LOW);
- }
- }
- void MeasureSound()
- {
- soundLevel = 0; //Reset it, before filling it up
- AVGsoundLevel = 0; //Reset it, before filling it up
- //Let's measure it for some time and create an average
- for(int i = 0; i < 10; i++)
- {
- soundLevel += analogRead(microphonePin);
- delay(200);
- }
- AVGsoundLevel = soundLevel / 10; //calculate average
- if(AVGsoundLevel < (triggerLevel-100)) //if there is less value, we flip the value of the variable
- {
- triggered = true; //set the bool to true
- }
- else
- {
- triggered = false; //set the bool to false
- }
- }
- void triggerSoundLevel()
- {
- soundLevel = 0; //Reset it, before filling it up
- triggerLevel = 0; //Reset
- for(int i = 0; i < 10; i++)
- {
- soundLevel += analogRead(microphonePin);
- delay(200);
- }
- triggerLevel = soundLevel / 10; //calculate average
- soundLevel = 0; //Reset it, again, otherwise, it goes to weird levels in the measure()
- }
- void SendMessage(int messageNumber)
- {
- rf_driver.send((uint8_t *)&messageNumber, sizeof(messageNumber)); //send the message through the RF module
- rf_driver.waitPacketSent();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement