hello ,
I am trying to make communication between two arduinos using i2c; i2c communication is working fine when i am using static data like this to send from ISR of slave to master
void requestCallback()
{
for(x=0;x<4;x++)
buffer[x]=x;
Wire.write(buffer, 4);
}
but whats happening with buffer when i am dynamically updating it with variables in i2c isr i am not understanding it....
here is my code for slave
#include <Wire.h>
const byte SlaveDeviceId = 1;
char *p;
int length=0;
int8_t ans=0,l;
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout);
char response[100];
int t=0;
uint8_t buffer[4];
int cell_id_2G[5];
void setup()
{
// Start I²C bus as a slave
Wire.begin(SlaveDeviceId);
// Set the callback to call when data is requested.
Wire.onRequest(requestCallback);
Serial.begin(9600);
Serial2.begin(9600);
}
void loop()
{
ans=sendATcommand("AT", "OK", 2000);
ans=sendATcommand("AT+QNWCFG=\"NWSCANMODE\",0", "OK",2000);
Serial.println("ENTER 2G");
sendATcommand("AT+CREG=2", "OK", 2000);
ans=sendATcommand("AT+CREG?", "+CREG:", 2000);
if(p!=NULL)
{
for(l=0;l<4;l++)
{
cell_id_2G[l]=p[12+l];
}
cell_id_2G[l]='\0';
}
else
{
cell_id_2G[0]='\0';
}
Serial.print(cell_id_2G[0]);
Serial.print(cell_id_2G[1]);
Serial.print(cell_id_2G[2]);
Serial.println(cell_id_2G[3]);
}
void requestCallback()
{
Serial.print("In ISR");
for(l=0;l<4;l++)
{
buffer[l]=cell_id_2G[l];
Serial.print(buffer[l]);
Serial.print(cell_id_2G[l]);
}
Wire.write(buffer,23);
}
----------------------------******************-------------------------(this section of code is working perfectly fine)
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
uint8_t x=0, answer=0;
unsigned long previous;
memset(response, '\0', 200); // Initialize the string
delay(100);
Serial2.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial2.available() != 0){
response[x] = Serial2.read();
x++;
//Serial.println(response);
// check if the desired answer is in the response of the module
}
// Waits for the asnwer with time out
}
while(((millis() - previous) < timeout));
if ((p=strstr(response, expected_answer1)) != NULL)
{
answer = 1;
}
return answer;
}
and here is my hyper terminal output
In ISR00000000In ISR00000000In ISR00000000In ISR00000000ENTER 2G
In ISR00000000In ISR00000000In ISR00000000In ISR0000000065485168
In ISR00000000In ISR00000000In ISR00000000In ISR00000000ENTER 2G
In ISR00000000In ISR00000000In ISR00000000In ISR0000000065485168
In ISR00000000In ISR00000000In ISR00000000In ISR00000000ENTER 2G
In ISR00000000In ISR00000000In ISR00000000In ISR0000000065485168
in ISR the value of cell_id_2G is printed as 0000,so at master also it is printing 0000...but in the main loop it is 65,48,51,68
y this variable is losing its value....someone plz help me with this....
thanks
asma