Advertisement
CreadPag

RECEPTOR ARDUINO

Oct 30th, 2016
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. RECEPTOR
  3.  
  4. Apaga el LED 13 si recibe el mensaje "Apagar"
  5. Enciende el LED 13 si recibe el mensaje "Encender"
  6.  
  7. */
  8.  
  9. #include <VirtualWire.h>
  10.  
  11. //Creamos un mensaje
  12. //La constante VW_MAX_MESSAGE_LEN viene definida en la libreria
  13. byte message[VW_MAX_MESSAGE_LEN];
  14. byte messageLength = VW_MAX_MESSAGE_LEN;
  15.  
  16. void setup()
  17. {
  18.   pinMode(13, OUTPUT); //Configuramos el pin 13
  19.  
  20.   Serial.begin(9600);//Iniciamos el Serial
  21.   Serial.println("Iniciando...");
  22.  
  23.   vw_setup(2000);
  24.   vw_rx_start();
  25. }
  26. void loop()
  27. {
  28.   if (vw_get_message(message, &messageLength))
  29.   {
  30.       if(comparar("Encender") == 0){
  31.         digitalWrite(13, HIGH);
  32.         Serial.write("LED Encendido\n");
  33.       }
  34.       else if(comparar("Apagar") == 0)
  35.       {
  36.         digitalWrite(13,LOW);
  37.         Serial.write("LED Apagado\n");
  38.       }
  39.  
  40.   }
  41. }
  42.  
  43. char comparar(char* cadena) {
  44.   //Esta funcion compara el string cadena con el mensaje recibido.
  45.   //Si son iguales, devuelve 1. Si no, devuelve 0.
  46.  
  47.   for(int i = 0; i<messageLength; i++)
  48.   {
  49.     if(message[i] != cadena[i])
  50.     {
  51.       return 1;
  52.     }
  53.   }
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement