I2C slave send data from multiple sensor to the Master Problem!

Hello! I have successfully accomplished to send data from one sensor over I2C from the slave to the Master (I used a light sensor). But for my project I want to send from the slave data from more than one sensor. For that purpose I have planed to use a potentiometer. I have tried a lot to send data from two sensors but all my attempts have failed. Below is the Master and the slave code which is working for one sensor.

Master Code

#include<Wire.h>

void setup()
{
  Wire.begin();
  Serial.begin(9600);
 }

void loop()
{   
     Wire.requestFrom(5,1);
        while(Wire.available())
      {    
        int c = Wire.read();
        int data=map(c,0,225,0,1023);
        Serial.print(c);
        Serial.print('\n'); 
        delay(500);
     }
  }

Slave Code

#include <Wire.h>

int ligthSensor=A0;
int led=13;
int led2=7;
int potensiometer=A1;
int datatosend[2];
int data;
int sensor;

void setup()
{
  pinMode(ligthSensor,INPUT);
  pinMode(led,OUTPUT);
  pinMode(potensiometer,INPUT);
  pinMode(led2,OUTPUT);
  Serial.begin(9600);
  Wire.begin(5);
  Wire.onRequest(requestEvent);
}

void loop()
{
  data=analogRead(A0);
  if(data > 300)
  {
    digitalWrite(led,HIGH);
  }
  else if (data < 300)
  {
    digitalWrite(led,LOW);
  }
  
  sensor=analogRead(A1);
  if(sensor > 300)
  {
    digitalWrite(led2,HIGH);
  }
  else if (sensor < 300)
  {
    digitalWrite(led2,LOW);
  }
  Serial.print(data);
  Serial.print('\t');
  Serial.println(sensor);
  Serial.print('\n'); 
  Serial.print('\n'); 
   delay(1000);
    
}

void requestEvent()
{
  int mapdeddata=map(data,0,1023,0,225);
  int mapedpoitensiometer=map(sensor,0,1023,0,225);
  datatosend[0]=mapdeddata;                                    //store the data to an array
  datatosend[1]=mapedpoitensiometer;

Wire.write(mapdeddata);
  
}

Can anyone help me on that problem. I have tried to save the data from the two sensors to an array and inside the "Wire.write" I have put the array, but the only thing that I could receive from the master was zeros.

It was very good for me that none answer to me because I have searched on my own and I found the answer to my problem by my self.

I post the code for the slave (which is the writer) for others that are facing the same problem and also the code for the master, works fine when I tested. The master code is almost the same as the code above.

Slave code:

// Wire Slave Sender

#include <Wire.h>
int table[]={0,0,0};
int lightSensor1=A0;
int lightSensor2=A1;
int button=8;

void setup()
{
  Serial.begin(9600);
  pinMode(lightSensor1,INPUT);
  pinMode(lightSensor2,INPUT);
  pinMode(button,INPUT);
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  table[0]=map(analogRead(lightSensor1),0,1023,0,255);
  table[1]=map(analogRead(lightSensor2),0,1023,0,255);
  if(digitalRead(button)==HIGH)
  {
    table[2]=1;
  }else{
    table[2]=0;
  }
  Serial.print(table[0]);
  Serial.print('\t');
  Serial.print(table[1]);
  Serial.print('\t');
  Serial.print(table[2]);
  Serial.print('\n');
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
//  for(int i=0;i<3;i++)
//  {
  uint8_t Buffer[3];
  Buffer[0]=table[0];
  Buffer[1]=table[1];
  Buffer[2]=table[2];
  
  Wire.write(Buffer,3); 
   
 }

Master Code:

// Wire Master Reader
#include <Wire.h>
int table[]={0,0,0};
int led1=51;
int led2=52;
int led3=53;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
}

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

    for(int i=0;i<3;i++)
    { 
    int c = Wire.read(); // receive a byte as character

    Serial.print(c); 
    table[i]=c;
    Serial.print('\t');    
    
    }
     Serial.print('\n');
       Serial.print(table[0]);
        Serial.print('\t'); 
       Serial.print(table[1]);
        Serial.print('\t'); 
       Serial.print(table[2]);
       Serial.print('\n');
    
    if(table[0]>150)
    {
      digitalWrite(led1,HIGH);
    }else{
      digitalWrite(led1,LOW);
    }
    
    if(table[1]>150)
    {
      digitalWrite(led2,HIGH);
    }else{
      digitalWrite(led2,LOW);
    }
    if(table[2]==1)
    {
      digitalWrite(led3,HIGH);
    }else{
      digitalWrite(led3,LOW);
    }    
  delay(500);
}

This is an excellent job ! Thanks for sharing :slight_smile: :slight_smile: :slight_smile:

thank you very much.

Thanks Bro !

Hi. Thank you for this idea, how about there are 4 arduinos, 1 pair of arduino will communicate and also the other pair, but only two wires will be using? How can I address it ?? I attached an image. hope you can help me? thank you

You can send the data immediately from a shared buffer. But take care to disable interrupts while updating that buffer, else you risk garbled values. A copy inside the ISR does not help against such garbage.