I’m attempting to get a working example of sending commands from one Arduino to another, receiving information back via I2C and printing that to the serial monitor. I’ve tried to set this up so that the master takes Serial input from the keyboard and parse the input into the commands used, only nothing gets printed to the serial monitor when i issue what I think should be a correct set of instructions. Here’s the code I’ve come up with for master and slave. Any guidance?
Master:
//master
#include <Wire.h>
byte index;
short Variable1;
short Variable2;
short CMD;
int handler;
byte inI2Cbyte;
char inI2C[32];
byte I2Cindex;
char inChar;
char inCMD[32];
short I2CSystem;
short I2CStatus1;
short I2CStatus2;
void setup()
{
Serial.begin(9600);
Wire.begin();
while(Serial.available() > 0)
{
inChar = Serial.read();
if(inChar != ';' && index < 32) //":" is the terminator. Send ":" to indicate end of command?
// while the inChar read from Serial.read does not contain terminator :, and the index is less than 15
{
inCMD[index] = inChar; //sets the inChar to the number in index, starting with 0
index++; //increments the index by one
inCMD[index] = '\0'; // this seems to add a null character to the char array. Is that necessary or desirable?
}
else if(inChar== ';')
{
Variable1 =(short)strtok(inCMD, ":");
Variable2 =(short)strtok(inCMD, ":");
CMD = (short)strtok(inCMD, ":");
handler = 1;
index = 0;
}
}
inI2Cbyte = Wire.read();
if(inI2Cbyte != ';') // ; terminates the read and causes a serialprint
{
inI2C[index] = inI2Cbyte; //sets the inChar to the number in index, starting with 0
index++; //increments the index by one
inI2C[index] = '\0'; // this seems to add a null character to the char array. Is that necessary or desirable?
}
else if(inI2Cbyte == ';') // Now, split the string to variables
{
I2CSystem = (short)strtok(inI2C, ":");
I2CStatus1 = (short)strtok(NULL, ":");
I2CStatus2 = (short)strtok(NULL, ":");
I2Cindex = 0;
Serial.print(I2CSystem);
Serial.print(I2CStatus1);
Serial.print(I2CStatus2);
}
else if(inI2Cbyte == '?')
{
Serial.print("Error getting" + CMD);
}
}
void loop()
{
while(handler = 1)
{
Wire.beginTransmission(2);
Wire.write(Variable1);
Wire.write(":");
Wire.write(Variable2);
Wire.write(":");
Wire.write(CMD);
Wire.write(";");
Wire.endTransmission();
Wire.requestFrom(2, 32);
CMD = 0;
handler = 0;
}
}
//slave
#include <Wire.h>
byte index;
short Variable1;
short Variable2;
short CMD;
byte inI2Cbyte;
char inI2C[32];
void setup()
{
Wire.begin(2); // join i2c bus with address #2 (slave)
Wire.onRequest(requestEvent); //declares that when I2C sends an onRequest, the function
//requestEvent() gets called
Wire.onReceive(recieveEvent); // declares that when I2C sends a command, the variable
//CMD gets updated by this function
}
void requestEvent()
{
switch(CMD)
{
case 1:
sendVariable1();
break;
case 2:
sendVariable2();
break;
default:
break;
}
}
void recieveEvent(int howMany)
{
inI2Cbyte = Wire.read();
if(inI2Cbyte != ';') //; will indicate there was an error getting temperature.
{
inI2C[index] = inI2Cbyte; //sets the inChar to the number in index, starting with 0
index++; //increments the index by one
inI2C[index] = '\0'; // this seems to add a null character to the char array. Is that necessary or desirable?
}
else if(inI2Cbyte == ';') // Now, split the string to variables
{
Variable1 = (short)strtok(inI2C, ":");
Variable2 = (short)strtok(NULL, ":");
CMD = (short)strtok(NULL, ":");
index = 0;
}
}
void sendVariable1()
{
Wire.write(Variable1);
}
void sendVariable2()
{
Wire.write(Variable2);
}
void loop()
{
}