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:
- Press button 1 on the Tx and LED 1 lights on the RX. Release button 1 and the LED 1 goes out.
- Press button 2 on the Tx and LED 2 lights on the RX. Release button 2 and the LED 2 goes out.
- 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)