Hello Friends!
So iam having a problem with I2C Communication between 2 Arduino basically iam trying to Write a 12 variables like this in Slave mode - Wire.Write(Varchar1,Varchar2,Varchar3) how can i concatenate the values? and send to Master?
this is my code
//Master
#include <Wire.h>
char c;
String teste;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Wire.requestFrom(2,2); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
{
c = Wire.read(); // receive a byte as character
teste += c;
}
Serial.println(teste);
teste = "";
delay(10);
}
// Code for Slave Sender - Program for Arduino 2
// Wire Slave Sender
// by Nicholas Zambetti
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <CapSense.h>
#include <Wire.h>
CapSense cs_2_3 = CapSense(2,3); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapSense cs_2_4 = CapSense(2,4); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapSense cs_2_5 = CapSense(2,5); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapSense cs_2_6 = CapSense(2,6);
CapSense cs_2_7 = CapSense(2,7);
CapSense cs_2_8 = CapSense(2,8);
CapSense cs_2_9 = CapSense(2,9);
CapSense cs_2_10 = CapSense(2,10);
CapSense cs_2_11 = CapSense(2,11);
CapSense cs_2_12 = CapSense(2,12);
CapSense cs_2_13 = CapSense(2,13);
char p3tr='0';
char p4tr='0';
char p5tr='0';
char p6tr='0';
char p7tr='0';
char p8tr='0';
char p9tr='0';
char p10tr='0';
char p11tr='0';
char p12tr='0';
char p13tr='0';
void setup()
{
cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_5.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_6.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_7.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_8.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_9.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_10.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_11.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_12.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_13.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_2_3.set_CS_Timeout_Millis(1);
cs_2_4.set_CS_Timeout_Millis(1);
cs_2_5.set_CS_Timeout_Millis(1);
cs_2_6.set_CS_Timeout_Millis(1);
cs_2_7.set_CS_Timeout_Millis(1);
cs_2_8.set_CS_Timeout_Millis(1);
cs_2_9.set_CS_Timeout_Millis(1);
cs_2_10.set_CS_Timeout_Millis(1);
cs_2_11.set_CS_Timeout_Millis(1);
cs_2_12.set_CS_Timeout_Millis(1);
cs_2_13.set_CS_Timeout_Millis(1);
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
void loop()
{
long total3 = cs_2_3.capSense(1000);
long total4 = cs_2_4.capSense(1000);
long total5 = cs_2_5.capSense(1000);
long total6 = cs_2_6.capSense(1000);
long total7 = cs_2_7.capSense(1000);
long total8 = cs_2_8.capSense(1000);
long total9 = cs_2_9.capSense(1000);
long total10 = cs_2_10.capSense(1000);
long total11 = cs_2_11.capSense(1000);
long total12 = cs_2_12.capSense(1000);
long total13 = cs_2_13.capSense(1000);
if (total3>100){
p3tr = '1';
}
else{
p3tr = '0';
}
if(total4>100){
p4tr = '1';
}
else{
p4tr = '0';
}
if(total5>100){
p5tr = '1';
}
else{
p5tr = '0';
}
if(total6>100){
p6tr = '1';
}
else{
p6tr = '0';
}
if(total7>100){
p7tr = '1';
}
else{
p7tr = '0';
}
if(total8>100){
p8tr = '1';
}
else{
p8tr ='0';
}
if(total9>100){
p9tr = '1';
}
else{
p9tr = '0';
}
if(total10>100){
p10tr = '1';
}
else{
p10tr = '0';
}
if(total11>100){
p11tr = '1';
}
else{
p11tr = '0';
}
if(total12>100){
p12tr = '1';
}
else{
p12tr = '0';
}
if(total13>100){
p13tr = '1';
}
else{
p13tr = '0';
}
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
Wire.write(????????); //i dont no i try all most everything
}
The question is how to send ALL variables “p3tr,p4tr…” value to the master??