Advertisement
RuiViana

AG_Transmissor

Jan 30th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. //  http://buildbot.com.br/blog/comunicacao-wireless-com-o-modulo-nrf24l01/
  2. //  Programa : Teste NRF24L01 - Emissor - Botoes
  3. //  Autor : Adilson Thomsen
  4.  
  5. #include <SPI.h>
  6. #include "nRF24L01.h"
  7. #include "RF24.h"
  8. int dados[1];                              // Armazena os dados a enviar
  9. int pos[1];
  10. RF24 radio(9, 10);                            // Inicializa a placa nos pinos 9 (CE) e 10 (CS) do Arduino
  11. const uint64_t pipe = 0xE14BC8F482LL;         // Define o endereco para comunicacao entre os modulos
  12. #define  pino_botao1  7                       // Define os pinos dos botoes
  13. #define  pino_botao2  2
  14. #define pot A0
  15. int valorLido = 0;
  16. //-----------------------------------
  17. void setup()
  18. {
  19.   pinMode(pino_botao1, INPUT_PULLUP);         // Define os botoes como entrada e ativa pull-up
  20.   pinMode(pino_botao2, INPUT_PULLUP);
  21.   Serial.begin(57600);                        // Inicializa a serial
  22.   Serial.println("Radio inicializado para transmitir ");
  23.   Serial.println(" ");
  24.   Serial.println("Pressione botao 1 ou 2 para iniciar a comunicacao...");
  25.   radio.begin();                              // Inicializa a comunicacao
  26.   radio.openWritingPipe(pipe);                // Entra em modo de transmissao
  27. }
  28. //-----------------------------------
  29. void loop()
  30. {
  31.   valorLido = analogRead(pot);
  32.   pos[0] =  valorLido;
  33.   Serial.println(pos[0]);
  34.   if (digitalRead(pino_botao1) == LOW)        // Envia o numero 1 caso o botao1 seja pressionado
  35.   {
  36.     delay(20);
  37.     if (digitalRead(pino_botao1) == LOW)      // Envia o numero 1 caso o botao1 seja pressionado
  38.     {
  39.       Serial.println("Botao 1 pressionado !");
  40.       dados[0] = 1;
  41.       radio.write(dados, 1);
  42.       //delay(200);
  43.     }
  44.   }
  45.   if (digitalRead(pino_botao2) == LOW)        // Envia o numero 2 caso o botao2 seja pressionado
  46.   {
  47.     delay(20);
  48.     if (digitalRead(pino_botao2) == LOW)
  49.     {
  50.       Serial.println("Botao 2 pressionado !");
  51.       dados[0] = 2;
  52.       radio.write(dados, 1);
  53.       //delay(200);
  54.     }
  55.   }
  56.   radio.write(pos, 1);
  57. }
  58.  
  59. //  http://shanes.net/another-nrf24l01-sketch-string-sendreceive/
  60. /*
  61.   #include <SPI.h>
  62.   #include <nRF24L01.h>
  63.   #include <RF24.h>
  64.   #include <RF24_config.h>
  65.  
  66.   /*
  67.   This sketch sends a string to a corresponding Arduino
  68.   with nrf24 attached.  It appends a specific value
  69.   (2 in this case) to the end to signify the end of the
  70.   message.
  71. */
  72. /*
  73.   int msg[1];
  74.   RF24 radio(9,10);
  75.   const uint64_t pipe = 0xE8E8F0F0E1LL;
  76.   //----------------------------------
  77.   void setup(void)
  78.   {
  79.   Serial.begin(9600);
  80.   radio.begin();
  81.   radio.openWritingPipe(pipe);
  82.   }
  83.   //----------------------------------
  84.   void loop(void)
  85.   {
  86.   String theMessage = "Hello there!";
  87.   int messageSize = theMessage.length();
  88.   for (int i = 0; i < messageSize; i++)
  89.   {
  90.     int charToSend[1];
  91.     charToSend[0] = theMessage.charAt(i);
  92.     radio.write(charToSend,1);
  93.   }
  94.   //send the 'terminate string' value...
  95.   msg[0] = 2;
  96.   radio.write(msg,1);
  97.   /*delay sending for a short period of time.  radio.powerDown()/radio.powerupp
  98.   //with a delay in between have worked well for this purpose(just using delay seems to
  99.   //interrupt the transmission start). However, this method could still be improved
  100.   as I still get the first character 'cut-off' sometimes. I have a 'checksum' function
  101.   on the receiver to verify the message was successfully sent.
  102. */
  103. /*
  104.   radio.powerDown();
  105.   delay(1000);
  106.   radio.powerUp();
  107.   }
  108. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement