choosing where to send data via i2c

Hi everybody,

I'm new on the i2c communication between 2 arduinos, but i've already mastered the basic communication of 1 byte.

I have one arduino which is getting data constantly from 8 capacitive sensors
What I want to do is send what are those sensors capting to the other arduino.

The idea i've came up with is sending 8 different values to 8 different "positions" of the slave-receiver arduino.

How could i do that?

for the moment i have:

#include <WSWire.h>

//#include <Wire.h>
#include <CapacitiveSensor.h>

CapacitiveSensor   cs_7_2 = CapacitiveSensor(7,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_7_3 = CapacitiveSensor(7,3);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_7_4 = CapacitiveSensor(7,4);        // R 10M entre pin 4 i 6. pin 6 en mode sensat
int sensor1 =  cs_7_2.capacitiveSensor(30);
int sensor2 =  cs_7_3.capacitiveSensor(30);
int sensor3 =  cs_7_4.capacitiveSensor(30);

void setup() {
  Wire.begin();   // join i2c bus (address optional for master)
  cs_7_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  Serial.begin(9600);
}


void loop() {
int opcion=1;
if (opcion==1){

int measure[]={sensor1,sensor2};

for(int thisMeasure; thisMeasure<2; thisMeasure++){
  sensor1 =  cs_7_2.capacitiveSensor(30);
  sensor2 =  cs_7_3.capacitiveSensor(30);
 if (measure[thisMeasure]>25) {
switch (thisMeasure) {     //escollim quin data enviem segons quina posició estiguem de measure 
  case 0:
  Wire.beginTransmission(8);
  Wire.write(sensor1);
  Wire.endTransmission();
  break;
  case 1:
  Wire.beginTransmission(8);
  Wire.write(sensor2);
  Wire.endTransmission();
  break;
}
}
if (thisMeasure==1) thisMeasure=0;
}
}

help

Start by explaining why you chose I2C as the communication interface between the two Arduinos. Why are you using two Arduinos? Has one a separate location far away? Does it make sense to let the receiving Arduino be the slave in the I2C communication?

You probably see that it's a good idea to tell us more about your project, we might help you to get the best way to transport that information between the Arduinos (or eliminate the need to have two of them).

How could i do that?

You can't. There is one "position" (the other Arduino) to send data to.

What you have to do is send sufficient data to the other Arduino so that it knows what to do with the data.

Your code, as posted, makes little sense, unless maybe you are familiar with the cap sense library.

CapacitiveSensor   cs_7_2 = CapacitiveSensor(7,2);
CapacitiveSensor   cs_7_3 = CapacitiveSensor(7,3);
CapacitiveSensor   cs_7_4 = CapacitiveSensor(7,4);

You create three instances. Fine.

int sensor1 =  cs_7_2.capacitiveSensor(30);
int sensor2 =  cs_7_3.capacitiveSensor(30);
int sensor3 =  cs_7_4.capacitiveSensor(30);

It is not at all obvious what this code is doing, or whether it makes sense to call these methods outside of a function.

int opcion=1;
if (opcion==1){

It doesn't make sense to test if the variable you just assigned a value to holds the value you just assigned.

int measure[]={sensor1,sensor2};

You have three sensors, so creating a two element array doesn't make sense.

 sensor1 =  cs_7_2.capacitiveSensor(30);
  sensor2 =  cs_7_3.capacitiveSensor(30);
 if (measure[thisMeasure]>25) {

The fact that you, at some point in the past, copied the current value of sensor1 into the array does not mean that the array contains the value you just read.

It doesn't make sense to read all the sensors on each pass through the for loop when only one of the sensor's values will be dealt with.

The Wire.write() method does NOT take an int. The value in sensorN will be truncated to a byte, and sent to the other Arduino.

What you need to do is a create a 3 element byte array, and put the sensor NUMBER in the first position and the sensor reading's high and low bytes in the other two positions, and then use the write() method that takes an array and a count, to send the three element array.

PaulS:
You can't. There is one "position" (the other Arduino) to send data to.

What you have to do is send sufficient data to the other Arduino so that it knows what to do with the data.

Your code, as posted, makes little sense, unless maybe you are familiar with the cap sense library.

CapacitiveSensor   cs_7_2 = CapacitiveSensor(7,2);

CapacitiveSensor  cs_7_3 = CapacitiveSensor(7,3);
CapacitiveSensor  cs_7_4 = CapacitiveSensor(7,4);



You create three instances. Fine.



int sensor1 =  cs_7_2.capacitiveSensor(30);
int sensor2 =  cs_7_3.capacitiveSensor(30);
int sensor3 =  cs_7_4.capacitiveSensor(30);



It is not at all obvious what this code is doing, or whether it makes sense to call these methods outside of a function.



int opcion=1;
if (opcion==1){



It doesn't make sense to test if the variable you just assigned a value to holds the value you just assigned.



int measure[]={sensor1,sensor2};



You have three sensors, so creating a two element array doesn't make sense.



sensor1 =  cs_7_2.capacitiveSensor(30);
  sensor2 =  cs_7_3.capacitiveSensor(30);
if (measure[thisMeasure]>25) {



The fact that you, at some point in the past, copied the current value of sensor1 into the array does not mean that the array contains the value you just read.

It doesn't make sense to read all the sensors on each pass through the for loop when only one of the sensor's values will be dealt with.

The Wire.write() method does NOT take an int. The value in sensorN will be truncated to a byte, and sent to the other Arduino.

What you need to do is a create a 3 element byte array, and put the sensor NUMBER in the first position and the sensor reading's high and low bytes in the other two positions, and then use the write() method that takes an array and a count, to send the three element array.

Sorry for the late response.
About the number of sensors, there's going to be 8 different sensors, but for the moment there's only 2 so that's why i was using an array.

My idea is to keep the reading of the sensors going on all the time. If one of these sensors is between 100 and 255 then send a byte to the slave, telling him which sensor has been touched and light one specific Led depending on this.

Maybe the for loop is not well programmed.