I recently started to dab into Arduinos and have been slowly trying different things. My latest thing is trying to communicate between two arduinos. I’m using a FS1000A wireless transmitter and so far, the connection works, I have everything wired properly, however, I seem to suck at the coding.
I used serial.println for debugging to make sure my transmitter is sending the correct info. This sketch is basically to turn on an LED on the receiver’s end when I press a button on the transmitter’s end. When I transmit, the tx light on both boards function as they should. And the receiver is getting info, I used serial.print for debugging as well, but it seems to be getting the wrong information completely. For example, I’m only sending a value of ‘1’ when a button is pressed, but the receiver is not even getting that and doesn’t even get to the ‘for’ loop:
Transmitter code:
//Transmitter Code
#include <VirtualWire.h>
const int button2Pin = 3; // pushbutton pin
char c ='0';
void setup()
{
Serial.begin(9600);
vw_set_ptt_inverted(true); // Required for DR3100
pinMode(button2Pin, INPUT);
digitalWrite(button2Pin,HIGH);
vw_setup(2000);
vw_set_tx_pin(13);
}
void loop()
{
int button2State;
button2State = digitalRead(button2Pin);
if(button2State == LOW)
{
if(c == '1')
{
c = '0';
}
else
{
c = '1';
}
vw_send((uint8_t *)c, 1);
Serial.println(c);
vw_wait_tx();
delay(200);
}
}
I know some of the code above can be avoided or consolidated, but this is what I came up with after messing with it to try and fix my issue, but the transmitter seems to be doing fine. Here is my receiver code:
//Reciever Code
#include <VirtualWire.h>
void setup()
{
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
Serial.begin(9600);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000);
vw_set_rx_pin(7);
vw_rx_start();
}
void loop()
{
byte buf[VW_MAX_MESSAGE_LEN];
byte buflen = VW_MAX_MESSAGE_LEN;
if(vw_get_message(buf, &buflen))
{
Serial.println(buf[0]); // Testing to see what's in the beginning of the array
for(int i = 0;i < 5;i++)
{
if(buf[i] == '1')
{
Serial.println(buf[i]);
digitalWrite(13,HIGH);
}
else if(buf[i] == '0')
{
digitalWrite(13,LOW);
}
}
}
}
Now when it reads buf[0], it sometimes outputs a 3, sometimes a 7, sometimes a 9, etc. It outputs a ‘3’ about 90% of the time. And the program doesn’t even get to the ‘for’ loop so the second serial.println doesn’t even go. I declared ‘buf’ and ‘buflen’ as byte just to see if there is a difference with uint8_t and it doesn’t change anything.
What am I doing wrong? These are all codes that I modified and added to from the example sketches that came with VirtualWire and that seems to work by sending the string “hello” without a problem. Been googling for answers for a week now, finally gave up and thought I’d open a thread. Any help appreciated.