Pages: 1 [2]   Go Down
Author Topic: I2C Problem, Communicating Between Two Arduinos  (Read 6520 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry about the variable kolik- it is the variable howMany. This mistake was caused by translating code from Czech to English.
The Arduino accessory is used to set value of the msg array. I think that two bytes are going to be send and if there are no data from accessory it's going to send -1s there.

Thanks for your reply
Logged

Peoples Republic of Cantabrigia
Offline Offline
God Member
*****
Karma: 6
Posts: 722
Arduino happiness
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

May I suggest using Bill Porters Easy Transfer I2C edition for this sort of problem? May make your life a lot easier.

He also has a serial edition. I used the serial version at 1MBit/s between two Atmels on one board.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your reply. I'll definitely try it. But I'd rather use I2C because in my project there'll be more than one slave.
Logged

Peoples Republic of Cantabrigia
Offline Offline
God Member
*****
Karma: 6
Posts: 722
Arduino happiness
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your reply. I'll definitely try it. But I'd rather use I2C because in my project there'll be more than one slave.

Should work great. His library includes examples, so hopefully it won't take much to get it all implemented quickly. I2C is a great bus, but be aware of the limitations re: bus length - Phillips originally designed it to be used only on one PCB. That's not to say that it won't work, but buses longer what than the OEM planned for may not be as reliable as you would like.

It's one reason I went to RS485-based buses between my Arduinos. Very good resistance against interference and the possibility of running shielded cables if necessary. But my distances are likely larger than yours...
Logged

West Lafayette
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've had little to no issue with the I2C bus since I got my stuff working, and I'm running it from an mBed to 4 seperate atmega chips, a gyro, and an accelerometer all on the same line. So long as you are routing signals over short distances (within a couple of feet or so) you should be fine. My rule of thumb is to always have a 10k pullup resistor on the SDA and SCL lines for each device you have, so the resistance will decrease as the capacitance added increases. I would also recommend you stick to the Wire library rather than going to an external one as there is more support here on the forum, and outside little issues like this, it is quite robust.

Anyways, I don't know if you are still having trouble with your code but here is my 2 cents on it. Your main problem is that you did not include your array length in your Wire.write command. The syntax is void write(*char data, int length). So you are only sending 1 byte, thus the reason why only the last byte is sent as that is what the index pulls up first. Double check the reference page: http://arduino.cc/en/Reference/WireWrite

Also why do you have a 10ms delay in the receiver? Essentially what is going to happen to your I2C line will be a really long pull-down of the SCL then a very rapid stream of data, I wouldn't be surprised if things got lost in that. The i2c request and receive functions are both run through interrupts, and generally you do not want to stick delays (especially of the ms variety, you could get away with micro delays) into interrupts as you want them to run as fast as possible.

There are other quirks and shenanigans in your code that you probably will have to debug out, but I think the issue related to the I2C bus is that you are sending a data array without specifying the data array length as the library wants.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your response, it's finally working.
Logged

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

Quote
Thank you for your response, it's finally working.
Great. So, what was the problem? How did you correct it?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem was in using Wire.write() without buffer length in the slave code. Correct command in this case is Wire.write(msg,2);
Logged

Pages: 1 [2]   Go Up
Jump to: