ตัวส่ง
ต่อ pin 10
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
// constants won't change. They're used here to set pin numbers:
const int buttonPinA = 4; // the number of the pushbutton pin
const int buttonPinB = 5;
const int buttonPinC = 6;
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonStateA = 0; // variable for reading the pushbutton status
int buttonStateB = 0;
int buttonStateC = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinA, INPUT);
pinMode(buttonPinB, INPUT);
pinMode(buttonPinC, INPUT);
mySwitch.enableTransmit(10);
}
void loop()
{
buttonStateA = digitalRead(buttonPinA);
buttonStateB = digitalRead(buttonPinB);
buttonStateC = digitalRead(buttonPinC);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonStateA == LOW)
{
// turn LED on:
mySwitch.send(13, 12);
delay(1000);
digitalWrite(ledPin, HIGH);
}
if (buttonStateB == LOW)
{
mySwitch.send(66, 12);
delay(1000);
// turn LED on:
digitalWrite(ledPin, HIGH);
}
if (buttonStateC == LOW)
{
// turn LED on:
mySwitch.send(22, 12);
delay(1000);
digitalWrite(ledPin, HIGH);
}
else {
mySwitch.send(1, 12);
digitalWrite(ledPin, LOW);
delay(50);
}
}
ตัวรับ
ต่อ pin 2
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); //ต่อกับขา inerrupt 0 => ในที่นี้คือขา #2
}
void loop() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
}
else {
Serial.print( mySwitch.getReceivedValue() );
Serial.println();
}
// mySwitch.resetAvailable();
}
}