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 