433mhz problems

Hello,
i set up wireless connection in between to 433Mhz moduls. Everything works and i want the arduino with the reciever to turn on the led on pin 13, if it recieves a "1" and turn it of if it recieves "2". It turn the led on but not of although i can see via the serial monitor, that it recieves a "2".

With switch case it also turns on the led but not of.

I am new in this forum and for the case the attachment dosent work, thats my code:

#include <VirtualWire.h>

#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
Serial.begin(9600);

pinMode(4, OUTPUT);
digitalWrite(4, LOW); // GND 0V
pinMode(7, OUTPUT);
digitalWrite(7, HIGH); // VCC 5V
vw_set_ptt_inverted(true);
vw_setup(2000); // Bits per sec
vw_set_rx_pin(5);
vw_rx_start(); // Start the receiver
}

void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen))
{
int i;
// Message with a good checksum received.

for (i = 0; i < buflen; i++)
{
Serial.print((char)buf*);*

  • }*

  • Serial.println("");*

  • if(i == 1){*

  • digitalWrite(13, HIGH);*

  • }*

  • if(i == 2){*

  • digitalWrite(13, LOW);*

  • }*

  • }*
    }
    Do you know what i am doing wrong?
    test.ino (1.15 KB)

Hello,

variable "i" is only index of char array.
You must try this:

.....

    if(buf[i] == "1") digitalWrite(13, HIGH);
    if(buf[i] == "2") digitalWrite(13, LOW);
   
......

Let us know....

What's going on with all the #undef?? Have you seen this anywhere else as a best practice??

Read about properly posting code here with the code tags your array indexing is changed into getting code in italic probably....

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

First of all, thanks for all of your quick replys.

elsabz:
Hello,

variable "i" is only index of char array.
You must try this:

.....

if(buf[i] == "1") digitalWrite(13, HIGH);
   if(buf[i] == "2") digitalWrite(13, LOW);
 
......




Let us know....

When i tried this code the led didnt even turn on.

I have tried to change a few lines in the code and noticed that it will only turn on the led when it says:

if(i == 1){
     digitalWrite(13, HIGH);
   }
   if(i == 2){
     digitalWrite(13, LOW);
   }

When i change it like this:

 if(i == 1){
      digitalWrite(13, LOW);
    }
    if(i == 2){
      digitalWrite(13, HIGH);
    }

or like this:

if(i == 2){
      digitalWrite(13, HIGH);
    }
    if(i == 1){
      digitalWrite(13, LOW);
    }

Nothing happends. This leads me to the conclusion, that "i" is not the recieved message but the state of the pin 5. But if i do it like elsabz nothing happends at all. I also tried this one:

    if((char)buf[i] == "1") digitalWrite(13, HIGH);
    if((char)buf[i] == "2") digitalWrite(13, LOW);

but didnt do anything too.

J-M-L:
What's going on with all the #undef?? Have you seen this anywhere else as a best practice??

Read about properly posting code here with the code tags your array indexing is changed into getting code in italic probably....

It is not my code, because i am not so experienced in arduino IDE. But i also changes nothing when i delete them.

Do you still have any ideas? Thanks

@elsabz: if(buf[i] == "1") digitalWrite(13, HIGH);
Single quotes, not double.

Yes sorry :cold_sweat:

if(buf[i] == '1') digitalWrite(13, HIGH);

Felix317 check te transmit program or post the code!

Thats the transmit program:

#include <VirtualWire.h>

#undef int
#undef abs
#undef double
#undef float
#undef round

int SchalterZustand;
int SchalterZustand2;

void setup()
{   pinMode(8, INPUT);
   pinMode(2, INPUT);
   pinMode(5, OUTPUT);
   digitalWrite(5, LOW);      // GND 0V
   pinMode(6, OUTPUT);
   digitalWrite(6, HIGH);     // VCC 5V
   vw_set_ptt_inverted(true); // Required for RF Link module
   vw_setup(2000);            // Bits per sec
   vw_set_tx_pin(7);          // pin 7 is used as the transmit data out into the TX Link module, change this as per your needs  
}

void loop()
{ SchalterZustand = digitalRead(2);
 SchalterZustand2 = digitalRead(8);

 if(SchalterZustand == 1){
   const char *msg = "1";       // this is your message to send

   vw_send((uint8_t *)msg, strlen(msg));
   vw_wait_tx();                // Wait for message to finish
   delay(200);
 }
 if(SchalterZustand2 == 1){
   const char *msg = "2";  

   vw_send((uint8_t *)msg, strlen(msg));
   vw_wait_tx();      
   delay(200);
 }
}

The 433Mhz sends a '1' when the button on pin 2 and a '2' if the button on pin 8 is pressed
I think the transmit program works fine, because i can see the recieved message via the serial monitor.

In the receive program i dont'see thys

pinMode(13, OUTPUT);

If you see the correct caracter in serial monitor, then the problem is in for/next cicle.
buf is a monodimensional array and have a size of VW_MAX_MESSAGE_LEN
for example:

buf[0] = '1'
buf[1] = '2'
buf[2] = '1'
buf[3] = '2'
buf[4] = '1'

You must check inside the buffer, or You can try by reduce the range of buf array.
for example:

 uint8_t buf[2];

I tried everything and i quit! >:(
Does anybody of you know a code or guide that turns on/off a led of the arduino with the reciever, when the button on the arduino with the transmitter is pressed? On the internet are only guides and codes that aren´t useful for me or i dont undestand them at all.

Just to clarify - you do have 2 arduinos — one on both ends, correct?

J-M-L:
Just to clarify - you do have 2 arduinos — one on both ends, correct?

Yes.

So can you try these codes, assuming you wired things correctly, with the proper voltage

emitter:

#include <VirtualWire.h>

void setup() {
  vw_setup(2000);
}
 
void loop() {
  static int val = 0; 
  vw_send((byte *) &val, sizeof(val)); 
  vw_wait_tx();
  val = (val+1) % 10;
  delay(1000);
}

receiver:

#include <VirtualWire.h>

void setup() {
  Serial.begin(115200);
  vw_setup(2000);
  vw_rx_start();
}

void loop() {
  int val;
  byte sizeOfVal;

  vw_wait_rx();
  sizeOfVal = sizeof(val);
  if (vw_get_message((byte *) &val, &sizeOfVal)) {
    Serial.println(val);
  }
}

connect the Arduino from the receiver to your PC using USB, open the console at 115200 bauds.

what do you see?

I see numbers from 0 to 9 showing up and then restarting at 0 again.

Good so your system works and you have code to send numbers and receive numbers. :slight_smile:

Now you can add in the receiver something like if what I got is 1, turn on the led, if it's 2 turn off the led.

Everything is working. Thanks, Thanks ,Thanks. You cant imagine how happy i am right now. I have been trying to get this projekt working for more than 2 two weeks, and finaly it works. :slight_smile:

I have just one last tiny problem. When i power everything on it works, but if i than do nothing for a minute the reciever dosent respond at all or just about 10% of the time i want it to do something.
When i disconnect the reciever and connect it again everything works untill i do nothing for a minute.
I dont think its the transmitter because i programmed it to indicate every transmission with a led.

Thats the transmitter:

#include <VirtualWire.h>
boolean on = false;
int data;
boolean on2 = false;
int data2;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);        // GND 0V
  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);       // VCC 5V
  vw_setup(2000);
  vw_set_tx_pin(7);
}
 
void loop() {
  data = digitalRead(8);
  data2 = digitalRead(9);
  
  if(data == 1){
    if(on == false){
      digitalWrite(13, HIGH);
      static int val = 1;
      vw_send((byte *) &val, sizeof(val));
      vw_wait_tx();
      delay(200);
      digitalWrite(13, LOW);
      on = true;}
    else{
      digitalWrite(13, HIGH);
      static int val = 2;
      vw_send((byte *) &val, sizeof(val));
      vw_wait_tx();
      delay(200);
      digitalWrite(13, LOW);
      on = false;}  
    }

  if(data2 == 1){
    if(on2 == false){
      digitalWrite(13, HIGH);
      static int val = 3;
      vw_send((byte *) &val, sizeof(val));
      vw_wait_tx();
      delay(200);
      digitalWrite(13, LOW);
      on2 = true;}
    else{
      digitalWrite(13, HIGH);
      static int val = 4;
      vw_send((byte *) &val, sizeof(val));
      vw_wait_tx();
      delay(200);
      digitalWrite(13, LOW);
      on2 = false;}  
    }
}

and Reciever:

#include <VirtualWire.h>

void setup() {
  pinMode(4, OUTPUT); 
  digitalWrite(4, LOW); // GND 0V
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH); // VCC 5V
  pinMode(13, OUTPUT);
  Serial.begin(115200);
  vw_setup(2000);
  vw_set_rx_pin(5);
  vw_rx_start();
}

