Hi Master, Why my program can't transfering data from slave to master, I'm using arduino uno as slave and master
#include <Arduino.h>
#include <Wire.h>
template <typename T> unsigned int I2C_writeAnything (const T& value)
{
const byte * p = (const byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
Wire.write(*p++);
return i;
} // end of I2C_writeAnything
template <typename T> unsigned int I2C_readAnything(T& value)
{
byte * p = (byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
*p++ = Wire.read();
return i;
} // end of I2C_readAnything
master Code
#include <Wire.h>
#include "I2C_Anything.h"
const byte SLAVE_ADDRESS1 = 1;
const byte SLAVE_ADDRESS2 = 2;
void setup()
{
Serial.begin(9600); // start serial for output
Wire.onReceive (receiveEvent);
}
volatile boolean haveData = false;
volatile float tempC;
volatile float h;
void loop()
{
Wire.begin (SLAVE_ADDRESS1 );
if (haveData)
{
Serial.print (tempC);
delay (1000);
Serial.print (h);
haveData = false;
delay (1000);
}
Wire.begin (SLAVE_ADDRESS2 );
if (haveData)
{
Serial.print (tempC);
delay (1000);
Serial.print (h);
haveData = false;
delay (1000);
}
delay (200);
}
void receiveEvent (int howMany)
{
if (howMany >= (sizeof tempC) + (sizeof h))
{
I2C_readAnything (tempC);
I2C_readAnything (h);
haveData = true;
} // end if have enough data
// delay(500);
}
Salve 1 Code
#include <Wire.h>
#include <LiquidCrystal.h>
#include "DHT.h"
#include "I2C_Anything.h"
#define tempPin A0
#define DHTPIN A1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const byte MY_ADDRESS = 1;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
float tempC;
void setup()
{
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);
pinMode (12, OUTPUT);
lcd.begin(16, 2);
Wire.begin (MY_ADDRESS);
Serial.begin (9600);
//Wire.onRequest(requestEvent);
}
void loop()
{
//delay(100);
float tempC;
float h;
int tempPin = 0;
h = dht.readHumidity();
float t = dht.readTemperature();
tempC = analogRead(tempPin); //pembacaan nilai dari sensor
tempC = (5.0*tempC*100.0/1024.0); //Converter dari tegangan ke suhu
if (isnan(t) || isnan(h)) {
//Serial.println("Failed to read from DHT");
}
else {
lcd.setCursor(0,0);
lcd.print("Temp=");
lcd.print(tempC);
tempC = analogRead(tempPin); //read the value from the sensor
tempC = (5.0*tempC *100.0/1024.0);
lcd.print(" \337C");
lcd.setCursor(0,1);
lcd.print("Humidity=");
lcd.print(h);
lcd.print("% ");
delay(100);
// long foo = 42;
}
//for (float fnum = 1; tempC <= 10; fnum += 0.015)
{
Wire.beginTransmission (MY_ADDRESS);
I2C_writeAnything (tempC);
I2C_writeAnything (h);
Wire.endTransmission ();
delay (200);
} // end of for
}
Slave 2
#include <Wire.h>
#include <LiquidCrystal.h>
#include "DHT.h"
#include "I2C_Anything.h"
#define tempPin A0
#define DHTPIN A1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const byte MY_ADDRESS = 2;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
float tempC;
void setup()
{
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);
pinMode (12, OUTPUT);
lcd.begin(16, 2);
Wire.begin (MY_ADDRESS);
Serial.begin (9600);
//Wire.onRequest(requestEvent);
}
void loop()
{
//delay(100);
float tempC;
float h;
int tempPin = 0;
h = dht.readHumidity();
float t = dht.readTemperature();
tempC = analogRead(tempPin); //pembacaan nilai dari sensor
tempC = (5.0*tempC*100.0/1024.0); //Converter dari tegangan ke suhu
if (isnan(t) || isnan(h)) {
//Serial.println("Failed to read from DHT");
}
else {
lcd.setCursor(0,0);
lcd.print("Temp=");
lcd.print(tempC);
tempC = analogRead(tempPin); //read the value from the sensor
tempC = (5.0*tempC *100.0/1024.0);
lcd.print(" \337C");
lcd.setCursor(0,1);
lcd.print("Humidity=");
lcd.print(h);
lcd.print("% ");
delay(100);
// long foo = 42;
}
//for (float fnum = 1; tempC <= 10; fnum += 0.015)
{
Wire.beginTransmission (MY_ADDRESS);
I2C_writeAnything (tempC);
I2C_writeAnything (h);
Wire.endTransmission ();
delay (200);
} // end of for
}