Anda di halaman 1dari 3

//

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

SparkFun Electronics 2011


NPoole
RF ASK Transmitter/Reciever Example
This code depends on the VirtualWire Library for Arduino and is
based on the example code provided by Mike McCauley (mikem@open.com.au)
See VirtualWire.h for detailed API docs.
This example shows how to use the VirtualWire library to send and receive
simple messages and use them to control digital I/O pins. Buttons are
connected to the transmitting Arduino on pins 8-11 (to ground, internal 20k
pull-up resistors are set in the code) and in the same fashion,
LEDs are connected to the recieving Arduino on pins 8-11. When a button is
pressed on the transmitter, the corresponding LED will light on the reciever.
This document contains both transmitter and reciever code, simply de-comment
the piece of code you need to use.

/* TRANSMITTER CODE
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000);
// Bits per sec
vw_set_tx_pin(3);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
}
void loop()
{
char *msg;
if(digitalRead(8) == LOW){
char *msg = "1";
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);}
if(digitalRead(9) == LOW){
char *msg = "2";

digitalWrite(13, true); // Flash a light to


vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message
digitalWrite(13, false);}
if(digitalRead(10) == LOW){
char *msg = "3";
digitalWrite(13, true); // Flash a light to
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message
digitalWrite(13, false);}
if(digitalRead(11) == LOW){
char *msg = "4";
digitalWrite(13, true); // Flash a light to
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message
digitalWrite(13, false);}
}

show transmitting
is gone

show transmitting
is gone

show transmitting
is gone

END TRANSMITTER CODE */


//****************************************************************************
***************//
/* RECIEVER CODE
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000);
// Bits per sec
vw_set_rx_pin(2);
vw_rx_start();
// Start the receiver PLL running
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(13, true); // Flash a light to show received good message

// Message with a good checksum received, dump it.


Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]);
if(buf[i] == '1'){digitalWrite(8, HIGH);}
if(buf[i] == '2'){digitalWrite(9, HIGH);}
if(buf[i] == '3'){digitalWrite(10, HIGH);}
if(buf[i] == '4'){digitalWrite(11, HIGH);}
Serial.print(" ");
}
Serial.println("");
digitalWrite(13, false);
}
}

Anda mungkin juga menyukai