Loading...
Pages: 1 [2]   Go Down
Author Topic: TWI/I2C - Arduino to Arduino, sending float  (Read 1661 times)
0 Members and 1 Guest are viewing this topic.
NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice work thanks.

As mentioned, what I wrote I never said was the best solution - I am not a programmer by any stretch of the imagination.
Just trying to help.

Appreciate the updated code.

Regards
James
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6809
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm a bit late to this thread but you don't need floats to work with fractional values. Use large ints and work in 10ths or 100th of the units you need, then format accordingly when you present the data to a human.

______
Rob
Logged

Rob Gray aka the GRAYnomad http://www.robgray.com

Offline Offline
Edison Member
*
Karma: 114
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your original code should work.

In cases like this, I typically use a low-level function to isolate the user code from the actual protocol, like this:

Code:
//send a byte with a protocol
void send_byte(unsigned char dat) {
//implement your protocol here
}

//send a word with send_byte()
void send_word(unsigned short wrd) {
  unsigned char i=sizeof(wrd);
  unsigned char *ptr=(unsigned char *) &wrd;
  while (i--) send_byte(*ptr++);
}

//send a float with send_byte()
void send_float(float flt) {
  unsigned char i=sizeof(flt);
  unsigned char *ptr = (unsigned char *) &flt;
  while (i--) send_byte(*ptr++);
}

You can pretty much expand it to cover any other data types.

It has the advantage of providing some stability to your code: if you wish to change the protocol through which the data is sent, you just change send_byte(), as everything else is built on top of it.
Logged

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

See: http://arduino.cc/forum/index.php?topic=104732.0

You could make a struct, put 6 floats in it and then use I2C_Anything to send the struct.


Hi,

I was asking for the code. I tried the above code and it works perfect! I just have to change few things ( master has to request slave to send).

Thank you so much!  smiley

Bye
Logged

Pages: 1 [2]   Go Up
Print
 
Jump to: