Hi there. I am trying to create a very simple(at first) setup using two arduino's and a couple of rf units(transmitter and receiver)
to turn an led on remotely. i am using the virtual wire library to achieve my goal.i have managed to get them both talking using the example code which came with the library.
i have now modified the code so that i can send data from one arduino from serial monitor. the receiver seems to only intermittently 'catch' the data however and the if statement control which i have implemented doesn't work at all. if you could have a look at my code and offer some suggestions i would be very grateful. please forgive my ignorance, i am new to this jive.
transmitter code:
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600); // Debugging only
// Initialise the IO and ISR
vw_set_tx_pin(12);//connected to the data pin of the rf transmitter
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(4000); // Bits per sec
}
void loop()
{
if (Serial.available() > 0) {
const char* msg = (const char*) Serial.read();
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
}
receiver code:
/const int ledPin = 13, ledPin1 = 11, ledPin2 = 10; // the pin that the LED is attached to
boolean counter_1 = 0, counter_2 = 0, counter_3 = 0; //variables to keep track of the led state
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600);
// initialize the LED pins as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Initialise the IO and ISR
vw_set_rx_pin(12);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(4000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
//digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
int i;
if (buf[i] == '1')
{
digitalWrite(ledPin,HIGH);
}
}
}
}
KenF:
Stop casting the value returned by Serial.read to something that it's not. Here's a scenario,
your Serial.read returns a value of 'A' (so that's 65). Why would you then want to look at what's stored at memory address 65?
As AWOL has pointed out, casting it to a pointer is totally wrong.
reason i tried that out is because vw_send wont accept an integer. i get 'invalid conversion from int to const char*' so i thought that perhaps if i did the conversion outside of the method(and please again forgive me if i am not making utter sense here, i am working with my limited knowledge on this subject) then it might work. turns out it did. or at least it compiled.
AWOL:
"gonadgranny" and "dannygrogan"
Merely anagrams who both happen to be Sky Broadband customers and both seem to be called Danny, or sock-puppets?
Just askin'
yes they are both me. it was not intentional, i forgot i already had an account set up here.
gonadgranny:
reason i tried that out is because vw_send wont accept an integer. i get 'invalid conversion from int to const char*' so i thought that perhaps if i did the conversion outside of the method(and please again forgive me if i am not making utter sense here, i am working with my limited knowledge on this subject) then it might work. turns out it did. or at least it compiled.
If someone demands payment by credit card but you only have hard cash, you wouldn't just pass them the serial number of a banknote and expect that to be accepted as a credit card would you? You'd put the cash into a credit card account and pass the credit card details.
Likewise, if you have a char but your function wants a pointer to a char array, you need to put that char into a char array and pass your function the pointer to that array.
Likewise, if you have a char but your function wants a pointer to a char array, you need to put that char into a char array and pass your function the pointer to that array.
But, since you have an int, and the function expects a byte array, you have a problem. Now, it's not insurmountable, and not even really difficult.
A char and a byte are the same size, and can be cast to each other. The same holds trur of char and byte arrays. So, if you have a char array, say one populated by sprintf() from the int, you could cast that as a byte array (or byte pointer) so that vw_send() would like it.