Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write an Arduino program that simply recieves a string (via the I2C wire library) from a master Arduino, then waits for a request, and sends that string back.

Here is my code:

#include <Wire.h>

void setup()
{
  Wire.begin(4); 
  Wire.onReceive(receiveEvent); 
  Wire.onRequest(requestEvent);
}

String data = "";

void loop()
{

}

void receiveEvent(int howMany)
{
  data = "";
  while( Wire.available()){
    data += (char)Wire.read();
  }
}

void requestEvent()
{
    Wire.write(data);
}

I read in the API that the write() function accepts a string, but I keep getting a "No matching function for call" error. I tried to simply replace

Wire.write(data);

with

Wire.write("test");

and that worked without error. Why is this the case?

share|improve this question
up vote 3 down vote accepted

data is a String. "test" is a char*. Wire.write() has no prototype that takes a String.

Wire.write(data.c_str());
share|improve this answer
    
That fixed the problem. Thanks! – Hoytman Mar 6 '15 at 20:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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