Pages: [1]   Go Down
Author Topic: two Arduino boards communicating on I2C  (Read 387 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi...!!!
I have connected two Arduino board with I2C and trying to communicate but it is giving some wrong answer

Code for board1
Code:
#include <Wire.h>
int mm;

void setup()
{Wire.begin(0x05);
Serial.begin(9600);
}
void loop()
{int rtrn;
 
byte outgoingByte[12] = {0x50,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B};


//For payload camera
 Wire.beginTransmission(6);
 //for(mm=0;mm<12;mm++)
// Wire.write(outgoingByte, sizeof outgoingByte);
  Wire.write(0x1A);
 
 rtrn = Wire.endTransmission();
 delay(1000);
 Serial.println(" Returning Success on Writing to the Payload::");
Serial.println(rtrn,DEC);
//Wire.requestFrom(6, 2);   
//while(!Wire.available());   
char c = Wire.read(); 

Serial.println("Payload data read:");
Serial.println(c,HEX);//}
delay(100);
Serial.println(' ');
Serial.println(' ');  } 





Code for board2

Code:
#include <Wire.h>
int mm;
void setup()
{Wire.begin(0x06);
Serial.begin(9600);
}
void loop()
{int rtrn;
 
byte outgoingByte[12] = {0x02,0x03,0x04,0x03,0x04,0x07,0x06,0x07,0x08,0x09,0x0A,0x0B};


//For payload camera
 Wire.beginTransmission(5);
  //Wire.write(outgoingByte, sizeof outgoingByte);
 Wire.write(0x1F);
 rtrn = Wire.endTransmission();
 delay(1000);
 Serial.println(" Returning Success on Writing to the Payload::");
Serial.println(rtrn,DEC);
//Wire.requestFrom(5, 2);   
//while(!Wire.available());   
char c = Wire.read();   
Serial.println("Payload data read:");
Serial.println(c,HEX);//}
delay(100);
Serial.println(' ');
Serial.println(' ');  } 



Output both the sides are same

Code:
Returning Success on Writing to the Payload::
0
Payload data read:
FFFFFFFF
 
 
 Returning Success on Writing to the Payload::
0
Payload data read:
FFFFFFFF
 
 
 Returning Success on Writing to the Payload::
0
Payload data read:
FFFFFFFF
 
 
 Returning Success on Writing to the Payload::
0
Payload data read:
FFFFFFFF
 
 
 Returning Success on Writing to the Payload::
0
Payload data read:
FFFFFFFF
 
 
 Returning Success on Writing to the Payload::
0
Payload data read:
FFFFFFFF
 
 
 Returning Success on Writing to the Payload::
0
Payload data read:
FFFFFFFF

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are a number of coding styles in general use. Some place the { at the end of the statement, like
Code:
void loop() {
if(something == somethingElse) [
while(notTimeToQuit) {

Some place the { on the next line, like
Code:
void loop()
{
if(something == somethingElse)
[
while(notTimeToQuit)
{

NONE of them call for placing ANYTHING on the line after the {. So, don't do that.

The coding styles ALL agree that the } goes on a line by itself. One } per line and NOTHING else on the line. Fix your code.

The coding styles also agree that indentation is to be consistent (and non-zero).

Now, I'm not a slave to convention, but your code is very hard to read.

Clearly, the commented out code is not part of your problem, so we don't need to see it. So, delete it.

But, it seems clear that you really don't understand I2C. There are no callbacks registered for when data is received. Using delay() and then expecting data to be present is not a good idea. Especially, since the other device is sitting on its hands doing nothing a lot, too.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now, I'm not a slave to convention

That would explain the unconventional use of brackets, then:

Code:
if(something == somethingElse) [
Code:
if(something == somethingElse)
[
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That would explain the unconventional use of brackets, then:
No, those would be explained by fat fingers. Or, really, a stupid sticky shift key. Damn thing.
Logged

Pages: [1]   Go Up
Jump to: