Pages: 1 [2] 3 4   Go Down
Author Topic: Arduino Wireless Remote -- Help Please!  (Read 3854 times)
0 Members and 1 Guest are viewing this topic.
Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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):
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 = "Forwards";
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'Forwards' every 50ms.
  delay(50);
  }
}

Car (Receiver):
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(" ");
  //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.
« Last Edit: March 16, 2013, 06:21:48 pm by MaxKulik » Logged

Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Transmitting code:
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:
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);
  }
}
« Last Edit: March 16, 2013, 06:36:09 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah thats fine. I also wanted to get a second button to work to trip a second relay. I tried this but it doesn't seem to work:

Send:
Code:
#include <VirtualWire.h>

int RF_TX_PIN = 2;
int Button1 = 13;      //Forward (Button 1)
int Button2 = 12;      //Backwards (Button 2)
 
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);
  pinMode(Button2, 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 if(digitalRead(Button2)){
  Serial.println("Backwards");
  const char *msg = "2";
  vw_send((uint8_t *)msg, strlen(msg)); // Send 'Backwards' every 50ms.
  }
 
  else {
  Serial.println("Stop");
  const char *msg = "0";
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'Stop' every 50ms.
  }
}

Receive:
Code:
#include <VirtualWire.h>
 
int RF_RX_PIN = 2;
int Relay1 = 8;
int Relay2 = 9;
 
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);
  pinMode(Relay2, 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') {                 // Triggers Relay 3
         digitalWrite(Relay1, HIGH);     // Turn Realy 1 on
      }
     
     if(buf[i] == '2') {                 // Triggers Relay 2
         digitalWrite(Relay2, HIGH);     // Turn Realy 2 on
      }

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

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your not turning the second relay off. Try adding digitalWrite(Relay2, LOW); somewhere.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried this but it will not compile and I don't really know why:
It gives me
Code:
expected } at end of input

Code:
#include <VirtualWire.h>
 
int RF_RX_PIN = 2;
int Relay1 = 8;
int Relay2 = 9;
 
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);
  pinMode(Relay2, 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') {                 // Triggers Relay 3
         digitalWrite(Relay1, HIGH);     // Turn Realy 1 on
      }
     else {
       digitalWrite(Relay1, LOW);
    }
     if(buf[i] == '2') {                 // Triggers Relay 2
       digitalWrite(Relay2, HIGH);     // Turn Realy 2 on
    }
     else {
       digitalWrite(Relay2, LOW);
     }
    delay(50);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you used Tools + Auto Format once in a while, or the tab key consistently, you'd see that you don't have matching } for every {.
Logged

Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When I try to do that It tells me this:

"Auto Format Canceled: Too many left curly braces."
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, and that's why. Add a closing bracket after the delay.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok let me try that
Logged

Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, adding the bracket worked. Now I need to fix the controller. Using this code:

Code:
#include <VirtualWire.h>

int RF_TX_PIN = 2;
int Button1 = 13;      //Forward (Button 1)
int Button2 = 12;      //Backwards (Button 2)
 
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);
  pinMode(Button2, 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 if(digitalRead(Button2)){
  Serial.println("Backwards");
  const char *msg = "2";
  vw_send((uint8_t *)msg, strlen(msg)); // Send 'Backwards' every 50ms.
  }
 
  else {
  Serial.println("Stop");
  const char *msg = "0";
  vw_send((uint8_t *)msg, strlen(msg));  // Send 'Stop' every 50ms.
  }
}

It just keeps sending "Backwards" over and over again.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check your wiring on button 2, it wont send anything if it is not HIGH.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Muskego WI
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Be nice to nerds... Chances are, you'll end up working for one. -Bill Gates
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My wiring is correct. I don't even have to press anything and it will just spam "Backwards".
Logged

Pages: 1 [2] 3 4   Go Up
Jump to: