Arduino Wireless Remote -- Help Please!

EDIT - You can follow this project on my blog here: http://maxkulik.koding.com/. You can also find more pictures there as well.
Hi Everyone!

I am currently working on an Arduino project where I am going to have 2 Arduinos set up. One will be sending a RF frequency and the other will be receiving it. The receiver will also have some relays connected to the Digital Out pins and I am trying to get it so that when I press one button on the "Sender Arduino", the Arduino receiving the signal will in return activate one of the two relays. I have taken some pics of my wiring and how I am planning on connecting everything. I have tried doing the code myself, but I have been having a really hard time trying to get this to work. I need it so that when one of the buttons are pressed it will make Relay 1 (R1) turn on and then when the other button is pressed Relay 2 (R2) will turn on. I did not connect the buttons to anything in the picture, because I am not 100% sure where they will need to be connected to for them to work the right way. Any help will be great!!!

This is the website that has the example code for the RF sender and receiver module - http://www.seeedstudio.com/wiki/Grove_-_433MHz_Simple_RF_Link_Kit

Here is a list of all of the parts that I am using:

SeeedStudio RF Receiver and sender module:
Wiki/Code - http://www.seeedstudio.com/wiki/Grove_-_433MHz_Simple_RF_Link_Kit
Retail Page - http://www.seeedstudio.com/depot/grove-433mhz-simple-rf-link-kit-p-1062.html?cPath=139_140

SeeedStudio Grove Relays:
Wiki/Code - http://www.epictinker.com/Grove-Relay-p/com22639p.htm
Retail Page - http://www.seeedstudio.com/wiki/index.php?title=Project_Five_–_Relay_Control

Here are the pictures of what I have in mind. This is also how I am playing to connect them too:

Any help at all will be soooo helpful!!!
Thanks!

EDIT
I just wanted to post an update on what the car looks like now
If you have any questions about the wiring or anything along that line just ask.


This is a picture of the car and the controller both on and running. The car and the controller both have an Arduino inside that is acting as the "brain" of the project.


In this picture, you can see the 2 relays that will make the car either move forwards or backwards.


This is the back end of the car. You can see the Arduino Development Shield/Board that I have all the wires soldered into. I am using the dev. board because it allows for me to Remove the Arduino to upload the code. The Arduino cannot be connected to the RF Receiver when uploading the code because for some reason you will end up getting a "sync" error.


Again here is a closer picture of the Relays. You ca also see some LEDs soldered to some PCB board. These will act as headlights. I just don't have the wired up yet because I am thinking about changing them out for some much more powerful blue or white LEDs.


This is a picture of the controller from the outside when it is on and all put together. Notice there are 2 buttons, one for forwards and another for backwards. On the bottom of the picture you can see there is an ON/OFF switch and a low power LED to indicate that the power is on.


This is a picture of the inside of the controller. You can see on the cover there is an Arduino R2 glued in. The Arduino is what controls the logic of the pressing and releasing of the the forward and backward buttons on the other side. Inside the project box you can see the Arduino is powered by and 9v battery connect to the "Vin" and "GND" pins on the Arduino. The little PCB board on the inside of the box is the RF Transmitter that sends the signal to the car; making it move forward or backward.


This is just a better picture of the RF Transmitter and the 9v battery on the inside of the project box


This is a close up of the Arduino inside the project box/controller.

I have made exactly what your trying to make. One arduino is the receiver, the other the transmitter, both connected by RF modules.

Your going to need the VirtualWire library and make use of the sprintf() function, as well as the strtok() function. With the help from Nick and Paul, I was able to fully understand the VW library.

Do a forum search for "VirtualWire and accelerometer" you should see my name. In that post, I give an example of a working VR setup.

Where could I go to get more information on how the VirtualWire library works? Or can you give me a hand in understanding how to use the code for what I am working with?

This is what I have so far for the receiver:

//Grove - 315(433) RF link kit Demo v1.0
//by :http://www.seeedstudio.com/
//connect the receive module to D2 to use ..
#include <VirtualWire.h>
 
