Help request - trying to switch 2 LEDs (lights) ON & OFF remotely

I would be grateful if someone could help me with a project I am working on. I need to remotely switch two LEDs (lights) ON and OFF using simple push buttons.

For the remote connections I am using a 433Mhz Superheterodyne 3400 RF Transmitter Receiver pair.

So that you are aware, I am a complete novice when it comes to Arduino and programming, however, I have made an attempt at writing the TX and RX sketches, and they do run / work to a certain degree. I do have a background in Electrical Engineering, but only a limited knowledge of Microcontrollers.

On paper the my requirements are reasonably simple, as follows:

  1. Press button 1 on the Tx and LED 1 lights on the RX. Release button 1 and the LED 1 goes out.
  2. Press button 2 on the Tx and LED 2 lights on the RX. Release button 2 and the LED 2 goes out.
  3. Press buttons 1 and 2 together and both LEDs light. Release both buttons and both LEDs go out.

The TX and RX sketches I have written do work and the LEDs light as detailed above, but with a major flaw. When I press button 1, LED 1 lights but flashes, and this is the same for the operation of button 2 / LED 2. Also, if I press both buttons (1 & 2) together, LEDs 1 & 2 flash alternatively (in sequence). I need the LEDs to come ON and stay ON until the buttons are released…i.e. not flash. I can see this ON / OFF switching when I monitor the output on the Serial Monitor.

BTW, I have used VirtualWire library and not RadioHead library for this simple project, and it does work.

I have tried several options to rectify the problem, without success. I’m pretty sure it has something to do with the loop function, however, it’s a bit beyond me at this stage.

My TX and RX sketches are detailed below:

Transmitter Sketch:

// Transmitter.pde

#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[8] = {‘h’};
char msg2[8] = {‘j’};

if(digitalRead(button1) == 0){

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) == 0){

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,HIGH);
digitalWrite(button2,HIGH);

}

Receiver Sketch:

// Receiver.pde

#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);*

  • }*

  • }*
    *} *
    If there is anyone out there that can help me, it will be greatly appreciated.
    Bill
    Turning_LED_ON_OFF_TX_2_Buttons.ino (949 Bytes)

You're missing some braces here:

 else
     digitalWrite(LED1,LOW);
     digitalWrite(LED2,LOW);

Probably intended:

else
     {  
     digitalWrite(LED1,LOW);
     digitalWrite(LED2,LOW);
     }

Similar thing in the transmitter code too.

Please note that the VirtualWire library was replaced by the RadioHead library about 6 years ago.
http://www.airspayce.com/mikem/arduino/RadioHead/index.html

The "On/Off Keying" (OOK) of the 433Mhz Superheterodyne 3400 RF Transmitter Receiver pair is a form of "Amplitude Shift Keying" (ASK) so use the library examples whose name starts with "ask_" as a basis for your sketch.

Note: The RadioHead library is so old that the examples use the ".pde" file extension. The 'pde extension was a hold-over from the "Processing" Java IDE project on which Arduino was based. You should probably change those file extensions to the modern ".ino", especially if you have Processing installed (See: Processing.org).

johnwasser and wildbill, many thanks for you quick replies and suggestions.

I had already tried both options (and many more)...i.e. adding the braces and writing the TX and RX sketches with the RadioHead library. My findings are below:

  1. When I add the braces to the TX and RX "else" statements the LEDs still flash when the individual buttons are pressed, but when both buttons are pressed together only one LED (could be either one) will light. That seems to be linked to the flash cycle.

  2. If I use the RadioHead library and make the necessary adjustments to the TX and RX sketches (I think are required) I get a similar problem to the one described above (with the braces). I can switch ON the LEDs individually, but not together.

After many days of experimenting and a lot of head scratching the closest I can get to my requirements is by up loading the sketches shown in my original post.

Because of my lack of experience in this field I am not sure if I am close to a solution or completely off course. So any help / guidance is greatly appreciated.

BTW, I have just noticed that the sketches I added to my original post have the LEDs ON and by pressing the buttons they are turned OFF. A quick change to the TX sketch can easily reverse the operation.