433mhz code help

Hey guys I’ve been playing around the the 433mhz modules. Took at look at some examples and got them to work. So now I’m trying to do my own thing. Without much success…

The idea is to press ‘greenButton’ from the transmitter and it will light up the ‘greenLed’ in the receiver. While pressing ‘yellowButton’ from the transmitter and it will light up the ‘yellowLed’ in the receiver. But problem I’m having is pressing either button lights up either led, sometimes one after the other. I’m guessing the information received from the transmitter isn’t clear? I’m told these modules do get some noise.

Code for Transmitter (Arduino MEGA)

#include <VirtualWire.h>

int greenButton = 8;
int yellowButton = 7;

void setup()
{
  pinMode(greenButton,INPUT);
  pinMode(yellowButton,INPUT);
  
  vw_set_ptt_inverted(true); //
  vw_set_tx_pin(12);
  vw_setup(4000);// speed of data transfer Kbps
}

void loop()
{
  char *controller;
  if(digitalRead(greenButton)==HIGH)
  {
    controller="1";
    vw_send((uint8_t *)controller, strlen(controller));
    vw_wait_tx();
  }
  if(digitalRead(yellowButton)==HIGH)
  {
    controller="2";
    vw_send((uint8_t *)controller, strlen(controller));
    vw_wait_tx();
  }
}

Code for Receiver (Arduino UNO)

#include <VirtualWire.h>

int greenLed = 8;
int yellowLed = 7;

void setup()
{
  pinMode(greenLed,OUTPUT);
  pinMode(yellowLed,OUTPUT);
  
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_set_rx_pin(12);
  vw_setup(4000);  // 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
  {
    if(buf[0]=='1')
    {
      digitalWrite(greenLed,HIGH);
      delay (2000);
      digitalWrite(greenLed,LOW);
    }
    if(buf[0]=='2')
    {
      digitalWrite(yellowLed,HIGH);
      delay (2000);
      digitalWrite(yellowLed,LOW);
    }
  }
  else
  {
    digitalWrite(yellowLed,LOW);
    digitalWrite(greenLed,LOW);
  }
}

Thanks

And want do your debug prints tell you is happening?

  1. Do you have external pull up/down resistors on the buttons?

2, Why are you send at very hich speed and then reading once every 2 secs?

Mark

And want do your debug prints tell you is happening?

There isn't a 'problem' that the IDE can see.

Do you have external pull up/down resistors on the buttons?

Why do I always forget that?!? But now that I've put some... It works!! Thanks. Can't believe my problem was that small.

There isn’t a ‘problem’ that the IDE can see

There isn’t a problem that I can see, which is why I’d add debug prints, to show us what your software is seeing.

There isn't a problem that I can see, which is why I'd add debug prints, to show us what your software is seeing.

Oh... I don't think I've heard of that before. I thought its the orange 'error' you see when you try to verify the code.