Go Down

Topic: Arduino Wireless Remote -- Help Please! (Read 5904 times) previous topic - next topic

MaxKulik

#15
Mar 17, 2013, 12:17 am Last Edit: Mar 17, 2013, 12:21 am by MaxKulik Reason: 1
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: [Select]

#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: [Select]

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

MaxKulik

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

HazardsMind

#17
Mar 17, 2013, 12:33 am Last Edit: Mar 17, 2013, 12:36 am by HazardsMind Reason: 1
Transmitting code:
Code: [Select]
#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: [Select]
#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);
  }
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

MaxKulik

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

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

MaxKulik

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: [Select]

#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: [Select]

#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);
  }
}

HazardsMind

Your not turning the second relay off. Try adding digitalWrite(Relay2, LOW); somewhere.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

MaxKulik

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

Code: [Select]

#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);
  }
}

PaulS

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

MaxKulik

When I try to do that It tells me this:

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

HazardsMind

Yes, and that's why. Add a closing bracket after the delay.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

MaxKulik


MaxKulik

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

Code: [Select]

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

HazardsMind

Check your wiring on button 2, it wont send anything if it is not HIGH.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

MaxKulik

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy