I2C problem

hello all
I have problem with sending data sensors with I2c betwin two arduino mega (sensors reader is slave )
I read slave_sender and master_reader example
but this example send and read only one data
this is my reading sensor value

#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN A15 
DHT dht(DHTPIN, DHTTYPE);
float temp;
float hum;
bool gas;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  temp = dht.readTemperature();
 hum = dht.readHumidity();
 Serial.println ("temp:");
 Serial.println(temp);
  Serial.println ("hum:");
 Serial.println(hum);
   Serial.println ("gas:");
 Serial.println(digitalRead(A14));
 
 delay(2000);
 
}

and this is slave sender

{
  Wire.write("hello "); 

}

but I dont know how change this to send several data
can you help me?

a

p990: a

b

Try to understand how master-slave communication works. Which Arduino is the master, which one the slave?

Please post all your code for both master and slave.

this is master

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Wire.requestFrom(8, 30);    // request 6 bytes from slave device #8

  while (Wire.available())   // slave may send less than requested
  {
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

and this is slave

// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN A15 
DHT dht(DHTPIN, DHTTYPE);
float temp;
float hum;
bool gasstate;
void setup()
{
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop()
{ temp = dht.readTemperature();
 hum = dht.readHumidity();
gasstate=digitalRead(A14);
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write("temp:"); 
  Wire.write(temp);
  Wire.write("hum:");
  Wire.write(hume);
  Wire.write("gas state:");
  Wire.write(gasstate);
   
  
  // respond with message of 6 bytes
  // as expected by master
}

but when I seee the serial port only one thing is printed
what should i do

Send some carriage returns and line feeds, or print some at the receiving end.

You need to fix those comments they are rubbish.

Also those variables you are sending are not bytes.

thank for your respon I want to konw how I can send string whit i2c?

You send it one byte at a time.

A misspelled variable hume? Floats are 4 bytes I don't think you want bool for your gas state if you are reading an analog input. Looks like you are using a Mega because of the A14. If not, you need to make a change. I realize your remarks are from the sample.

{ Wire.write("\r\ntemp: "); // adds a carriage return and a line feed plus a space so add 3 more bytes Wire.write(temp); Wire.write("\r\nhum: "); // adds a carriage return and a line feed plus a space so add 3 more bytes Wire.write(hum); Wire.write("\r\ngas state: "); // adds a carriage return and a line feed plus a space so add 3 more bytes Wire.write(gasstate);

Make gasstate an int that is still 4 bytes bool is a 1 or a 0, true or false. This way it would be 39 bytes instead of 30 bytes and make it easy to read. What you are getting it is writing on top of itself.

Didn't test it but I think I might have you headed in the right direction.