About what? The evils of Strings? Do some research. Read the source code.
About how to send an array? I could say the same thing. But, the write() method is overloaded to send arrays. The overload takes two arguments - where the data starts and the amount of data to send.
I reworked the code to pass time from a Yun to a Mega but I still don't get an output on the Mega and I cannot find the problem.
Any help is appreciated.
Yun SCL -> SCL Mega
Yun SDA -> SDA Mega
No pull-up resistors are used
/* Master board Yun */
#include <Wire.h>
#include <Process.h>
String dateTime;
int strlength;
char b;
void setup() {
Serial.begin(9600);
Wire.begin(); // join i2c bus as Master
}
void loop() {
Bridge.begin();
dateTime = getTimeStamp(); // store date & time
Wire.beginTransmission(8); // transmit to device #8
strlength = dateTime.length();
for (int i = 0; i >= strlength; i++)
{
b = dateTime.charAt(i);
Wire.write(b);
}
Wire.endTransmission(); // stop transmitting
delay(1000);
}
String getTimeStamp() {
String result;
Process time;
time.begin("date");
//time.addParameter("+%D-%T");
time.addParameter("+%T"); // shows time
time.run();
while (time.available() > 0) {
char c = time.read();
if (c != '\n')
result += c;
}
return result;
}
/* Mega as slave */
#include <Wire.h>
String dateTime;
char v;
void setup() {
Wire.begin(8); // join i2c bus set device #8
Serial.begin(9600); // start serial for output
}
void loop() {
while (Wire.available()) { //
dateTime = ""; // clear string
v = Wire.read();
dateTime += v;
}
delay(500);
Serial.println(dateTime); // window is scrolling but no data is printed
}
while (Wire.available()) { //
dateTime = ""; // clear string
v = Wire.read();
dateTime += v;
}
I think that if you describe what this code does, in English, there will come a palm-to-forehead moment. If not, post the text here, and I'll supply the palm-to-forehead moment.
Perhaps you should simply print each character received. If you don't print anything, it's because you don't receive anything, and that is a hardware problem not a software problem.
By the way, receiving data from Wire is generally done by registering an onReceive handler and writing the handler.