Hi, I'm building a big game project and there is a function that checks if the user hit the main target
and if he hit the target the function sends 1, if not so 0.
This function also sends the number of hits that the user hit badly. (hard to explain but the function sends 0 or higher)
The reason that I want to send from slave to master is that the master is also sending data to the slave and it's working fine and I don't want to ruin it.
Problem: I want to send from slave to master 2 ints that inside of int array and it's not working.
Note: I didn't put the full codes because the codes are pretty long, I have the library Wire the things that need to be in void setup...
This is the code of the function that gets the variables: (Slave code - Arduino Mega)
void add_or_remove_points(int checkHit, int otherTargetHit)
{
int i = 0;
arr[0] = checkHit;
arr[1] = otherTargetHit;
for (i = 0; i < SEND_DATA_ARR_SIZE; i++) //SEND_DATA_ARR_SIZE = 2
{
Wire.write(arr[i]);
}
}
Note 1: This code is working but the problem is that the master device is getting 0 and 255 and not the ints that I need to send.
Note 2: In void setup() I wrote "Wire.onRequest(add_or_remove_points);"
This is part of the function of the Master device: (Arduino Uno)
void in_game()
{
int i = 0;
Wire.requestFrom(SLAVE_ADDR, ANSWER_SIZE * sizeof(int)); //ANSWER_SIZE = 2
while (Wire.available())
{
answer_Arr[i] = Wire.read();
i++;
}
Serial.print("checkHit: ");
Serial.println(answer_Arr[0]);
Serial.print("otherTargetHit: ");
Serial.println(answer_Arr[1]);
}
Project_Slave.ino (7.11 KB)
Project_LCD_Master.ino (9.85 KB)