i2c problem.incorrect data from one arduino to another.


so approach project once again.i have stopwatch on 1 arduino plus nrf24l01 feedback data.i wanted save data , know using 2 spi modules prove difficult.so connected slave arduino master via i2c , hooked sd card.so wanted sent float(time) slave arduino cannot done unless create union did(i think).
although testing union on master gives correct results, when slave receives data  takes bytes incorrectly.ive made union 2 arrays b[2] can see code bellow, @ b[0] seconds take time correct @ b[1] false data.is there wrong regarding union structure? or code i2c wrong?please im out of ideas.
master code
code: [select]
#include <liquidcrystal.h>
#include <wire.h>
liquidcrystal lcd(7, 6, 5, 4, 3, 2);


void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
  wire.begin(); // join i2c bus (address optional master)
  serial.begin(9600);
 
  pinmode(8, input);
  digitalwrite(8, high);
  pinmode(9, input);
  digitalwrite(9, high);
  pinmode(10, input);
  digitalwrite(10, high);
 

}
double = 0;
double = millis();
double c ;
unsigned int g;
double f ;
unsigned int p;


 union u_tag {
   int b[2];
   //float fval;
 } u;

void loop()
{
 
lcd.clear();
lcd.print("press start");
delay(100);
 
 if(digitalread(8) ==high)
 {

    lcd.clear();
 a = millis();
   while(digitalread(9) == low)
   {
   
     c = millis();
   i = (c - a) / 1000;
   g= (c - a);
   p=i;
   lcd.print(i);
   lcd.setcursor(11,0);
   lcd.print("sec's");
   lcd.setcursor(0,0);
   serial.println(c);
   serial.println(a);
   serial.println(i);
   serial.println("......");
//   delay(100);
 f=(g - p*1000) ;
 //f milliseconds :25
 //i whole time 02:25
u.b[0]=i ;
u.b[1]=f ;
serial.println(u.b[0]);
serial.println(u.b[1]);
 
 
   }
   
   if(digitalread(9) == high)
   {
     while(digitalread(8) == low)
     {
       lcd.setcursor(0,0);
       lcd.print(i);
       lcd.setcursor(11,0);
       lcd.print("sec's");
       lcd.setcursor(0,0);
//       delay(100);
       
   
 
     }
     
      serial.print("f=");
     serial.print(p);
     serial.print(":");
     serial.println(f);
     wire.begintransmission(9); // transmit device #4

//wire.write( p);   
  //wire.write(f);
   wire.write(u.b[0]);
   wire.write(u.b[1]);
 //sends 1 byte 
wire.endtransmission();    // stop transmitting
 


 }

 }
 

}

slave code
code: [select]
#include <wire.h>
#include <spi.h>
#include <sd.h>
const int chipselect = 10;
void setup()
{
 wire.begin(9);                // join i2c bus address #4
 wire.onreceive(receiveevent); // register event
 serial.begin(250000);           // start serial output
 serial.println("slave setup");
  serial.print("initializing sd card...");

  // see if card present , can initialized:
  if (!sd.begin(chipselect)) {
    serial.println("card failed, or not present");
    // don't more:
    return;
  }
  serial.println("card initialized.");
}
union u_tag {
 int b[2];
   //float fval;
 } u;
 //int f ;
  //int p;
void loop()
{
 

// function executes whenever data received master
// function registered event, see setup()
void receiveevent(int howmany)
{
 while(1 < wire.available()) // loop through last
 {
 
 
  //p = wire.read();    // receive byte integer
  //f = wire.read();
  u.b[0] = wire.read();
  u.b[1] = wire.read();
 serial.print("time is:");
 //serial.print(p);         // print integer
 //serial.print(":");
  //serial.println(f);
  serial.print(u.b[0]);
  serial.print(":");
  serial.println(u.b[1]);
  file datafile = sd.open("datalog.txt", file_write);

  // if file available, write it:
  if (datafile) {
   datafile.print(u.b[0]);
    datafile.print(":");
    datafile.println(u.b[1]);
    datafile.close();
 
 
  }
  // if file isn't open, pop error:
  else {
    serial.println("error opening datalog.txt");
  }
 }
}

if sending float 4 separate bytes, union should composed of byte array of 4 bytes , float. integer (you using signed integer) 2 bytes long.


Arduino Forum > Using Arduino > Project Guidance > i2c problem.incorrect data from one arduino to another.


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts