I2CBus

Hi all,

I have developed some code for I2CBus.

I2CBus
Communication between two or more micro controllers/devices

This is only for initialising the I2CBus, Send and receive data. The programe makes use of Wire Library

Master
It manages the Bus and request information from the slave/s devices It unpacks the data received from a "slave" into an array. Which then can be assigned to different variables to execute the relevant code.

Slave
In the current sketch: The Slave microcontroller connects to the bus with the address #8. It reads from sensors attached and packs the information collected in an array. Upon request of the Master microcontroller, it sends the array via bus.

More sensors and devices can be added for further expansion.

Please have a look at my repository and help me/community by improving this.
The project is open source.

/* I2CBus MASTER
 *  Created by Amir Rajabifar 
 */

// including the Wire library 
#include <Wire.h>

// an array to store the received data from device #8 
byte Data[2];

void setup() {
  Wire.begin();                    // join i2c bus (address optional for master)
  Serial.begin(9600);              // starts the erial communication 
}

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

  while (Wire.available()) {       //check if data is available   
    for(byte i = 0; i < 2; i++){
    Data [i] = Wire.read();        // it assigne the data to the array
    Serial.print(Data[i]);         // print the array  
    Serial.print('\t');

/*     
 *      Variabls can be decleard in global scope and can be written to from 
 *      array. 
 *      
 *      RPM = Data[0];
 *      Vol = Data[1];
 *      
 *     So one.... (depending on the application) 
 */
    }
  }
  
  Serial.println();                // Print a new line 

//******************** DO STUFF ********************\\ 
 
}
/* I2CBus SLAVE (#8)
 *  Created by Amir Rajabifar 
 */
 
// including the Wire library 
#include <Wire.h>

// variable to store the reading from sensors
int pot1;
int pot2;

// an array to store the data that need to be sent
byte data[2]; 

void setup() {
  Serial.begin(9600);           // Begin Serial coomunication 
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
// Read from the sensors and assign to variables  
  pot1 = analogRead(A0);
  pot2 = analogRead(A1);
// more sensors can be added
// change a 10 bit number to 8 bit  
  pot1 /= 4;
  pot2 /= 4;

// Write the value of variables to the array
  data[0] = pot1;
  data[1] = pot2;

//Print the array to Serial buffer (for debuging) 
  for (int i = 0; i < 2; i++){
    Serial.print(data[i]); 
    Serial.print('\t');
  }
  Serial.println();
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
// respond with message of 2 bytes as expected by master
  Wire.write(data, 2);
}

Here is the code. Thought i'd post it here as well.