int RF_RX_PIN = 0;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("setup");
  vw_set_rx_pin(RF_RX_PIN);  // Setup receive pin.
  vw_setup(2000); // Transmission speed in bits per second.
  vw_rx_start(); // Start the PLL receiver.
}
 
void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if(vw_get_message(buf, &buflen)) // non-blocking I/O
  {
    int i;
    // Message with a good checksum received, dump HEX
    Serial.print("Got: ");
    for(i = 0; i < buflen; ++i)
    {
      Serial.print(buf[i], HEX);
      Serial.print(" ");
	  //Serial.print(buf[i]);
    }
    Serial.println("");
  }
}

... and this is the transmitter code:

#include <VirtualWire.h>

//Grove - 315(433) RF link kit Demo v1.0
//by :http://www.seeedstudio.com/
//connect the sent module to D2 to use  
#include <VirtualWire.h>
 
int RF_TX_PIN = 1;
 
void setup()
{
  vw_set_tx_pin(RF_TX_PIN); // Setup transmit pin
  vw_setup(2000); // Transmission speed in bits per second.
}
 
void loop()
{
  int i = 0;
  const char *msg = i;
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'hello' every 400ms.
  i++;
  delay(2000);
 
}

I'll help you. Virtualwire sends out and receives strings of data. Sprintf() will convert your data into a string and send it out. The strtok() will break the string up into individual segments, based on what you want to split the data with. I chose to use a comma as my spliting point. My post would show you exactly how I did it. Did you do that search?

Yes, I did do the search. I came up with this: http://arduino.cc/forum/index.php/topic,151900.15.html

Is this right?

Yep, that's it. As you can probably tell by the title, I was sending accelerometer data over to my robot in the form of a string. I then used strtok to split that data up and atoi() to convert the data to usable ints.

Yes, I do see that. One of the main things that I am confused on it how you are you can tell what pin will be the virtual pin. In one point you say:

  digitalWrite(37, LOW); // RF ground
  digitalWrite(35,HIGH); // RF Vcc
  digitalWrite(53,HIGH); // accelormeter Vcc

I don't understand have VirtualWire pins are defined in the code.

None of those are the sending pin, look at the comments beside them.
This is where you set the transmitting and receiving pins.

 vw_set_tx_pin(33); //RF sending pin
vw_set_rx_pin(8); //RF receiving pin

Ohhh ok, I see. Give me a min here to look over the code again.

If I want to just get it set up so that when I press a button on one Arduino (2 Buttons, 1 one Digital Pin 13 and the other 12) it will send a signal out of the transmitter and the Arduino that is receiving the signal turns on relay 1(Relay 1 is connected to Digital pin 13). I feel that it is really simple and I am working on a version of the code, but do you think that you could help me out?

MaxKulik:
I feel that it is really simple and I am working on a version of the code, but do you think that you could help me out?

Not without seeing the latest code you have been working on.

You have to post what you have so far.

When I stopped working on the code to post it I was currently trying to get a serial monitor thing setup for debugging.

#include <VirtualWire.h>

int RF_TX_PIN = 2;
int Button1 = 13;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("setup");
  vw_set_tx_pin(RF_TX_PIN); // Setup transmit pin
  vw_setup(2000); // Transmission speed in bits per second.
  pinMode(Button1, INPUT);
}
 
void loop()
{
  if (digitalRead(Button1)){
  const char *msg = "hello";
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'hello' every 400ms.
  delay(100);
  }
}

Ok so a little playing around and I got the Arduino to show that the button is working:

#include <VirtualWire.h>

int RF_TX_PIN = 2;
int Button1 = 13;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("setup");
  vw_set_tx_pin(RF_TX_PIN); // Setup transmit pin
  vw_setup(2000); // Transmission speed in bits per second.
  pinMode(Button1, INPUT);
}
 
void loop()
{
  if (digitalRead(Button1)){
  Serial.println("It Worked!");
  const char *msg = "hello";
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'hello' every 400ms.
  delay(100);
  }
}

Ok, what about the receiving code?

