Send float data using I2C from Arduino to Arduino


hi, have problem send float data ds18b20 (temperature sensor) arduino i2c communication. source code master , slave. in serial monitor slave, got temperature in celcius, when serial monitor in master there's nothing (blank). can help? before

--master code--
#include <wire.h>

void setup()
{
wire.begin(); //join i2c bus
serial.begin(9600);
}

void loop()
{
wire.requestfrom(2,3);
while(wire.available())
{
  int a=wire.read();
  char c=wire.read();
  int b=wire.read();
 
  serial.print("nilai 1");
  serial.println(a);
    serial.print("nilai 2");
  serial.println(c);
    serial.print("nilai 3");
  serial.println(b);
}
delay (500);
}

--slave code--
#include <onewire.h>
#include <wire.h>

#define temperature_error -1000
#define ds18s20_id 0x10
#define ds18b20_id 0x28


int ds18s20_pin = 2; //ds18s20 signal pin on digital 2

//temperature chip i/o
onewire ds(ds18s20_pin);  // on digital pin 2

void setup(void)
{
  serial.begin(9600);
  wire.begin(2); //join i2c bus dengan address #2
  wire.onrequest(requestevent); //register event
}

void loop(void)
{
  tampilserial();
}


float gettemperature()
{
  //returns temperature 1 ds18s20 in deg celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr))
  {
      ds.reset_search();
      return temperature_error; // no more sensors on chain, reset search
  }

  if ( onewire::crc8( addr, 7) != addr[7])
  {
      return temperature_error; //crc not valid!
  }

  if ( addr[0] != ds18s20_id && addr[0] != ds18b20_id)
  {
      return temperature_error; // device not recognized
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion
  delay(850); // wait time...
 
  ds.reset();   // byte present = ds.reset();
  ds.select(addr);   
  ds.write(0xbe); // read scratchpad command

  (int = 0; < 9; i++)  // receive 9 bytes
  {
    data = ds.read();
  }
 
  ds.reset_search();
 
  /* memory consuming readable code:
  byte msb = data[1];
  byte lsb = data[0];

  float tempread = ((msb << 8) | lsb); //using two's compliment
  float temperaturesum = tempread / 16;  // 1/16 = 0.0625
  return temperaturesum;
  */
   
  return ( (data[1] << 8) + data[0] )*0.0625 ;

}

void tampilserial()
{
    float temperature = gettemperature();
  if ( temperature != temperature_error )
  {
     serial.println(temperature);
  }
 
  delay(1000); //just here slow down output easier read
}

void requestevent()
{
  float temperature=gettemperature();
  if(temperature != temperature_error)
  {
    int whole = temperature;
    int fract = (temperature-whole)*10;
    wire.write(whole);
    wire.write('.');
    wire.write(fract);
  }
}

now, without smilies , italics, please.


Arduino Forum > Using Arduino > Programming Questions > Send float data using I2C from Arduino to Arduino


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support