Advertisement
CuriousScientist

RF 433 Transmitter code

Apr 4th, 2020
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found this code useful, please subscribe to my channel: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //It took several hours for me to prepare everything, but subscription is just a click for you. Thank you!
  3. //This code belongs to the following tutorial video: https://youtu.be/lGR8yVVkEL0
  4. //TRANSMITTER
  5.  
  6. #include <RH_ASK.h> //RadioHead Amplitude Shift Keying Library
  7.  
  8. #include <SPI.h> //SPI Library
  9. #include <Wire.h> //Wire Library (I2C)
  10.  
  11. //ASCII-based OLED library
  12. //Its huge advantage that it is _not_ graphical, so it is using much less memory
  13. #include "SSD1306Ascii.h"
  14. #include "SSD1306AsciiAvrI2c.h"
  15. // 0X3C+SA0 - 0x3C or 0x3D
  16. #define I2C_ADDRESS 0x3C //Address
  17. #define RST_PIN -1 //For OLED with no reset pin
  18.  
  19. SSD1306AsciiAvrI2c display; //Instantiating a display
  20. RH_ASK rf_driver; //Instatiating a rf_driver for the transmitter
  21.  
  22. int soundLevel = 0; //variable for the microphone's output
  23. int triggerLevel = 0;
  24. int AVGsoundLevel = 0;
  25. int messageNumber = 0; //1 = triggered, 0 = reset (receiver will wait for a new trigger).
  26. bool triggered = false; //we change this based on the status
  27.  
  28. //PINS
  29. int microphonePin = A6;
  30. int redLED = 5; // On, when triggered (Triggered)
  31. int greenLED = 6; //on when not triggered (Waiting)
  32. int calibratePin = 2; //to save the triggel level
  33.  
  34. void setup()
  35. {
  36.  
  37.   //Initializing the display
  38.   #if RST_PIN >= 0
  39.   display.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
  40.   #else // RST_PIN >= 0
  41.   display.begin(&Adafruit128x64, I2C_ADDRESS);
  42.   #endif // RST_PIN >= 0
  43.   display.setFont(System5x7); //font type
  44.   display.set1X(); //font size (small)
  45.   display.clear();
  46.   //-----------------------------------------------------    
  47.   // Initialize ASK Object
  48.   rf_driver.init();
  49.   //-----------------------------------------------------
  50.   //Pins. By default, green will be on.
  51.   pinMode(calibratePin, INPUT_PULLUP); //use the internal pullup
  52.   pinMode(redLED, OUTPUT);
  53.   pinMode(greenLED, OUTPUT);
  54.  
  55.   digitalWrite(redLED, LOW);
  56.   digitalWrite(greenLED, HIGH);
  57.  
  58.   attachInterrupt(digitalPinToInterrupt(calibratePin), triggerSoundLevel, RISING); //L -> H, button for calibration
  59.  
  60. }
  61.  
  62. void loop()
  63. {    
  64.     PrintScreen();    //Print the info on the screen
  65.    
  66.     MeasureSound();   //Measure the sound
  67.    
  68. }
  69.  
  70.  
  71. void PrintScreen()
  72. {  
  73.   display.clear();  //clear display
  74.   display.setCursor(0, 0); //start in the 0 position  
  75.   //----------------------------
  76.   display.print("Level: ");  
  77.   display.println(AVGsoundLevel);  
  78.   display.println();
  79.   //----------------------------
  80.   display.print("Trigger level: ");  
  81.   display.println(triggerLevel);
  82.   //----------------------------
  83.   display.println();
  84.   display.print("Status: ");
  85.   if(triggered == false)
  86.   {
  87.     //This gets printed if we do not cross the trigger value
  88.     display.println("Waiting");
  89.     messageNumber = 0; //sends a 0 to the receiver
  90.     SendMessage(messageNumber);
  91.     digitalWrite(redLED, LOW); //only green is on while waiting
  92.     digitalWrite(greenLED, HIGH);
  93.    
  94.   }
  95.   else
  96.   { //triggered == true
  97.     //This gets printed if we cross the trigger value
  98.     display.println("Triggered");
  99.     messageNumber = 1; //sends a 0 to the receiver
  100.     SendMessage(messageNumber);
  101.     digitalWrite(redLED, HIGH); //red is on when triggered and green is off
  102.     digitalWrite(greenLED, LOW);    
  103.   }
  104.        
  105. }
  106.  
  107. void MeasureSound()
  108. {
  109.   soundLevel = 0; //Reset it, before filling it up
  110.   AVGsoundLevel = 0; //Reset it, before filling it up
  111.  
  112.   //Let's measure it for some time and create an average
  113.   for(int i = 0; i < 10; i++)
  114.   {
  115.     soundLevel += analogRead(microphonePin);
  116.     delay(200);
  117.   }
  118.   AVGsoundLevel = soundLevel / 10; //calculate average
  119.  
  120.   if(AVGsoundLevel < (triggerLevel-100)) //if there is less value, we flip the value of the variable
  121.   {
  122.     triggered = true; //set the bool to true
  123.   }
  124.   else
  125.   {
  126.     triggered = false; //set the bool to false
  127.   }
  128.  
  129. }
  130.  
  131. void triggerSoundLevel()
  132. {
  133.   soundLevel = 0;   //Reset it, before filling it up
  134.   triggerLevel = 0; //Reset
  135.  
  136.   for(int i = 0; i < 10; i++)
  137.   {
  138.     soundLevel += analogRead(microphonePin);
  139.     delay(200);    
  140.   }
  141.     triggerLevel = soundLevel / 10;  //calculate average
  142.    
  143.     soundLevel = 0; //Reset it, again, otherwise, it goes to weird levels in the measure()    
  144.    
  145. }
  146.  
  147. void SendMessage(int messageNumber)
  148. {
  149.     rf_driver.send((uint8_t *)&messageNumber, sizeof(messageNumber)); //send the message through the RF module    
  150.     rf_driver.waitPacketSent();  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement