Paso 6: Parte 2: codificación
CÓDIGO DEL TRANSMISOR
------------------------------------------------------------------------
#include < VirtualWire.h >
const int led_pin = 13;
const int transmit_pin = 9;
const int sensor_pin = 7;
int sensor_value;
void setup() {}
vw_set_tx_pin(transmit_pin);
vw_setup(2000);
pinMode (led_pin, salida);
pinMode(sensor_pin,INPUT);
}
void loop() {}
sensor_value = digitalRead(sensor_pin);
char mensaje [3] = {'o', 'f', 'f'};
Si (sensor_value == 1) {}
MSG [0] = ' o ';
MSG [1] = ' n ';
MSG [2] = '#';
}
digitalWrite (led_pin, HIGH);
vw_send ((uint8_t *) msg, 3);
vw_wait_tx();
digitalWrite (led_pin, LOW);
Delay(1000);
}
-----------------------------------------------------------------------------
CÓDIGO DE RECEPTOR
----------------------------------------------------------------------------
#include < VirtualWire.h >
const int motion_pin = 10;
const int led_pin = 13;
const int transmit_pin = 9;
const int receive_pin = 6;
void setup() {}
Delay(1000);
Serial.Begin(9600);
Serial.println("Setup");
vw_set_rx_pin(receive_pin);
vw_setup(2000);
vw_rx_start();
pinMode (led_pin, salida);
pinMode (motion_pin, salida);
}
void loop() {}
uint8_t buf [VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
Si (vw_get_message (buf & buflen)) {}
digitalWrite (led_pin, HIGH);
Serial.Print ("tienes:");
para (int i = 0; i < buflen; i ++) {}
Serial.Print(Char(BUF[i]));
Serial.Print(' ');
}
{if(Char(BUF[2])=='#')}
digitalWrite (motion_pin, HIGH);
Delay(1000);
digitalWrite (motion_pin, bajo);
}
Serial.println();
digitalWrite (led_pin, LOW);
}
}
-------------------------------------------------------------