system
March 17, 2013, 1:17am
21
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:
#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:
#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);
}
}
Your not turning the second relay off. Try adding digitalWrite(Relay2, LOW); somewhere.
system
March 17, 2013, 1:47am
23
I tried this but it will not compile and I don't really know why:
It gives me expected } at end of input
#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);
}
}
system
March 17, 2013, 1:55am
24
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 {.
system
March 17, 2013, 2:10am
25
When I try to do that It tells me this:
"Auto Format Canceled: Too many left curly braces."
Yes, and that's why. Add a closing bracket after the delay.
system
March 17, 2013, 3:12am
28
Ok, adding the bracket worked. Now I need to fix the controller. Using this 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.
Check your wiring on button 2, it wont send anything if it is not HIGH.
system
March 17, 2013, 4:47am
30
My wiring is correct. I don't even have to press anything and it will just spam "Backwards".
Something is making button 2 HIGH, so, do you have pull down resistors? You can not have a floating input, otherwise it will give you bad results.
system
March 17, 2013, 7:30am
32
I don't exactly know what a Pull down resistor is. I know what a resistor is though.
system
March 17, 2013, 12:06pm
33
if (digitalRead(Button1)){
The digitalRead() function does not return a boolean. You should NOT be writing code that looks like it does.
system
March 17, 2013, 4:38pm
34
So are you saying the code is set up to return a "True" or "False" statement but in face it is not?
system
March 17, 2013, 4:44pm
35
So are you saying the code is set up to return a "True" or "False" statement but in face it is not?
No. I'm saying that you have written the code in a way that makes it appear as though digitalRead() returns true or false, when, in fact it returns HIGH or LOW.
system
March 17, 2013, 4:46pm
36
Can "True" or False" act as "HIGH" or "LOW"? Or is there a different way I can re-write that statement in order for it to function as it should?
system
March 17, 2013, 6:51pm
37
Alright playing around with the controller code for a while and this is what I have:
#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.
}
}
Everything works, other then the fact that it is always transmitting "2" or backwards. As of right now I am stuck because I even did some research on how if-then-else statements and how they work and I don't see why this doesn't work.
Did you install pull down resistors on the buttons? Do a search for "pull down resistor", and to use them with buttons.
Added: Wait, did you put one the receive code a way to turn off the second relay? Another thing, does the serial monitor put out "Backwards"?
system
March 17, 2013, 7:22pm
39
EDIT No and yes it does say "Backwards"
Is this what I am looking for:
I can do this easy, I just want to make sure I am looking at the right thing. I am currently reading about them too, just to understand them a bit more.
system
March 17, 2013, 7:32pm
40
HazardsMind:
Did you install pull down resistors on the buttons? Do a search for "pull down resistor", and to use them with buttons.
Added: Wait, did you put one the receive code a way to turn off the second relay? Another thing, does the serial monitor put out "Backwards"?
No I don't think I did and Yes it does just print out "Backwards" on the serial monitor, BUT every now-and-then I will see "stop".