0

My sensor outputs an int. I would like this to be an array of strings so I can send it with my 433MHz transmitter.

Converting an int to a string to an array of strings seems to be extremely complicated. I've tried multiple approaches.

The error is always:

array must be initialized with a brace-enclosed initializer

#include <VirtualWire.h>

const int led_pin = 11;
const int transmit_pin = 12;
const int receive_pin = 2;
const int transmit_en_pin = 3;
int mostureSensor = 0;

void setup()
{
  // Initialise the IO and ISR
  vw_set_tx_pin(transmit_pin);
  vw_set_rx_pin(receive_pin);
  vw_set_ptt_pin(transmit_en_pin);
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(1000);   // Bits per sec
}

byte count = 1;

void loop()
{
  // read the input on analog pin 0:
  String sensorValue = String(analogRead(mostureSensor));

  char charBuf[7];
  char msg[7] = sensorValue.toCharArray(charBuf, 7);

  //char msg[7] = {'h','a','l','l','o',' ','#'};
  Serial.println("sending");
  msg[6] = count;
  digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
  vw_send((uint8_t *)msg, 7);
  vw_wait_tx(); // Wait until the whole message is gone
  digitalWrite(led_pin, LOW);
  delay(1000);
  count = count + 1;
}
1

toCharArray is a method. It will copy the value from SensorValur to charBuff, it won't return the char array.

Line:

 char msg[7] = sensorValue.toCharArray(charBuf, 7);

Should be:

 sensorValue.toCharArray(charBuf, 7);

charBuf will already have the sensorValue copied.

Check more about "toCharArray" method :

https://www.arduino.cc/en/Reference/StringToCharArray

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.