Monday, 27 June 2016

I2C Master Slave Communication Between Two Arduino

Arduino Come with Many Libraries which are useful for Many of us Out there.. But it will Be So much pain if we wanted to do Something other than Those Examples.

Recently I am Working on I2C Communication Between Two Arduinos and the simple Hello From Examples in Arduino IDE Worked Fine, But I struggled  More than 2 Days to send the Integer value by Editing the Original Example.

In this Process the the materials Provided by Nick Gammon in his Website is helped Me alot..

 From Nick Gammon Site : Warning - because of the way the Wire library is written, the requestEvent handler can only (successfully) do a single send. The reason is that each attempt to send a reply resets the internal buffer back to the start. Thus in your requestEvent, if you need to send multiple bytes, you should assemble them into a temporary buffer, and then send that buffer using a single Wire.write. For example:
This is Where I got Struck.. 

and another pitfall is  I Am trying to Read the Integer data And Converting it to the String using  
char valC[10]; // Define the Variable to store the Sesor Data
itoa(val,valC,10); // Convert the Int into String
But I got Struck on How many Bytes i need to request from the Slave 2 or 3 or 4 And if I Requested 10 bytes and I only got 3 bytes value and In Serial monitor it is Printing Like

I have No Idea what is that Weird Character is (Y with a Hat on it..) So I asked the Google the same Question 

So that weird character is the ASCII equivalent for 255. so I wrote a small code to Process that One in Master Side.
  •  for (int j = 0; j < len; j++)
    1.   {

    2.     if ((char)valR[j] != (char)255) 
    3.             {
    4.               valF[valF_Index] = valR[j];
    5.               valF_Index++;
    6.             }

    7.   }
    8.   valF[valF_Index] = '\0';
    9.   Serial.println(valF);
    which gives me the Final String..

    You can Find the Total Code For Master and Slave in My GitHub Page