Thanks guys,
At the end I re-Checked the I2C protocol and understood it can only pass Chars ether by array or singles and managed to work with the code from here: http://ift.tt/1DAeWM4
and created my version to pass a 4 digit code and it work quite fast (less than 1ms).
And for the slave
At the end I re-Checked the I2C protocol and understood it can only pass Chars ether by array or singles and managed to work with the code from here: http://ift.tt/1DAeWM4
and created my version to pass a 4 digit code and it work quite fast (less than 1ms).
// Wire Master Reader int x,y; #include <Wire.h> void setup() { Wire.begin(); // join i2c bus (address optional for master) Serial.begin(115200); // start serial for output } void loop() { Wire.requestFrom(2, 6); // request 6 bytes from slave device #2 int a = Wire.read(); // receive a byte as character int b = Wire.read(); // receive a byte as character int c = Wire.read(); // receive a byte as character int d = Wire.read(); // receive a byte as character int e = Wire.read(); // receive a byte as character if(a== 45) x=-((b-48)*1000+(c-48)*100+(d-48)*10+e-48); else x=(b-48)*1000+(c-48)*100+(d-48)*10+e-48; Serial.println(x); delay(1); } |
And for the slave
// Wire Slave Sender #include <Wire.h> String inString; char A[6]; int x=0,i,val,Sign; void setup() { Serial.begin(115200); // start serial for output Wire.begin(2); // join i2c bus with address #2 Wire.onRequest(requestEvent); // register event } void loop() { Sign=0; val= -1234; // Value to pass if(val<0){ // if value negative val=-val; Sign=1;} inString = String(val,DEC); inString.toCharArray(A,6); if(inString.length()!=5){ // manipulation to bring carateres to the right if string is less then 5 caracteres for(x=0; x<(5-inString.length());x++){ for(i=0;i<5;i++) A[5-i]=A[4-i]; A[0]=48;}} if (Sign==1) A[0] = 45; delay(1); } void requestEvent() { Wire.write(A); // respond with message of 6 bytes } |
Comments
Post a Comment