Portugal
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« on: August 24, 2012, 10:40:17 am » |
Good afternoon, first of all my congratulations to this community. My problem is this, I bought 2 arduinos, and i wanted to make communication between them by i2c . I wanted to send one value, and then send the contents of two arrays. For this I followed this link http://arduino.cc/en/Tutorial/MasterReader but not working. The program is huge so I will only show the Send function, I did. void setup(){ //in setup i have this, and more other thing, but of communitation i have only this Wire.begin(); in loop i have this sender(val, array1, array2, , cont1, cont2); //val is a int val, array1 is a first array, array2 is second array and cont1 is size of first array and cont2 is the size of second array my function sender is void enviar(unsigned int nblocos,unsigned int *posicoes, unsigned int *valorespos, unsigned int conter1, unsigned int conter2 ){ Wire.beginTransmission(4); // transmit to device #4 Wire.write(nblocos); for (int i= 0; i< (conter1); i++){ Wire.write(posicoes ); } for (int i= 0; i< (conter2); i++){ Wire.write(valorespos); } Wire.endTransmission(); delay(1000); }
and my receiver have this
#include <Wire.h>
void setup() { Wire.begin(4); // join i2c bus with address #4 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output }
void loop() { delay(100); }
// function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while(1 < Wire.available()) // loop through all but the last { int x = Wire.read(); // receive byte as an integer Serial.println(x); // print the integer }
But i dont print anything
the usb is connected in receiver, and i connecter 5v in arduino transmiter to Vin arduino receiver (arduino with usb)
What i make wrong
thanks a lot
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35511
Seattle, WA USA
|
 |
« Reply #1 on: August 24, 2012, 10:53:06 am » |
What i make wrong A number of things. You did not tell us how the Arduinos are connected, to start with. The program is huge so I will only show the Send function, I did. For another. You need to create a simple sketch to send data. When that is known to work, you can then incorporate that into a larger sketch.
|
|
|
|
|
Logged
|
|
|
|
|
Colorado
Offline
Full Member
Karma: 0
Posts: 140
|
 |
« Reply #2 on: August 24, 2012, 11:38:03 am » |
I found this Introduction to I2C very helpful when I setup my first one a few weeks ago. You may want to carefully examine the wiring diagram (where the author has 3 Arduinos) in that tutorial. It can be important to have pull-up resistors. I have 3 ATmega328Ps communicating with I2C and they all share a single pair of pull-up resistors.
|
|
|
|
« Last Edit: August 24, 2012, 11:41:49 am by BlueJakester »
|
Logged
|
|
|
|
|
Portugal
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #3 on: August 26, 2012, 11:12:21 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Colorado
Offline
Full Member
Karma: 0
Posts: 140
|
 |
« Reply #4 on: August 26, 2012, 02:56:56 pm » |
^^^ That shouldn't be difficult. I'm new at all of this and always start out with a simple, fundamental example of something such as I2C, XBee, clocks, displays, etc. When I have the basic configuration working I move on to expand it or accomplish whatever the project goals are. That has been a good way for me to learn and it may work for you too.
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #5 on: September 05, 2012, 11:24:18 am » |
Hello everybody, sorry to be a bother again, but have not had success in transmission i2c. Someone could tell me what this evil in these two programs, the master and the slave. Is not operating. Master#include <Wire.h>
int array[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64}; unsigned int sizearr=0; unsigned int num=0;
void setup(){ Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master) }
void loop() { sizearr=64; num=4; enviar(num, array, sizearr); }
void enviar(unsigned int numbers, int *arr, unsigned int sizear){ //valorpos é valores da posiçao, sizear é o sizearray, cont2 e' o contador 2 //enviar o start bit Wire.beginTransmission(4); // transmit to device #4 Wire.write(numbers);
// enviar conteudo do array four for (int i= 0; i< (sizear); i++){ Wire.write(arr[i]); delay(500); } Wire.endTransmission(); }
Slave#include <Wire.h>
void setup() { // join i2c bus with address #4 Serial.begin(9600); // start serial for output }
void loop() { delay(100); Wire.begin(4); Wire.onReceive(receiveEvent); // register event }
// function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while(1 < Wire.available()) // loop through all but the last { int x = Wire.read(); // receive byte as an integer Serial.println(x); // print the integer } }
Thanks 
|
|
|
|
« Last Edit: September 05, 2012, 12:05:58 pm by capucho10p »
|
Logged
|
|
|
|
|
Leeds, UK
Offline
Newbie
Karma: 0
Posts: 29
|
 |
« Reply #6 on: September 05, 2012, 11:56:45 am » |
You dont' say what actually happens or what does not happen that you expect to. for (int i= 0; i< (sizear); i++){ Wire.write(arr); delay(500); } looks wrong as you are constantly sending the same byte. I suspect you want something like Wire.write(arr[i]); Paul
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #7 on: September 05, 2012, 12:09:22 pm » |
Thanks paul, but in my original program a have arr[ i], but i dont put code, and arr[ i] is arr ahahahha
I dont find the problem, and I checked the connections.
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Faraday Member
Karma: 69
Posts: 3281
|
 |
« Reply #8 on: September 05, 2012, 12:09:57 pm » |
@OP: always use the code tags for code to avoid the interpretation problems that countrypaul mentioned. An array index with variable i: array[i] = a; get to this without the tags: array = a;
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #9 on: September 05, 2012, 08:30:44 pm » |
@OP: always use the code tags for code to avoid the interpretation problems that countrypaul mentioned. An array index with variable i: array[i] = a; get to this without the tags: array = a;
I make this and nothing. Any ideias?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35511
Seattle, WA USA
|
 |
« Reply #10 on: September 06, 2012, 05:47:41 am » |
Hello everybody, sorry to be a bother again, but have not had success in transmission i2c.
You STILL haven't told us how the two Arduinos are connected. You MUST tell us that.
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #11 on: September 06, 2012, 07:44:31 am » |
First, my apologies for my bad english. My mounting hardware is equal to this.  but the powering the Arduinos are independently,i connect the 5V output of the Master Arduino to the VIN pin on the slave.The slave is the arduino that have the usb, to print que values.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35511
Seattle, WA USA
|
 |
« Reply #12 on: September 06, 2012, 08:07:04 am » |
Why is the slave doing this? void loop() { delay(100); Wire.begin(4); Wire.onReceive(receiveEvent); // register event } Wire.begin() and Wire.onReceive() should be called in setup(), not loop(). Your master is not giving the slave any time to do anything. It hammers data to it as fast as possible. int x = Wire.read(); // receive byte as an integer Once again, why? There is no possible benefit to storing the byte in an int.
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #13 on: September 06, 2012, 08:59:43 am » |
Pauls i make the changers and nothing. I dont know why, this dont work. #include <Wire.h>
void setup() { Serial.begin(9600); // start serial for output // join i2c bus with address #4 Wire.begin(4); Wire.onReceive(receiveEvent); // register event delay(100); }
void loop() { }
// function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while(1 < Wire.available()) // loop through all but the last { byte x = Wire.read(); // receive byte as an integer Serial.println(x); // print the integer } } #include <Wire.h>
//unsigned int array[]={1,2,3,4,5,6,7,8}; int array[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64}; unsigned int sizearr=0; unsigned int num=0;
void setup(){ Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master) }
void loop() { sizearr=64; num=4; enviar(num, array, sizearr); }
void enviar(unsigned int numbers, int *arr, unsigned int sizear){ //valorpos é valores da posiçao, sizear é o sizearray, cont2 e' o contador 2 int a=0;
//enviar o start bit Wire.beginTransmission(4); // transmit to device #4 Wire.write(numbers);
// enviar conteudo do array four for (int i= 0; i< (sizear); i++){ a=arr[i]; Wire.write(a); delay(500); } Wire.endTransmission(); } It's not working  initially was using int, because later on I'll have to send negative number. I only want send two arrays by i2c ahahah
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35511
Seattle, WA USA
|
 |
« Reply #14 on: September 06, 2012, 09:53:35 am » |
It's not working Doesn't tell us squat. Both slave and master have Serial.begin() statements, but the master never prints anything to the serial port. How do you know that it is sending anything to the slave? The onReceive() event needs to print something when it is called. Otherwise, you haven't a clue that the slave is doing anything. delay(500); Why? Why are you making the master wait half a second between values sent?
|
|
|
|
|
Logged
|
|
|
|
|
|