void loop() {
  int val;
  byte sizeOfVal;

  vw_wait_rx();
  sizeOfVal = sizeof(val);
  if (vw_get_message((byte *) &val, &sizeOfVal)) {
    Serial.println(val);
    if(val == 1){
      digitalWrite(13, HIGH);}
    if(val == 2){
      digitalWrite(13, LOW);}
  }
}

The transmitter is supposed to send a 1 or 2 (a 1 when a 2 was send before and a 2 when there was send a 1 before) when there is an input on pin 8 and a 3 or 4 when there is input on on 9. (3 and 4 are for another reciever)

good news...

there are some weird stuff in your code though, the static val thingy repeated everywhere... not sure you got what that does.. or what is your boolean thing stuff?

You should also declare names for your PINs so and not address things directly, it makes everything complicated to read.

how are things wired on 8 and 9? do you have buttons there? do you have a pull down resistor?

also looking at this

  digitalWrite(5, LOW);        // GND 0V
  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);       // VCC 5V

are you powering the module from your PINs? this is not good as it might draw too much current. Connect directly to 5V (or 3.3V depending on your module) and GND as in my picture above.

Note you need to do the vw_set_tx_pin before calling vw_setup

I would do something like this - also tracking only LOW to HIGH button transitions

wiring should be (with pulldown resistor, you could simplify using the builtin pullup)

Arduino GND --> Resistor 10kΩ --> Pin 8 ---> Button1 ---> Arduino 5V
Arduino GND --> Resistor 10kΩ --> Pin 9 ---> Button2 ---> Arduino 5V

Arduino Pin 7 --> RF433 TX pin
Arduino GND --> RF433 GND
Arduino 5V --> RF433 Vin

(I've not tested, just typed it in, so might be buggy)

#include <VirtualWire.h>
const byte RF433TXPin = 7;

const byte Button1 = 8;
byte b1State, b1PreviousState;
boolean Lamp1IsOn = false;

const byte Button2 = 9;
byte b2State, b2PreviousState;
boolean Lamp2IsOn = false;


void setup() {
  pinMode(LED_BUILTIN, OUTPUT); // on your UNO LED_BUILTIN is defined to be PIN 13 for you
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  b1PreviousState = LOW;
  b2PreviousState = LOW;
  vw_set_tx_pin(RF433TXPin);
  vw_setup(2000);
}

void loop() {
  int val;

  // ------------------------------------------------------------------------------------

  b1State = digitalRead(Button1);
  if ((b1State == HIGH) && (b1PreviousState == LOW)) { // transition LOW to HIGH
    digitalWrite(LED_BUILTIN, HIGH);
    if (Lamp1IsOn) val = 2; else val = 1;
    vw_send((byte *) &val, sizeof(val));
    vw_wait_tx();
    delay(200);
    Lamp1IsOn = !Lamp1IsOn; // change Lamp status to the opposite
    digitalWrite(LED_BUILTIN, LOW);
  }
  b1PreviousState = b1State;

  // ------------------------------------------------------------------------------------

  b2State = digitalRead(Button2);
  if ((b2State == HIGH) && (b2PreviousState == LOW)) { // transition LOW to HIGH
    digitalWrite(LED_BUILTIN, HIGH);
    if (Lamp2IsOn) val = 4; else val = 3;
    vw_send((byte *) &val, sizeof(val));
    vw_wait_tx();
    delay(200);
    Lamp2IsOn = !Lamp2IsOn; // change Lamp status to the opposite
    digitalWrite(LED_BUILTIN, LOW);
  }
  b2PreviousState = b2State;
  // ------------------------------------------------------------------------------------

}

in the grand scheme of things you should debounce buttons, but because there is a delay(200) hardcoded into the action, that should be enough to debounce in most cases.

Also if you stick to just sending 1,2,3,4 (or anything below 255) then no need to send two bytes (val is an int and int are stored on 2 bytes), you could declare val as a byte and that will only send 1 byte with your value. Of course need to change the type for val in the receiving end too in that case to match the type and use byte there as well.

J-M-L:
good news...

there are some weird stuff in your code though, the static val thingy repeated everywhere... not sure you got what that does.. or what is your boolean thing stuff?

You should also declare names for your PINs so and not address things directly, it makes everything complicated to read.

how are things wired on 8 and 9? do you have buttons there? do you have a pull down resistor?

also looking at this

  digitalWrite(5, LOW);        // GND 0V

pinMode(6, OUTPUT);
 digitalWrite(6, HIGH);       // VCC 5V



are you powering the module from your PINs? this is not good as it might draw too much current. Connect directly to 5V (or 3.3V depending on your module) and GND as in my picture above.

Note you need to do the vw_set_tx_pin before calling vw_setup

You are right, i have no idea what it does but its working.

Sorry, that didnt declare any name for the pins, but i had no idea how to name them.

They are connected to an esp8266 wifi module. I would connect the 433Mhz directly to the esp but it can only output 3.3v. When i want to send a 1 or 2, the esp sends for 0.2 sec´s a signal to pin 8 of the arduino. The same goes for 3 and 4 just with pin 9.

Yes they are powering the module. I read this on the internet(it says the current from the GPIO is enough for the module) and thats the best option for me, because i need the 5v for a relay on the reciever. On the transmitter i am doing this for space reasons.

Somehow my problem disappeared, bacause now everything works fine. Weired...
But when it recur´s i will try to connect the modules to the 5v directly.

J-M-L:
I would do something like this - also tracking only LOW to HIGH button transitions

wiring should be (with pulldown resistor, you could simplify using the builtin pullup)

Arduino GND --> Resistor 10kΩ --> Pin 8 ---> Button1 ---> Arduino 5V
Arduino GND --> Resistor 10kΩ --> Pin 9 ---> Button2 ---> Arduino 5V

Arduino Pin 7 --> RF433 TX pin
Arduino GND --> RF433 GND
Arduino 5V --> RF433 Vin

(I've not tested, just typed it in, so might be buggy)

#include <VirtualWire.h>

const byte RF433TXPin = 7;

const byte Button1 = 8;
byte b1State, b1PreviousState;
boolean Lamp1IsOn = false;

const byte Button2 = 9;
byte b2State, b2PreviousState;
boolean Lamp2IsOn = false;

void setup() {
 pinMode(LED_BUILTIN, OUTPUT); // on your UNO LED_BUILTIN is defined to be PIN 13 for you
 pinMode(Button1, INPUT);
 pinMode(Button2, INPUT);
 b1PreviousState = LOW;
 b2PreviousState = LOW;
 vw_set_tx_pin(RF433TXPin);
 vw_setup(2000);
}

void loop() {
 int val;

// ------------------------------------------------------------------------------------

b1State = digitalRead(Button1);
 if ((b1State == HIGH) && (b1PreviousState == LOW)) { // transition LOW to HIGH
   digitalWrite(LED_BUILTIN, HIGH);
   if (Lamp1IsOn) val = 2; else val = 1;
   vw_send((byte *) &val, sizeof(val));
   vw_wait_tx();
   delay(200);
   Lamp1IsOn = !Lamp1IsOn; // change Lamp status to the opposite
   digitalWrite(LED_BUILTIN, LOW);
 }
 b1PreviousState = b1State;

// ------------------------------------------------------------------------------------

b2State = digitalRead(Button2);
 if ((b2State == HIGH) && (b2PreviousState == LOW)) { // transition LOW to HIGH
   digitalWrite(LED_BUILTIN, HIGH);
   if (Lamp2IsOn) val = 4; else val = 3;
   vw_send((byte *) &val, sizeof(val));
   vw_wait_tx();
   delay(200);
   Lamp2IsOn = !Lamp2IsOn; // change Lamp status to the opposite
   digitalWrite(LED_BUILTIN, LOW);
 }
 b2PreviousState = b2State;
 // ------------------------------------------------------------------------------------

}




in the grand scheme of things you should debounce buttons, but because there is a delay(200) hardcoded into the action, that should be enough to debounce in most cases.


Also if you stick to just sending 1,2,3,4 (or anything below 255) then no need to send two bytes (`val` is an `int` and `int` are stored on 2 bytes), you could declare `val` as a `byte` and that will only send 1 byte with your value. Of course need to change the type for `val` in the receiving end too in that case to match the type and use `byte` there as well.

I will also try it and thanks a lot. You seem to be really investing time into my project, and to be honest, i dont know if i would do all of this for a random guy from the internet. I appreciate that.
Thanks :slight_smile:

To be honest it took 5 minutes... :slight_smile:

I'm a bit lost in the set up you have, arduino+ESP+433Mhz ?