#include <VirtualWire.h>
const int transmit_pin = 12;
int button1 = 2;
int button2 = 3;
int DL=1000;
void setup(){
// Initialise the IO and ISR
vw_set_tx_pin(transmit_pin);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
vw_setup(2000); // Bits per sec
}
byte count = 1;
void loop(){
char msg1[2] = {'h'};
char msg2[2] = {'j'};
if(digitalRead(button1) == 1){
vw_send((uint8_t *)msg1, 1); // change this number according to the sensor values
vw_wait_tx(); // Wait until the whole message is gone
delay(DL);
}
if(digitalRead(button2) == 1){
vw_send((uint8_t *)msg2, 1); // change this number according to the sensor values
vw_wait_tx(); // Wait until the whole message is gone
delay(DL);
}
else
digitalWrite(button1,LOW);
digitalWrite(button2,LOW);
}
Receiver Sketch:
#include <VirtualWire.h>
const int receive_pin = 11;
int LED1 = 2;
int LED2 = 3;
int DT=1000;
void setup(){
delay(DT);
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop(){
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)){ // Non-blocking
int i;
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++){
char c = (buf);
if( c == 'h'){
digitalWrite(LED1 , HIGH);
Serial.print(c);
Serial.print(' ');
delay(DT);
}
if( c == 'j'){
digitalWrite(LED2 , HIGH);
Serial.print(c);
Serial.print(' ');
delay(DT);
}
else
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
}
}
}