RuiViana

EP_Receptor

Jan 18th, 2018
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //HC-12 Momentary Button Receive
  2. //Autor Tom Heylen tomtomheylen.com
  3. #include <SoftwareSerial.h>
  4. SoftwareSerial mySerial(2, 3); // RX, TX
  5. int ledPin = 13;
  6. int ledPin2 = 10;
  7. //--------------------
  8. void setup()
  9. {
  10.   mySerial.begin(9600);
  11.   pinMode(ledPin, OUTPUT);
  12.   pinMode(ledPin2, OUTPUT);
  13. }
  14. //--------------------
  15. void loop() {
  16.  
  17.   if (mySerial.available() > 1)
  18.   {
  19.     int input = mySerial.parseInt();            //read serial input and convert to integer (-32,768 to 32,767)
  20.     if (input == 1111)                          //if on code is received
  21.     {
  22.       digitalWrite(ledPin, HIGH);               //turn LED on
  23.     }
  24.     if (input == 0000)                          //if off code is received
  25.     {
  26.       digitalWrite(ledPin, LOW);                //turn LED off
  27.     }
  28.     if (input == 3333)                          //if on code is received
  29.     {
  30.       digitalWrite(ledPin2, HIGH);              //turn LED on
  31.     }
  32.     if (input == 2222)                          //if off code is received
  33.     {
  34.       digitalWrite(ledPin2, LOW);               //turn LED off
  35.     }
  36.   }
  37.   mySerial.flush();                             //clear the serial buffer for unwanted inputs
  38.   delay(20);                                    //delay little for better serial communication
  39. }
RAW Paste Data