Pages: [1]   Go Down
Author Topic: How i2c can't be work with UNO and pro mini send lots of Data.  (Read 520 times)
0 Members and 1 Guest are viewing this topic.
China
Offline Offline
Newbie
*
Karma: 0
Posts: 6
keep it simple & stupid
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I try to send temperature data form the Arduino pro mini to the Arduino UNO.But lose a lot data but the first .
I think the i2c work like a multi-threading .So,I do this code.But How I could send a data easy???
The temperature it a double number,I couldn't recevie the data formally.
Code:
#include <Wire.h>

int test[]={};
int i=0;
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(400);
  Serial.println("fd");
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int h)
{
 
    while(1 < Wire.available()) // loop through all but the last
    {
      char c = Wire.read(); // receive byte as a character
      Serial.print(c);         // print the character
    }
   int  x=Wire.read();
   Serial.println(x);
   delay(100);
}



Code:
#include <Wire.h>
void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop()
{
  double z=15.52;
  int t=(int)z;
  int y;
  y=(z-t)*100;
  byte ts=14;
 
  delay(500);
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write(y);
  Wire.endTransmission();    // stop transmitting
  delay(500);
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write(t);
  Wire.endTransmission();    // stop transmitting
  delay(500);
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write(ts);
  Wire.endTransmission();    // stop transmitting

}


Logged

没有一点点防备,也没有一丝顾虑

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

You are sending 2 ints and a byte, in three separate transmissions. That will result in three separate calls to the receive event. The first two will have two bytes to read. The second will have one.

In the 2 byte case, you are reading/storing the first and printing it as a char, and then reading/storing the second and printing it as an int. I don't think you are loosing data as much as mangling it.

Though you've offered no proof that there is a problem - no serial output - nor have you explained HOW the two Arduinos are connected, nor any explanation as to why there are two for such a simple task.
Logged

China
Offline Offline
Newbie
*
Karma: 0
Posts: 6
keep it simple & stupid
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the event will receive the data with be pause.And the loop function with continue to the work.So the result it shows:
Code:
15
fd
52
fd
14
fd
fd
as so on...
I don't know how I can receive such like the 15.52 and store it.last it change from Arduino example,but just a simple example.
Logged

没有一点点防备,也没有一丝顾虑

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19343
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void receiveEvent(int h)
{
 
    while(1 < Wire.available()) // loop through all but the last
    {
      char c = Wire.read(); // receive byte as a character
      Serial.print(c);         // print the character
    }
   int  x=Wire.read();
   Serial.println(x);
   delay(100);
}

This is an interrupt service routine. Don't call Serial.print inside it. Don't call delay. Then get back to us.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1]   Go Up
Jump to: