Go Down

Topic: How i2c can't be work with UNO and pro mini send lots of Data. (Read 535 times) previous topic - next topic

Phodal

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: [Select]

#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: [Select]

#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

}


???????????????

PaulS

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.

Phodal

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: [Select]
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.
???????????????

Nick Gammon

Code: [Select]
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.
http://www.gammon.com.au/electronics

Go Up