i am trying to figure out how can establish i2c connection between one master and one slave arduino and send an char Array
my problem so far is that i cant read the sending array to my slave device
here is the code for both of them
Master:
#include <Wire.h>
byte call_02 = 12; // pin for reading button
byte call_01 = 11; // pin for reading button
char buttonArray[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
// char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'}; // it is the sama as above
// char Str5[8] = "arduino"; // it is the sama as above
//const byte button0_Floor = 11;
//const byte button1_Floor = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
pinMode(call_01, INPUT_PULLUP);
pinMode(call_02, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
int button0_FloorState = digitalRead(call_01);
int button1_FloorState = digitalRead(call_02);
if (button0_FloorState == LOW) {
buttonArray[2] = 'W' ;
}
if (button1_FloorState == LOW) {
buttonArray[4] = 'Q' ;
}
Serial.println(buttonArray);
Serial.println(buttonArray[2]);
delay(500);
Wire.beginTransmission(4); // transmite to slave with address 4
Wire.write(buttonArray, 8); // sends array with the name buttonArray
}
Slave :
#include <Wire.h>
const byte slaveAddress1 = 4;
boolean flagButton = false;
char buf[8];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin(slaveAddress1); // join the I2C bus as slave
Wire.onRequest(requestEvent); // Register a function to be called when a master requests data from this slave device
Wire.onReceive(receiveEvent);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(buf[2]);
Serial.println(buf);
delay(500);
Serial.println("test");
}
void requestEvent() {
if (flagButton == false) {
Wire.write(' ');
Serial.println("no call");
}
}
void receiveEvent(int howMany)
{
while (Wire.available())
{
buf[8] = Wire.read();
}
}
the goal of these code is just to understand how "array" (send/receive) works in order to use it as function in a bigger project i have in my mind were i am using "Wire.write" function for each button state separately
Your read method is wrong, you are reading all bytes to the last byte of the array:
byte index = 0;
while (Wire.available() && (index < 8)) //Make sure that only a max of 8 bytes are read
{
buf[index++] = Wire.read(); //Read one byte to index and increment (++) index
}
PaulS:
Check again. OP is writing all bytes PAST the end of the array.
PaulS:
buf[8] = Wire.read();
On the slave, there is no element at position 8. You are crapping on memory you don't own.
You need a variable as an index into the array, incremented each time you are to the array.
Based on the above mentioned observations, I have moderated/tested your codes (UNO+NANO) which are given below. Please, check/compare these codes with your original codes and note down the mistakes you have done.
Master Codes:
#include <Wire.h>
byte call_02 = 12; // pin for reading button
byte call_01 = 11; // pin for reading button
char buttonArray[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
// char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'}; // it is the sama as above
// char Str5[8] = "arduino"; // it is the sama as above
//const byte button0_Floor = 11;
//const byte button1_Floor = 12;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
pinMode(call_01, INPUT_PULLUP);
pinMode(call_02, INPUT_PULLUP);
}
void loop()
{
// put your main code here, to run repeatedly:
/* temporary suspension
int button0_FloorState = digitalRead(call_01);
int button1_FloorState = digitalRead(call_02);
if (button0_FloorState == LOW)
{
buttonArray[2] = 'W' ;
}
if (button1_FloorState == LOW)
{
buttonArray[4] = 'Q' ;
}
*/
Serial.println(buttonArray); //arduino
Serial.println(buttonArray[2]); //d
Serial.println("=====================");
//delay(500);
Wire.beginTransmission(4); // transmite to slave with address 4
Wire.write(buttonArray, sizeof(buttonArray));//8); // sends array with the name buttonArray
Wire.endTransmission();
delay(2000);
}
Slave Codes
bool flag1 = LOW;
#include <Wire.h>
const byte slaveAddress1 = 4;
boolean flagButton = false;
char buf[8] = ""; //;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin(slaveAddress1); // join the I2C bus as slave
Wire.onRequest(requestEvent); // Register a function to be called when a master requests data from this slave device
Wire.onReceive(receiveEvent);
}
void loop()
{
if (flag1 == HIGH)
{
// put your main code here, to run repeatedly:
Serial.println(buf); //arduino
Serial.println(buf[2]); //d
Serial.println("=================");
flag1 = LOW;
}
// delay(500);
// Serial.println("test");
}
void requestEvent()
{
if (flagButton == false)
{
Wire.write(' ');
Serial.println("no call");
}
}
void receiveEvent(int howMany)
{
// while (Wire.available())
//{
for (int i = 0; i < howMany; i++)
{
buf[i] = Wire.read(); //arduino buf[8] = Wire.read(); //arduino
}
// }
flag1 = HIGH;
}
BTW: You are referred to this link for a tutorial on Master/Slave operations of UNOs.