hello everyone, i having problem with i2c communucation.
my setup is 2 arduino. one is mega the master and the second is a mega also a slave.
the master requestfrom the slave.
basically the slave has 7 sensor attach to it.
when i try with array of size 7(tomaster[7]) it work correctly,the array get pass to the master but with array of size 9( tomaster[9]) the array is not pass.
Am passing an array of data type float.(volatile float tomaster[9]
Any help will be most welcome.
here is my code:
My slave:
//i2c slave mega
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS A2
OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensorstemp(&ourWire);
// array to send to master
volatile float tomaster[9];
//----//
//variable used by temperature sensor
float temperature=0.00;
//------//
//variable used by voltage sensor
int val11;
float val2;
float voltage=0.00;
//-------//
//variable used by light sensor
int sensorPin = 0;//lightsensor on A0 on uno
int lightsensor;
//-------//
// lowest and highest sensor readings for rain sensor:
const int sensorMin = 0;Â Â // sensor minimum
const int sensorMax = 1024;Â // sensor maximum
float range;
//------//
// variable for mq3 sensor:
int mq3val=0;
//------//
// variable for mq135 sensor:
int mq135val=0;
//------//
// variable and pins for distance sensor:
#include <NewPing.h>
#define TRIGGER_PINÂ 14Â // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PINÂ Â 15Â // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int distances;
//------//
void setup() {
Serial.begin(9600);
Wire.begin(5);//Address of slave uno
Wire.onRequest(requestEvent);
/*-( Start up the DallasTemperature library )-*/
sensorstemp.begin();
}
void loop() {
Â
Â
// reading the temperature sensor in celsius
sensorstemp.requestTemperatures(); // Send the command to get temperatures
temperature=sensorstemp.getTempCByIndex(0);
delay(50);
//-------//
//reading the voltage sensor from 0.00 to 25v
float volt;
val11=analogRead(A1);
volt=val11/4.092;
val2=(volt/10);
voltage=val2;
delay(50);
//-------//
//reading light sensor data from 0 - 960 A0
int sensorValue = analogRead(sensorPin);
lightsensor=(sensorValue);
delay(50);
//---------------//
// read the sensor on analog A3:
 int sensorReading = analogRead(A3);
 // map the sensor range (four options):
 // ex: 'long int map(long int, long int, long int, long int, long int)'
range = map(sensorReading, sensorMin, sensorMax, 0, 3);
delay(50);
//------//
// read the sensor on analog A4:
mq3val=analogRead(A4);
delay(50);
//------//
// read the sensor on analog A5:
mq135val=analogRead(A5);
delay(50);
//------//
// read the sensor on tx & rx pin:
// Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
delay(50);
distances=sonar.ping_cm(); // Send ping, get distance in cm and print result (0 = outside set distance range)
//------//
// storing in volatile array.
// The interrupts are disabled, to prevent that the requestEvent has half-a-written value.
noInterrupts();Â Â Â Â Â // disable interrupts.
tomaster[0] = lightsensor; // storing the lightsensor to array nodepayload
tomaster[1] = voltage;Â Â // storing the voltage to array nodepayload
tomaster[2] = temperature;// storing the temperature to array nodepayload
tomaster[3] = range;
tomaster[4] = mq3val;
tomaster[5] = mq135val;
tomaster[6] = distances;
tomaster[7] = 51.508131;
tomaster[8] = -0.128002;
interrupts();Â Â Â Â Â Â Â // enable interrupts
//--------//
//for debuging purpose
//Serial.println(lightsensor);
//Serial.println(tomaster[0]);
//Serial.println(voltage);
//Serial.println(tomaster[1]);
//Serial.println(temperature);
//Serial.println(tomaster[2]);
//Serial.println(range);
//Serial.println(tomaster[3]);
//Serial.println(mq3val);
//Serial.println(tomaster[4]);
//Serial.println(mq135val);
//Serial.println(tomaster[5]);
//Serial.println(distances);
//Serial.println(tomaster[6]);
//------//
}
//end of loop//
//onrequest function
void requestEvent(){
//tomaster is my array
Wire.write((char*)tomaster, sizeof(tomaster));
 }
The master:
#include <Wire.h>
float fromslave[9];
void setup() {
 Serial.begin(9600);
 Wire.begin();    // Activate I2C link
}
void loop() {
Â
 Wire.requestFrom(5, sizeof(fromslave)); Â
 int n = Wire.available();   Â
 if( n == sizeof(fromslave)) {  Â
  Wire.readBytes( (char *) fromslave, sizeof(fromslave)); Â
 Â
  for( int i=0; i<9; i++) {
   Serial.println(fromslave[i]);
  }
  Serial.println("-----");
 }
 delay(500);  // slow down sketch, run loop once a second.
}