'Sending string using i2c commuication on two arduino micro-controller
i am using two arduino mcu to connect two lines of LED-matrix display. I tried using i2c communication to connect a master mcu (which controls the first line of the LED-matrix display) and slave mcu (which controls the second line of LED-matrix display). I need to pass a string data(consist of 300 characters) from the master to the slave, so that i can display the same string to the second line of the LED-matrix display.
The problem is, I can't pass a string variable using Wire.read() from master to slave. My solution to this, was to convert the string to character before using wire.read() to transmit the data, but the slave cannot receive the whole string, only the first few characters. I also had problem in the timing of the display, the second line of the LED-matrix(which is controlled by the slave), displays the string very late.
This is the sample code for the master mcu
//Master Code
String inData;
String LED_DATA;
char buf[300];
void Input(void){
while(Serial.available() > 0)
{
char received = Serial.read();
inData+=received;
if(received == '~')
{
LED_DATA = inData;
inData.toCharArray(buf,300);
Wire.beginTransmission(5);
Wire.write(buf);
Wire.endTransmission();
}
}
}
And this is the sample code for the slave
//slave
char LED_DATA[100];
void setup(){
Wire.begin(5);
Wire.onReceive(receiveEvent);
}
void receiveEvent(int howMany){
while(Wire.available()){
LED_DATA[300] = Wire.read();
}
}
I am new to arduino and microcontroller. What is the easiest way possible to solve my problem? Thank you very much.
Solution 1:[1]
You can pass string to Wire.write(), by passing the variable as a char * . So if String a = "test"; was the earlier declaration, try using Char* a = "test"; it will send the data through
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | arul praveen |