Arduino data transfer

Hello, I have been working with the 433Mhz transmitter and receiver modules, I am trying to send a port status over to another board in order for it to use it, and for now I am just trying to send raw port data to make out a hexadecimal or even to try to show the binary status of it. For some reason it doesn't work once I pull some of the pins from high to low or from low to high the data just goes all over the place... so if anyone has an answer please help me out here because I have gone clueless...
I am using the VirtualWire library so if anyone needs a reference to that it is here : Virtual Wire Library
I have been using an arduino UNO clone and an "Original" arduino UNO so the board types on the programmer for me are "Nano" and "Uno"

Receiver code:

#include <VirtualWire.h>
int N1 = 0, N2, Sum;
void setup()
{
    Serial.begin(9600);
    Serial.println("setup");
  vw_set_rx_pin(A1);
    vw_setup(2000);
    vw_rx_start();  
}

void loop()
{
   Sum = 0;
 N1 = 0;
 N2 = 0;
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) 
    {
      
	int i;

        digitalWrite(13, true); 
	Serial.print("Got: ");
	int X = 0;
	for (i = 0; i < buflen; i++)
	{
  


	    //Serial.print(buf[i], DEC);
      Serial.write(buf[i]);
	    Serial.print(" ");
	}
 if(buf[0] > 57 || buf[0] < 48) buf[0] = 48;
 if(buf[1] > 57 || buf[1] < 48) buf[1] = 48;
 if(buf[2] > 57 || buf[2] < 48) buf[2] = 48;
 

    N1 = (buf[0]-48);
    N2 = (buf[1]-48);
    if(buf[1] == 0 & buf[2] ==0){
      Sum = Sum + N1;
    }
    else if(buf[2] == 0){
      Sum = N1*10;
      Sum = Sum +N2;
    }
    else{
      Sum = N1 * 100;
      Sum = Sum + N2*10 + (buf[2]-48);
    }
   Sum = N1 +N2;
   Sum = Sum + (buf[2] - 48);
  Serial.print("Buf[0] = ");
   Serial.println(buf[0]);
   Serial.print("Buf[1] = ");
   Serial.println(buf[1]);
   Serial.print("Buf[2] = ");
   Serial.println(buf[2]);
   
	Serial.println("");
 Serial.println(Sum, BIN);
 
        digitalWrite(13, false);
    }
}

Transmitter code:

#include <VirtualWire.h>

void setup(){
 // Serial.begin(9600);
  vw_setup(2000);
  vw_set_tx_pin(A1);
}

void loop(){
  int X = PINB;         // Tried using PIND as well...
//  char Y = (X);
//  X >> 4;
 // Serial.println(Y, HEX);
  const char msg[4];
  sprintf(msg, "%d", X);
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx();
  delay(200);
  Serial.println(*msg);
 // Serial.println(msg);
}

Please post your transmitter side serial debug output.

  int X = PINB;         // Tried using PIND as well...

PINB is an unsigned 8 bit value. Why are you storing that in a signed, 16 bit variable?

  const char msg[4];
  sprintf(msg, "%d", X);
  vw_send((uint8_t *)msg, strlen(msg));

The vw_send() method is perfectly capable of sending a uint8_t value in binary. Converting the value to a string is NOT necessary.

But, lets say that you did convert 0x0F to a string. The string would contain "15\0", and you'd send two bytes.

 if(buf[0] > 57 || buf[0] < 48) buf[0] = 48;
 if(buf[1] > 57 || buf[1] < 48) buf[1] = 48;
 if(buf[2] > 57 || buf[2] < 48) buf[2] = 48;

You have NO idea what is in buf[2], but you diddle with it, anyway, and now buf contains '1', '5', '0', and some other junk.

    N1 = (buf[0]-48);
    N2 = (buf[1]-48);
    if(buf[1] == 0 & buf[2] ==0){
      Sum = Sum + N1;
    }

N1 will be 1. N2 will be 5. You have constrained buf[0], buf[1], and buf[2] to be between 48 and 57, so the test for buf[1] equal to 0 and for buf[2] equal to 0 are useless. Bitwise anding false and false is NOT the same as logically anding false and false, though, lucky for you, the result is the same.

Since false & false is still false, the body won't be executed.

    else if(buf[2] == 0){
      Sum = N1*10;
      Sum = Sum +N2;
    }

Well, buf[2] is constrained to be between 48 and 57, so it can't possibly be 0, so the body of this statement will never be executed.

So, this clause will ALWAYS be executed.

    else{
      Sum = N1 * 100;
      Sum = Sum + N2*10 + (buf[2]-48);
    }

At this point, even though you sent 15, Sum will be 150.

   Sum = N1 +N2;
   Sum = Sum + (buf[2] - 48);

But, then you crap all over the value, and assign Sum the value of 6. What, exactly, were you smoking?

is this:

Serial.println(*msg);

equivalent to this:

Serial.println(msg[0]);

?

Well, I have been trying to change the receiver code a little, the transmitter is fine so far so I'd rather not to touch it... This is the receiver part so far, I have set all the pins on the port at logical '1' and on the debugger of the receiver it shows me that buf[0] = 54 (which is 6 in ascii), buf[1] = 51 (which is 3 in ascii) and for some odd reason buf[2] shows me that it has the number 51 inside of it as well.... so what am I doing wrong because I am clearly out of any clue by now....

#include <VirtualWire.h>

void setup()
{
Serial.begin(9600);
Serial.println("setup");
vw_set_rx_pin(A1);

// vw_set_ptt_inverted(true);
vw_setup(2000);

vw_rx_start();
}

void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen))
{

int i;

digitalWrite(13, true);
Serial.print("Got: ");
int X = 0;
for (i = 0; i < buflen; i++)
{

Serial.write(buf*);*

  • Serial.print(" ");*
    }
  • if((buf[2]) == 0){*
  • buf[2] = buf[1];*
  • buf[1] = buf[0];*
  • }*
    _ N1 = (buf[0]-48)*100;_
    _ N2 = (buf[1]-48)*10;_
  • Sum = N1 +N2;*
  • Sum = Sum + (buf[2] - 48);*
  • Serial.print("Buf[0] = ");*
  • Serial.println(buf[0]);*
  • Serial.print("Buf[1] = ");*
  • Serial.println(buf[1]);*
  • Serial.print("Buf[2] = ");*
  • Serial.println(buf[2]); *
    Serial.println("");
  • digitalWrite(13, false);*
  • }*
    }

the transmitter is fine so far so I'd rather not to touch it.

It really isn't. From a receiving point of view, it would be far simpler if you KNEW that you would always get the same number of bytes.

Sending the 8 bit value in binary means that you always get ONE byte.

If you really, really, for some undisclosed reason, need to send the data as text, then do it intelligently.

  const char msg[4];
  sprintf(msg, "%03d", X);
  vw_send((uint8_t *)msg, strlen(msg));

The 03 that I added to the format means to always use 3 digits, padding with 0s up front as required. So, 15 will be send as "015".

Then, the receiving end becomes dirt simple:

void loop()
{
   uint8_t buf[VW_MAX_MESSAGE_LEN];
   uint8_t buflen = VW_MAX_MESSAGE_LEN;

   if (vw_get_message(buf, &buflen))
   {
      buf[buflen] = '\0'; // Add a NULL to the array, after the last valid byte

      int someMeaningfulName = atoi((char *)buf);

Of course, this change does not really rely on changing the transmitter. Using functions, like atoi(), that were written by people that understand programming is better, in my opinion, than trying to do everything yourself.

Wel, I just added what you've pointed out at the transmitter part, which was the "%03d" and yes, it does work now, so as far as I understand, you split that number from the X into three numbers, and even if there is a 0 at the beginning it sends it out as a "0" and then the 2nd number and so on right?

and well, I am trying to learn it myself by fucking it up, fixing it and actually understanding what I have done wrong, it's pretty much the first time I am asking for help from others since I have struggled with this one pretty well...
But thanks for the help anyways

Wel, I just added what you've pointed out at the transmitter part, which was the "%03d" and yes, it does work now, so as far as I understand, you split that number from the X into three numbers, and even if there is a 0 at the beginning it sends it out as a "0" and then the 2nd number and so on right?

Yes. 7 is sent as "007". 15 is sent as "015". 128 is sent as "128". 3000 is sent as "3000".

and well, I am trying to learn it myself by fucking it up, fixing it and actually understanding what I have done wrong, it's pretty much the first time I am asking for help from others since I have struggled with this one pretty well...

If your goal is to learn programming and string handling, that is great. If your goal is to get the ^)&%)* project done, use tools other people have written/tested/debugged. That was my only point.