Thanks for your advices,
So I modify the code as you said :
For master :
// Déclaration des variables et librairies I2C
#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, 2); // request 2 bytes from slave device #2
Serial.println("potentiometer value");
byte loByte;
byte hiByte;
if(Wire.available() >= 2) // slave may send less than requested
{
// char c = Wire.receive(); // test with char
// Serial.print(c);
hiByte = Wire.receive();
loByte = Wire.receive();
}
Serial.print("hiByte = ");
Serial.println(hiByte,BIN);
Serial.print("loByte = ");
Serial.println(loByte,BIN);
int val = (hiByte << 8) + loByte;
Serial.print(val);
Serial.println("");
delay(500);
}
for salve :
// Déclaration des variables et librairies I2C
#include <Wire.h>
#include <stdlib.h>
#define potPin 0 //
int readVal;
byte hi;
byte lo;
///////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
// Communication I2C
Wire.begin(2);
Wire.onRequest(requestEvent); // register event
Serial.begin(9600);
}
void loop()
{
readVal = analogRead(potPin);
hi = highByte(readVal);
lo = lowByte(readVal);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void requestEvent()
{
// respond with message of 2 bytes
Wire.send(hi);
Wire.send(lo);
// Wire.send("hello"); //test with char
}
With that program I made some test as you advice me :
I read that on the serial port (with the analog input connected to 5V):
potentiometer value
hiByte = 11111111
loByte = 11111111
-1
(That is why I said this value sould be 1023 or 1024 because I connect the analog input 5V)
I read that on the serial port (with the analog input connected to 3V):
potentiometer value
hiByte = 10111100
loByte = 11111111
-17153
or:
potentiometer value
hiByte = 10111101
loByte = 11111111
-16897
or:
potentiometer value
hiByte = 11000001
loByte = 11111111
-15873
I read that on the serial port (with the analog input connected to 0V):
potentiometer value
hiByte = 0
loByte = 11111111
255
Then I try to send 1 with the slave and ask 2 with the master as you said, so I just comment this line (Wire.send(lo)) in the slave code :
void requestEvent()
{
// respond with message of 2 bytes
Wire.send(hi);
// Wire.send(lo);
// Wire.send("hello"); //test with char
}
I read that on the serial port (with the analog input connected to 5V):
potentiometer value
hiByte = 11
loByte = 11111111
1023
I read that on the serial port (with the analog input connected to 3V):
potentiometer value
hiByte = 10
loByte = 11111111
767
I read that on the serial port (with the analog input connected to GND):
potentiometer value
hiByte = 0
loByte = 11111111
255
I also try to comment the other line (of course hiByte now mean display loByte on the serial port):
void requestEvent()
{
// respond with message of 2 bytes
// Wire.send(hi);
Wire.send(lo);
// Wire.send("hello"); //test with char
}
I read that on the serial port (with the analog input connected to 5V):
potentiometer value
hiByte = 11111111
loByte = 11111111
-1
or:
potentiometer value
hiByte = 11111110
loByte = 11111111
-257
I read that on the serial port (with the analog input connected to 3V):
potentiometer value
hiByte = 10111011
loByte = 11111111
-17409
or:
potentiometer value
hiByte = 10111101
loByte = 11111111
-16897
or
potentiometer value
hiByte = 10111010
loByte = 11111111
-17665
I read that on the serial port (with the analog input connected to 0V):
potentiometer value
hiByte = 0
loByte = 11111111
255
In the two last test were I send 1 and ask for 2 it's seems to correspond to what I expect but I didn't manage to hab both lo and hi correct in the same time, I tried to ask 3 bytes with the master and send hi and lo but it doesn't like the first test...
I hope these test will help ..