Advertisement
digigram

RF_Receiver

Jun 23rd, 2014
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. /*
  2. rf_receiver
  3. This sketch will receive a rf signal, and decode it. If it conforms,
  4. it will proceed to toggle the state of a relay. Receiver on pin 3 (5)
  5. */
  6. #include <VirtualWire.h>
  7. byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
  8. byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
  9. int led = 1;
  10. int lstate = 0;
  11.  
  12. void setup()
  13. {
  14. //Serial.begin(9600);
  15. //Serial.println("Device is ready");
  16. // Initialize the IO and ISR
  17. vw_setup(2000); // Bits per sec
  18. vw_set_rx_pin(0);
  19. vw_rx_start(); // Start the receiver
  20. pinMode(led, OUTPUT);
  21. digitalWrite(led, HIGH);
  22. }
  23. void loop()
  24. {
  25. char currState;
  26. if (vw_get_message(message, &messageLength)) // Non-blocking
  27. {
  28. //Serial.print("Received: ");
  29. for (int i = 0; i < messageLength; i++)
  30. {
  31. if (message[0] == 'L' && message[1] == '0' && message[2] == '1'){
  32. if (lstate == 0) {
  33. digitalWrite(led, HIGH);
  34. lstate = 1;
  35. } else {
  36. digitalWrite(led, LOW);
  37. lstate = 0;
  38. }
  39. }
  40. else if (message[0] == 'M' && message[1] == '0' && message[2] == '1' ){
  41. digitalWrite(led, LOW);
  42. }
  43. else {
  44. //Serial.write(message[2]);
  45. }
  46. }
  47. //Serial.println();
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement