Advertisement
RuiViana

AG_Receptor

Jan 30th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. //  http://buildbot.com.br/blog/comunicacao-wireless-com-o-modulo-nrf24l01/
  2. //  Programa : Teste NRF24L01 - Receptor - Led
  3. //  Autor : Adilson Thomsen
  4.  
  5. #include <SPI.h>
  6. #include "nRF24L01.h"
  7. #include "RF24.h"
  8. int recebidos[1];                         // Armazena os dados recebidos
  9. RF24 radio(9, 10);                        // Inicializa a placa nos pinos 9 (CE) e 10 (CS) do Arduino
  10. const uint64_t pipe = 0xE14BC8F482LL;     // Define o endereco para comunicacao entre os modulos
  11. int LED1 = 5;                             // Define os pinos dos leds
  12. int LED2 = 3;
  13. int LED3 = 4;
  14. //-----------------------------------
  15. void setup()
  16. {
  17.   pinMode(LED1, OUTPUT);                    // Define os pinos dos leds como saida
  18.   pinMode(LED2, OUTPUT);
  19.   pinMode(LED3, OUTPUT);
  20.   Serial.begin(57600);                      // Inicializa a serial
  21.   radio.begin();                            // Inicializa a comunicacao
  22.   radio.openReadingPipe(1, pipe);           // Entra em modo de recepcao
  23.   radio.startListening();
  24.   Serial.println("Radio inicializado para receber ");
  25. }
  26. //-----------------------------------
  27. void loop()
  28. {
  29.   if (radio.available())                    // Verifica se ha sinal de radio
  30.   {
  31.     bool done = false;
  32.     while (!done)
  33.     {
  34.       done = radio.read(recebidos, 1);
  35.       Serial.print("Dados recebidos : ");
  36.       Serial.println(recebidos[0]);
  37.       if (recebidos[0] == 1)                // Se recebeu o numero 1, acende o LED1
  38.       {
  39.         delay(10);
  40.         digitalWrite(LED1, HIGH);
  41.         digitalWrite(LED3, LOW);
  42.       }
  43.       else
  44.       {
  45.         digitalWrite(LED1, LOW);
  46.       }
  47.       if (recebidos[0] == 2)                // Se recebeu o numero 2, acende o LED2
  48.       {
  49.         delay(10);
  50.         digitalWrite(LED2, HIGH);
  51.         digitalWrite(LED3, LOW);
  52.       }
  53.       else
  54.       {
  55.         digitalWrite(LED2, LOW);
  56.       }
  57.       delay(100);
  58.     }
  59.   }
  60.   else
  61.   {
  62.     digitalWrite(LED3, HIGH);
  63.     //    Serial.println("Aguardando dados...");
  64.   }
  65. }
  66. //  http://shanes.net/another-nrf24l01-sketch-string-sendreceive/
  67. /*
  68.  #include <nRF24L01.h>
  69. #include <RF24.h>
  70. #include <RF24_config.h>
  71. #include <SPI.h>
  72.  
  73. /*
  74. This sketch receives strings from sending unit via nrf24
  75. and prints them out via serial.  The sketch waits until
  76. it receives a specific value (2 in this case), then it
  77. prints the complete message and clears the message buffer.
  78. */
  79. /*
  80. int messageLength = 12;
  81. int msg[1];
  82. RF24 radio(9,10);
  83. const uint64_t pipe = 0xE8E8F0F0E1LL;
  84. int lastmsg = 1;
  85. String theMessage = "";
  86. void setup(void){
  87.   Serial.begin(9600);
  88.   radio.begin();
  89.   radio.openReadingPipe(1,pipe);
  90.   radio.startListening();
  91. }
  92. void loop(void){
  93.   if (radio.available()){
  94.     bool done = false;  
  95.       done = radio.read(msg, 1);
  96.       char theChar = msg[0];
  97.       if (msg[0] != 2){
  98.         theMessage.concat(theChar);
  99.         }
  100.       else {
  101.        if (theMessage.length() == messageLength) {
  102.        Serial.println(theMessage);
  103.        }
  104.        theMessage= "";
  105.       }
  106.    }
  107. }
  108.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement