Arduino rf 433MHz car locker!

Hello everyone, I have an idea to do car lock-unlocker with two arduinos and rf 433mnz transmitter-receiver!
I'm new to Arduino and programming languages??, until now managed to run Arduino to the computer via the communication port, but I could not manage one arduino by the other Arduino!
I have some code on whom I work, but without success!
Transmitter

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
const int buttonPin1 = A0;    
const int buttonPin2 = A1;
const int ledPin =  13;     
int buttonState1 = 0;         
int buttonState2 = 0;

void setup()
{
 
  vw_set_ptt_inverted(true);
  vw_set_tx_pin(12);
  vw_setup(2000);
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin1, INPUT); 
  pinMode(buttonPin2, INPUT); 
}

void loop()       {
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
 
    if (buttonState1 == HIGH){
    const char *msg = "asd";
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx(); 
    delay(1000);
  }
    if (buttonState2 == HIGH){
   const char *msg = "zxc";
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx(); 
    delay(1000);
 }
}

I use two codes for the receiver, but both do not work
Receiver 1 :

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(12);
  vw_setup(2000);
  vw_rx_start();
  pinMode(13, OUTPUT);
}

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

  if (vw_get_message(buf, &buflen))
  {
    if (buf[0] == 'asd') {
	digitalWrite(13, HIGH);
    }
    if (buf[0] == 'zxc') {
	digitalWrite(13, LOW);
    }
  }
}

Receiver 2 :

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(12);
  vw_setup(2000);
  vw_rx_start();
  pinMode(13, OUTPUT);
}

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

  if (vw_get_message(buf, &buflen))
  {
    if (buf[0] == '617364') {
	digitalWrite(13, HIGH);
    }
    if (buf[0] == '7A7863') {
	digitalWrite(13, LOW);
    }
  }
}

I will be happy if someone help me! Thanks!!!

if (buf[0] == 'asd') {

Three chars into one char does not go.
Either make it into a string, and use strcmp, or compare one character at a time.

Thank you very much for the answer!
Is it possible for you to show me what to change in code, because I used one character, but still it dosen't work!!!
:slight_smile:

because I used one character, but still it dosen't work!!!

Post your code.

Transmitter:

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
const int buttonPin1 = A0;    
const int buttonPin2 = A1;
const int ledPin =  13;     
int buttonState1 = 0;         
int buttonState2 = 0;

void setup()
{
 
  vw_set_ptt_inverted(true);
  vw_set_tx_pin(12);
  vw_setup(2000);
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin1, INPUT); 
  pinMode(buttonPin2, INPUT); 
}

void loop()       {
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
 
    if (buttonState1 == HIGH){
    const char *msg = "a";
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx(); 
    delay(1000);
  }
    if (buttonState2 == HIGH){
   const char *msg = "z";
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx(); 
    delay(1000);
 }
}

Receiver:

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(12);
  vw_setup(2000);
  vw_rx_start();
  pinMode(13, OUTPUT);
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
 byte strcmp;
  if (vw_get_message(buf, &buflen))
  {
    if (buf[0] == 'a') {
	digitalWrite(13, HIGH);
    }
    if (buf[0] == 'z') {
	digitalWrite(13, LOW);
    }
  }
}

OK, so now you're only interested in the first letter of the password?
And what happened?

Like that it dosen't work. If I use the code from VirtualWire for the receiver , when is pressed "a" I have (61) and for "z"-(7A). I use this numbers in my receiver code, but still no work :expressionless:
Receiver:

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(12);
  vw_setup(2000);
  vw_rx_start();
  pinMode(13, OUTPUT);
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
 byte strcmp;
  if (vw_get_message(buf, &buflen))
  {
    if (buf[0] == '61') {
	digitalWrite(13, HIGH);
    }
    if (buf[0] == '7A') {
	digitalWrite(13, LOW);
    }
  }
}

Did you mean

if (buf[0] == 0x61) {

perhaps?

That's just the same as if (buf[0] == 'a') {

Like that it dosen't work.

That is an infuriating, pointless, meaningless phrase. (badly spelled too)
"work" implies some so-far undisclosed observation.
"doesn't work" also implies some so-far undisclosed observation that is in some way at odds with the first.

WE CAN'T SEE YOUR PROJECT.

Have you verified (with a simple LED blink) that your receiver is receiving any messages?

Just out of interest what RF link are you using? im looking for a device that will do a similar sort of think.

Many thanks

Tris

I use this RF link and now everything works correctly, I just made ??a small mistake with RX pin.
From the beginning I used the code from VirtualWire, there RX is 11pin.
In my code I use 12th pin and there was a problem!!!
Thank you for the help AWOL !!!

Welcome back :slight_smile:
I managed to make communication between two arduinos but now I want to use the entire password, not just one character !
Now I'm here:
Transmitter

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
const int buttonPin1 = A5;    
int buttonState1 = 0;         

String msg;

void setup()
{
  vw_set_ptt_inverted(true);
  vw_set_tx_pin(12);
  vw_setup(2000);
  pinMode(buttonPin1, INPUT); 
 }

 
 void loop()       {
   buttonState1 = digitalRead(buttonPin1);

    if (buttonState1 == HIGH){
    String msg[3] = " put ";
    vw_send((uint8_t *)msg, 3);
    vw_wait_tx(); // Wait until the whole message is gone
    delay(1000);
    
  }
}

Receiver

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(12);
  vw_setup(2000);
  vw_rx_start();
  pinMode(13, OUTPUT);
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  
  if (vw_get_message(buf, &buflen))
  {
    if (buf[3] == 'p','u','t') 
    {
   for (int i = 0; i < 10; i++) {
        digitalWrite(13, HIGH);
        delay(100);
        digitalWrite(13, LOW);
        delay(100); 
}
}
}
}

It works, but no matter what is the password!!!
If instead of ( 'p', 'u', 't') I put (' ', ' ', ' ') still works!
Why is it?
Can anyone explain!
Thank you :slight_smile:

    String msg[3] = " put ";

This is creating an array of String instances, initializing only the first one. That is probably not what you want.

    vw_send((uint8_t *)msg, 3);

Sending a String object, and sending the data IN a String object are not the same thing.

Do yourself a BIG favor and ditch the String class. It is clear that you do not understand its purpose or how to use it. Even the string (note the lower case s) " put " has 5 characters. Why you would want to only send the first 3 is a mystery.

    vw_wait_tx(); // Wait until the whole message is gone
    delay(1000);

Then wait some more...

    if (buf[3] == 'p','u','t')

There are so many things wrong here. buf[3] is the 4th character in the array (that contains garbage read from the sender, since the sender is sending garbage) The 4th character is being compared to the letter p. That is either true or false. If it is true, you have:

if(true, 'u', 't')
{
}

If it is false, you have

if(false, 'u', 't')
{
}

The comma operator does something. Can YOU explain what that something is? Can you make sense of these two if statements? If you can't, how can you expect that Arduino to.

The proper way to compare the input (if it were valid) would be:

if(buf[0] == 'p' && buf[1] == 'u' && buf[2] == 't')
{
}

You can't just make up shortcuts and expect the compiler to understand them.

Great, thank you PaulS :smiley:
Transmitter

 void loop()       
 {
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH){
  char msg[6] = {'b', 'o', 'o', 'k', 'e', 'r'};
  vw_send((uint8_t *)msg, 6);
  vw_wait_tx(); // Wait until the whole message is gone
  delay(1000);
 }
}

Receiver

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  
  if (vw_get_message(buf, &buflen))
  {
   if(buf[0] == 'b' && buf[1] == 'o' && buf[2] == 'o' && buf[3] == 'k' && buf[4] == 'e' && buf[5] == 'r')
   {
    for (int i = 0; i < 10; i++) 
    {
     digitalWrite(13, HIGH);
     delay(100);
     digitalWrite(13, LOW);
     delay(100); 
    }
   }
  }
 }

Now it works correctly!!!
Thanks again, RESPECT!!!!!!

You could make that easier, on the receiver end:
uint8_t buf[VW_MAX_MESSAGE_LEN+1];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen))
{
buf[buflen] = '\0';
if(strcmp(buf, "booker") == 0)
{
// they match

I tried this

  uint8_t buf[VW_MAX_MESSAGE_LEN+1];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  
  if (vw_get_message(buf, &buflen))
  {
     buf[buflen] = '\0';
     if(strcmp(buf, "booker") == 0)
     {
     }

But I get the following message:
-invalid conversion from 'uint8_t*' to 'const shar*'
-error: initializing argument 1 of 'int strcmp(const char*, const shar*)'

Anyway, with the first method everything is ok!

So, lie to the compiler:

if(strcmp((const char *)buf, "booker") == 0)

Anyway, with the first method everything is ok!

Until you decide to change your password to "booker isn't a quitter!". Then, things get ugly.