Ok. So I have been playing around with the code for a few days now and I have worked out some bugs. I have one of the push buttons connected to pin 13 on the Arduino for the controller on the car. It sends the code when I press the button and the receiver on the other Arduino on the car receives the code. I also have it set up so that when the car receives a signal it flips Relay 1 and makes the car go forward.

Now the only problem that I am having is when I let go of the button on the controller, the car continue to moves forward.
Here is the code that I have so far:

Controller (Transmitter):

#include <VirtualWire.h>

int RF_TX_PIN = 2;
int Button1 = 13;      //Forward (Button 1)
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Setup Complete");
  vw_set_tx_pin(RF_TX_PIN); // Setup transmit pin
  vw_setup(2000); // Transmission speed in bits per second.
  pinMode(Button1, INPUT);
}
 
void loop()
{
  if (digitalRead(Button1)){
  Serial.println("Forwards");
  const char *msg = "Forwards";
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'Forwards' every 50ms.
  delay(50);
  }
}

Car (Receiver):

#include <VirtualWire.h>
 
int RF_RX_PIN = 2;
int Relay1 = 8;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Serial Connection Complete");
  Serial.println();
  Serial.println("*NOTE* Currently Connected to Receiver");
  vw_set_rx_pin(RF_RX_PIN);  // Setup receive pin.
  vw_setup(2000); // Transmission speed in bits per second.
  vw_rx_start(); // Start the PLL receiver.
  pinMode(Relay1, OUTPUT);
}
 
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, dump HEX
    Serial.print("Got: ");
    for(i = 0; i < buflen; ++i)
    {
      Serial.print(buf[i], HEX);
      Serial.print(" ");
	  //Serial.print(buf[i]);
    }
    Serial.println("");
    digitalWrite(Relay1, HIGH);
    delay(50);
  }
}

NOTE -
I have also changed it code so that the "SIG" pins on the Transmitter and Receiver are both on pin 2 (On their own Arduino). I changed this because I noticed that in the demo code for both the Transmitter and Receiver are setup for pin 2. When I tried to set them up on pins 0 and 1 the Arduinos would not send or receive a signal.

I thought I should also say that if you want, you can find the demo code on the wiki page here:

http://www.seeedstudio.com/wiki/Grove_-_433MHz_Simple_RF_Link_Kit

Transmitting code:

#include <VirtualWire.h>

int RF_TX_PIN = 2;
int Button1 = 13;      //Forward (Button 1)
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Setup Complete");
  vw_set_tx_pin(RF_TX_PIN); // Setup transmit pin
  vw_setup(2000); // Transmission speed in bits per second.
  pinMode(Button1, INPUT);
}
 
void loop()
{
  if (digitalRead(Button1)){
  Serial.println("Forwards");
  const char *msg = "1";
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'Forwards' every 50ms.
  }

  else {
  Serial.println("Stop");
  const char *msg = "0";
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'Forwards' every 50ms.
  }
}

Receiving Code:

#include <VirtualWire.h>
 
int RF_RX_PIN = 2;
int Relay1 = 8;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Serial Connection Complete");
  Serial.println();
  Serial.println("*NOTE* Currently Connected to Receiver");
  vw_set_rx_pin(RF_RX_PIN);  // Setup receive pin.
  vw_setup(2000); // Transmission speed in bits per second.
  vw_rx_start(); // Start the PLL receiver.
  pinMode(Relay1, OUTPUT);
}
 
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, dump HEX
    Serial.print("Got: ");
    for(i = 0; i < buflen; ++i)
    {
      Serial.print(buf[i], HEX);
      Serial.print(" ");
     
     if(buf[i] == '1') {
         digitalWrite(Relay1, HIGH);
      }

     else {
       digitalWrite(Relay1, LOW);
      }
    }
    
    delay(50);
  }
}

Wow! Thanks a ton! I would have never thought of having the controller always transmit stop and then use that as a variable.

It didn't send forward and stop, it sent 1 and 0. There are many other ways to send data, but you should always start small, then build up.