Hello,
I`ve made a short video about how to control LEDs with push-buttons wireless with a cheap 433MHz module and the virtual wire library.
I got a lot of information from this great forum, so hopefully sharing this here is helpful for some of you too.
Here are the codes for the transmitter and the receiver:
transmitter:
/*
This code changes the state of two variables by pressing two pushbuttons and sends
the information wireless over 433MHz, allowing to remote control two LEDs.
Use the "receiver_LED" code for the receiver.
Made by Matthias - https://www.youtube.com/user/Experimentaltechnik
25.6.2014
*/
#include <VirtualWire.h>
//download the library here: http://www.airspayce.com/mikem/arduino/VirtualWire/
//author of the library: Mike McCauley
byte ledState = B00;
byte ledState2 = B00;
byte ledStates = B00;
byte sendState =0;
int counter =0;
int counter2 =0;
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
int buttonState; // the current reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
int buttonState2; // the current reading from the input pin
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long debounceDelay2 = 50; // the debounce time; increase if the output flickers
char ledStatesChar[2]; //char array; stores the Led States; needed in vw_send function
void setup()
{
// Initialise the IO and ISR
// vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop()
{
if(sendState == 1){ //data is only send if a button was pressed
ledStates =(ledState | ledState2);
itoa(ledStates, ledStatesChar, 10);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)ledStatesChar, strlen(ledStatesChar));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
sendState =0;
//debounce code button 1:
int buttonValue = digitalRead(buttonPin);
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonState != buttonValue) {
if(buttonValue == HIGH){
counter++;
if ((counter%2 ==0)) {
ledState = B00; //00 = first LED off, 01 = first LED on
}
else {
ledState = B01;
}
sendState =1;
}
lastDebounceTime = millis();
buttonState = buttonValue;
}
}
//debounce code button 2:
int buttonValue2 = digitalRead(buttonPin2);
if ((millis() - lastDebounceTime2) > debounceDelay2) {
if (buttonState2 != buttonValue2) {
if(buttonValue2 == HIGH){
counter2++;
if ((counter2%2 ==0)) {
ledState2 = B00; //00 = second LED off, 10 = second LED on
}
else {
ledState2 = B10;
}
sendState =1;
}
lastDebounceTime2 = millis();
buttonState2 = buttonValue2;
}
}
}
receiver:
/*
This code receives the LED-states from the "transmitter_LED" code
and turns two LEDs on or off.
Made by Matthias - https://www.youtube.com/user/Experimentaltechnik
25.6.2014
*/
#include <VirtualWire.h>
//download the library here: http://www.airspayce.com/mikem/arduino/VirtualWire/
//author of the library: Mike McCauley
const int ledRed = 2;
const int ledGreen = 3;
char ledStatesChar[2];
byte ledStates = B00;
byte ledState = B00;
byte ledState2 = B00;
void setup()
{
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
// Initialise the IO and ISR
// vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
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;
digitalWrite(13, true); // Flash a light to show received good message
for (i = 0; i < buflen; i++)
{
ledStatesChar[i] = char(buf[i]); //save the data
}
ledStatesChar[buflen] = '\0';
ledStates = atoi(ledStatesChar);
digitalWrite(13, false);
}
ledState = ledStates & B01;
ledState2 = ledStates & B10;
digitalWrite(ledRed, ledState); //turns the LED on or off
digitalWrite(ledGreen, ledState2); //turns the LED on or off
}