Go Down

Topic: 433MHz RF Module to control relay (Read 59 times) previous topic - next topic

rehpej

Hi may I know how to tell arduino to trigger off when not receiving any transmission?

Receiver
Code: [Select]

#include <VirtualWire.h>

int led1 = 3;
int led2 = 4;
int led3 = 5;

void setup() {
  Serial.begin(9600);
  Serial.println("Receive");
  vw_set_rx_pin(2); //RX pin
  vw_setup(2000);
  vw_rx_start(); //Start receiver PLL
  pinMode(13,OUTPUT);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,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
    for(int i = 0;i < buflen; i++) {
      if(buf[i] == '1') { //Receive push button 1
        Serial.println("Led 1");
        digitalWrite(led1,HIGH); //Turn ON Led1
        digitalWrite(13,HIGH);
      }
      if(buf[i] == '2') { //Receive push button 2
        Serial.println("Led 2");
        digitalWrite(led2,HIGH); //Turn ON Led2
        digitalWrite(13,HIGH);
      }
      if(buf[i] == '3') { //Receive push button 3
        Serial.println("Led 3");
        digitalWrite(led3,HIGH); //Turn ON Led3
        digitalWrite(13,HIGH);
      }
    }
  }
}


Transmitter
Code: [Select]

#include <VirtualWire.h>

int pushB1 = 3;
int pushB2 = 4;
int pushB3 = 5;
char *msg;

void setup () {
  Serial.begin(9600);
  Serial.println("Transmit");
  vw_set_tx_pin(2);
  vw_setup(2000);
  pinMode(13,OUTPUT);
  pinMode(pushB1,INPUT);
  pinMode(pushB2,INPUT);
  pinMode(pushB3,INPUT);
}

void loop () {
  if(digitalRead(pushB1)==HIGH) {
    Serial.println("Button 1");
    msg = "1";
    digitalWrite(13,HIGH);
    vw_send((uint8_t*)msg, strlen(msg));
    vw_wait_tx();
  }
  if(digitalRead(pushB2)==HIGH) {
    Serial.println("Button 2");
    msg = "2";
    digitalWrite(13,HIGH);
    vw_send((uint8_t*)msg, strlen(msg));
    vw_wait_tx();
  }
  if(digitalRead(pushB3)==HIGH) {
    Serial.println("Button 3");
    msg = "3";
    digitalWrite(13,HIGH);
    vw_send((uint8_t*)msg, strlen(msg));
    vw_wait_tx();
  }
}


When the push button is pressed will lit up the LED, but when unpressed the LED is still HIGH.

What Ive added so far is

Receiver
Code: [Select]

if(buf[i] == '0') { //Receive off signal
        Serial.println("OFF");
        digitalWrite(led1,LOW); //Turn OFF Led and pin13
        digitalWrite(led2,LOW);
        digitalWrite(led3,LOW);
        digitalWrite(13,LOW);
      }


Transmitter
Code: [Select]

if((digitalRead(pushB1) || digitalRead(pushB2) || digitalRead(pushB3)) == LOW) {
    Serial.println("OFF");
    msg = "0";
    digitalWrite(13,LOW);
    vw_send((uint8_t*)msg, strlen(msg));
    vw_wait_tx();
  }


This way both receiver and transmitter is continuous communicating. What I want is that when not transmitting and receiving the LED will turn off.

INTP

Everything under
  if(vw_get_message(buf, &buflen)) {

works as "if any message received".
Throw in an else after that closes out and write the LEDs LOW.

rehpej

Code: [Select]
  if(vw_get_message(buf, &buflen)) { //Non-blocking
    for(int i = 0;i < buflen; i++) {
      if(buf[i] == '1') { //Receive push button 1
        Serial.println("Led 1");
        digitalWrite(led1,HIGH); //Turn ON Led1
        digitalWrite(13,HIGH);
      }
      if(buf[i] == '2') { //Receive push button 2
        Serial.println("Led 2");
        digitalWrite(led2,HIGH); //Turn ON Led2
        digitalWrite(13,HIGH);
      }
      if(buf[i] == '3') { //Receive push button 3
        Serial.println("Led 3");
        digitalWrite(led3,HIGH); //Turn ON Led3
        digitalWrite(13,HIGH);
      }
      if(buf[i] == '0') { //Receive off signal
        Serial.println("OFF");
        digitalWrite(led1,LOW); //Turn OFF Led and pin13
        digitalWrite(led2,LOW);
        digitalWrite(led3,LOW);
        digitalWrite(13,LOW);
      }
    }
  }


so i tried adding

Code: [Select]

else {
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(13,LOW);
}


but the behavior of the leds are blinking when i press and hold the buttons

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy