Converting int > String > Char* and back. For use with VirtualWire RF

PaulS:

I've managed to convert my sensor data from int to a String and then to a char array

Why use the String? You can go from int to char array (itoa()) and back (atoi()) directly.

Because I from what I read itoa() and atoi() seemed a bit complicated. But it turned out not to be that hard at all.

  Sensor1String.toCharArray(Sensor1CharMsg,(Sensor1String.length()+1));

The second argument is the number of characters that the array can hold, NOT the number of characters in the String. The String object already knows how many characters it has.

This is a bit null and void since I went with the other solution but I had problems getting only 2 digits when I were supposed to get 3 and so on. Adding +1 helped.

  for (int i = 0; i <= Sensor1String.length(); i++) {

Serial.print(Sensor1CharMsg[i]);
  }



The array being printed here is already NULL terminated. There is no reason to print it one character at a time.


Serial.print(Sensor1CharMsg);

Also not relevant anymore but for the sake of education, thank you.

 digitalWrite(13, true); // Flash a light to show transmitting

This does not flash a light. It turns it on. As anyone can see.

I know, this actually comes from the VirtualWire example code. But I've changed the comment to be more correct.

On the receiver, you need to use the atoi() function to convert Sensor1CharMsg to an int.

Thanks for pointing me in the right direction. I did however get into a problem on the receiving end that I didn't describe here first. The char array I use to store the chars that I later convert to an integer will contain 4 digits once it has been given that number for instance, I send 1023 to it and when I send 906 to it it will actually show 9063, if I go directly to 0 I still have 9063 (or something similar) since the char array never got flushed. I managed to solve that in what I hope is a good enough solution.

Here is the updated transmitter code:

#include <VirtualWire.h>

const int Sensor1Pin = A2;
// const int Sensor2Pin = 3;
const int ledPin = 13;

int Sensor1Data;
//int Sensor2Data;
char Sensor1CharMsg[4];

void setup() {

// LED
pinMode(ledPin,OUTPUT);

// Sensor(s)
pinMode(Sensor1Pin,INPUT);

// for debuggin
Serial.begin(9600);

// VirtualWire setup
vw_setup(2000); // Bits per sec

}

void loop() {
 
  // Read and store Sensor 1 data
  Sensor1Data = analogRead(Sensor1Pin);
 
  // Convert integer data to Char array directly
  itoa(Sensor1Data,Sensor1CharMsg,10);
 
  // DEBUG
  Serial.print("Sensor1 Integer: ");
  Serial.print(Sensor1Data);
  Serial.print(" Sensor1 CharMsg: ");
  Serial.print(Sensor1CharMsg);
  Serial.println(" ");
  delay(1000);

// END DEBUG

digitalWrite(13, true); // Turn on a light to show transmitting
vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false); // Turn off a light after transmission
delay(200);

} // END void loop...





And updated receiver code: 


#include <VirtualWire.h>

int ledPin = 13;

// Sensors
int Sensor1Data;

// RF Transmission container
char Sensor1CharMsg[4];

void setup() {
  Serial.begin(9600);
 
  // sets the digital pin as output
  pinMode(ledPin, OUTPUT);     
   
    // VirtualWire
    // Initialise the IO and ISR
    // Required for DR3100
    vw_set_ptt_inverted(true);
    // Bits per sec
    vw_setup(2000);
   
    // Start the receiver PLL running
    vw_rx_start();

} // END void setup

void loop(){
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
   
    // Non-blocking
    if (vw_get_message(buf, &buflen))
    {
int i;
        // Turn on a light to show received good message
        digitalWrite(13, true);

// Message with a good checksum received, dump it.
        for (i = 0; i < buflen; i++)
{           
          // Fill Sensor1CharMsg Char array with corresponding
          // chars from buffer. 
          Sensor1CharMsg[i] = char(buf[i]);
}
       
        // Convert Sensor1CharMsg Char array to integer
        Sensor1Data = atoi(Sensor1CharMsg);
       
        // DEBUG
        Serial.print("Sensor 1: ");
        Serial.println(Sensor1Data);
       
        // END DEBUG
       
        // Clear Sensor1CharMsg char array for coming messages
        // This needs to be done otherwise problems will occur
        // when the incoming messages has less digits than the
        // one before.
        for (i = 0; i < 4; i++)
        {
          Sensor1CharMsg[i] = 0;
        }
       
        // Turn off light to and await next message
        digitalWrite(13, false);
    }
}