Hi all,
I'm attempting to set up two Arduino Megas on I2C. One is a joystick (master), whose primary task is monitoring switches and pots and sending results upon changes, and the other is the (slave) receiver whose primary task is operating the equipment. The issue at the moment seems to be that data is accurately received inside the "receiveEvent" routine, but global variables outside the Event scope are not affected. I have used "volatile" everywhere the compiler lets me, to no avail.
I have plagiarized the Wire Example (Wire Master Writer Slave Reader) for the code. I understand that "using Serial inside an ISP is not good," but I'm not sure why, so I used it to verify all bytes were successfully transmitted / received, then commented it back out and continued testing before resorting to this post. When used, the Serial.print commands verified that data sent from the joystick is being received properly inside the "receiveEvent" routine.
I'd appreciate any insight moderators / gurus share to offer.
char temp1, temp2;
volatile uint8_t mBuf[6];
typedef struct wLine {
volatile uint8_t cmd; // byte will be 'a' .. 'd'
volatile uint8_t swVal;
volatile int vVal;
volatile int hVal;
};
volatile wLine comm;
....
void setup() {
Serial.begin(9600); // start serial for output
Wire.begin(8); // join i2c bus (address optional for master)
Wire.onReceive(receiveEvent); // register event
...
comm.cmd = 'L';
comm.swVal = mBuf[1] = 0x3F;
comm.vVal = -1;
comm.hVal = -1;
mBuf[0] = mBuf[1] = mBuf[2] = mBuf[3] = mBuf[4] = mBuf[5] = -1;
...
}
void loop() {
if (comm.swVal == 0x3F) { // Benign state where no switches are pressed
}
else { // <-----------------This section never gets executed,
// though I've verified that data is correctly received
// (and changes with switch press) inside the handler
Serial.println("--------------"); // print the character
Serial.print("cmd: "); // print the character
// Serial.print(mBuf[2]); // print the character
Serial.print(", sw: "); // print the character
Serial.print(comm.swVal, HEX); // print the character
Serial.println(", hVal: "); // print the character
Serial.println(comm.hVal, HEX); // print the character
Serial.print(", vVal: "); // print the character
Serial.println(comm.vVal, HEX); // print the character
Serial.println("--------------"); // print the character
...
}
void receiveEvent(int howMany) {
uint8_t buf[6], index = 0;
buf[0] = 0; buf[1] = 0; buf[2] = 0; buf[3] = 0; buf[4] = 0; buf[5] = 0;
// Serial.println("-------------------"); // print the character
while (0 < Wire.available()) // loop through all but the last
{
buf[index] = Wire.read(); // receive byte
mBuf[index] = buf[index];
// Serial.print("index: "); // print the character
// Serial.print(index, HEX); // print the character
// Serial.print(", byte: "); // print the character
// Serial.println(buf[index], HEX); // print the character
index++;
}
// Serial.println("-------------------"); // print the character
mBuf[0] = buf[0]; mBuf[1] = buf[1]; mBuf[2] = buf[2];
mBuf[3] = buf[3]; mBuf[4] = buf[4]; mBuf[5] = buf[5];
comm.cmd = buf[0]; // single byte ASCII char for later expansion
// Serial.print("Event: cmd: ");
// Serial.print(comm.cmd, HEX);
// Serial.print("buf[0]: ");
// Serial.println(buf[0], HEX);
comm.swVal = buf[1]; // receive byte (switch settings, norm 0x3F HIGH)
// Serial.print("sw: ");
// Serial.print(comm.swVal, HEX);
// Serial.print(" buf[1]: ");
// Serial.println(buf[1], HEX);
comm.hVal = buf[2]; // 2 byte horizontal DAC
comm.hVal <<= 8;
comm.hVal += buf[3];// | buf[3] << 8;
// Serial.print("hVal: ");
// Serial.print(comm.hVal, HEX);
// Serial.print("buf[2]: ");
// Serial.print(buf[2], HEX);
// Serial.print(" ");
// Serial.println(buf[3], HEX);
comm.vVal = buf[4]; // 2 byte vertical DAC
comm.vVal <<= 8;
comm.vVal += buf[5];
// Serial.print(" vVal: ");
// Serial.println(comm.vVal, HEX);
// Serial.println("-------------------");
}