Virtual Wire Receiver reading wrong info

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.

The transmitter program sends just one character, but the receiver attempts to read out and interpret 5 values in the character array buf -- why? You should not ignore the return value &buflen in if(vw_get_message(buf, &buflen))

I think you are confusing the ASCII value for a character with the character itself. For example, the ASCII value for the character '0' is 48. I think in this case (but haven't tested it) Serial.print(buf[ i ]) prints the number contained in buf[ i ] (declared as byte), not the character.

Thanks, I actually had the for loop as i <=buflen and tried out integers as a way to figure it out if it makes a difference, and it doesn’t.

I’ll play with the code a little bit more to see if I can get it to read the character instead of the byte, you may be right about that.

Thanks for your help, after a bit more dabbling, I was able to figure it out, I can now successfully control an LED with a push of a button remote, here is my code if anyone is interested, may help others in the future:

Transmitter:

//By Nazmus
//EasyProgramming.net
//Transmitter Code 
//transmit character to turn on and off an LED using Virtual Wire

#include <VirtualWire.h>

const int button2Pin = 3;  // pushbutton  pin
const char *msgs = "0"; // initial value for msgs
void setup()
{
  Serial.begin(9600); //For Debugging
  vw_set_ptt_inverted(true); // Required for DR3100
  pinMode(button2Pin, INPUT); // input for push button
  digitalWrite(button2Pin,HIGH); //sets pin status - not really necessary
  vw_setup(2000); 
  vw_set_tx_pin(13); //transmit pin
}

void loop()
{
  int button2State; // declare button state, will either be HIGH or LOW
  
  button2State = digitalRead(button2Pin); // Reads button state

    if(button2State == LOW) 
    {
      if(msgs == "0") // If button is pressed and msgs value is 0, change to 1
      {
        msgs = "1";
      }
      else if(msgs == "1")
      {
        msgs = "0"; // If button is pressed and msgs value is 1, change to 0
      }

      vw_send((uint8_t *)msgs, strlen(msgs)); // Send the data
      Serial.println(*msgs); // Print on screen for debugging
      vw_wait_tx(); // Waits for message to be finished sending
      delay(200);
    }
 }

Receiver:

//By Nazmus
//EasyProgramming.net
//Receiver Code 
//transmit character to turn on and off an LED using Virtual Wire

#include <VirtualWire.h>

void setup()
{
  pinMode(13,OUTPUT); // LED Output pin
  digitalWrite(13,LOW); // Turn off to begin with
  
  Serial.begin(9600); // For debugging
  vw_setup(2000);
  vw_set_rx_pin(7); // Receive pin (pin 13 used for LED)
  vw_rx_start();  // Start listening
}

void loop()
{
  uint8_t buflen=VW_MAX_MESSAGE_LEN; // Max length of message
  uint8_t buf[buflen]; // Array with incoming data
  
  if(vw_get_message(buf, &buflen))  // Check to see if anything is being transmitted
  {
    for(int i=0;i<buflen;i++)
    {
      Serial.println(buf[i]); // Print for debugging
      if(buf[i]=='1') 
      {
        Serial.println(buf[i]);
        digitalWrite(13,HIGH); // If the character received is a 1, turn on LED
      }
      else if(buf[i]=='0')
      {
        digitalWrite(13,LOW); // If the character received is a 0, turn off LED
      }
    }
  }
}