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
- //RECEIVER
- #include <RH_ASK.h>//RadioHead Amplitude Shift Keying Library
- #include <SPI.h> // SPI Library
- #include <Wire.h> //Wire Library (I2C)
- RH_ASK rf_driver; //Instatiating a rf_driver for the receiver
- //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"
- #define I2C_ADDRESS 0x3C //Address
- #define RST_PIN -1 //For OLED with no reset pin
- SSD1306AsciiAvrI2c display;
- // Set buffer to size of expected message
- uint8_t buf[24];
- uint8_t buflen = sizeof(buf);
- String receivedString; //String for long messages
- //REC pin = D11
- int redLED = 5; //on, when it is too loud
- int greenLED = 6; //on when it is OK
- void setup()
- {
- // Initialize ASK Object
- rf_driver.init();
- #if RST_PIN >= 0
- display.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
- #else // RST_PIN >= 0
- display.begin(&Adafruit128x64, I2C_ADDRESS);
- #endif // RST_PIN >= 0
- // Call oled.setI2cClock(frequency) to change from the default frequency.
- display.setFont(System5x7);
- display.set1X();
- display.clear();
- //Managing the LEDs
- pinMode(redLED, OUTPUT);
- pinMode(greenLED, OUTPUT);
- digitalWrite(redLED, LOW);
- digitalWrite(greenLED, HIGH);
- //----------------------------
- //Serial.begin(9600);
- //Serial.println("Receiver started");
- }
- void loop()
- {
- // Check if received packet is correct size
- if (rf_driver.recv(buf, &buflen))
- {
- for (int i = 0; i < buflen-1; i++)
- {
- receivedString += String(buf[i]); //building up a string from the received data
- }
- PrintScreen(); //print to OLED
- receivedString=""; //reset to zero, so we don't mix the messages
- }
- }
- void PrintScreen()
- {
- display.clear();
- display.setCursor(0, 0);
- display.println("Status: ");
- display.println();
- if(receivedString == "0")
- {
- display.println("Waiting...");
- digitalWrite(redLED, LOW);
- digitalWrite(greenLED, HIGH); //Only green is on, while we are waiting
- }
- if(receivedString == "1")
- {
- display.println("Check device!");
- digitalWrite(redLED, HIGH); //Red goes on, when the trigger level is crossed on the transmitter side
- digitalWrite(greenLED, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement