5

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?

asked Mar 6, 2015 at 20:17
1
  • Try this instead of wire.write(data); wire.print(data); Commented Aug 31, 2018 at 19:25

1 Answer 1

12

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

Wire.write(data.c_str());
answered Mar 6, 2015 at 20:19
1
  • working perfectly :) Commented Aug 28, 2019 at 9:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.