aceta-minophen/Programming-Toolbox

Wire.h library only showing values from 0 to 255

aceta-minophen opened this issue · 3 comments

It's working well while transmitting 0 and 1, but for values grater than 255, it's going bonkers.

That's because the write method only send a byte and a single byte goes from 0 to 255.Since you are above that (260) it will roll back. The solution could passes to convert that value(260) to a string and send it as a string or create an array of values and send the entire array with Wire.write(array,sizeof(array));

The above solution didn't work, so I need to try the highbyte, lowbyte method.

Found a solution in this webpage.

Arduino code - Slave
if( Wire.available()>0){
byte low = Wire.read(); // read a byte of the buffer
byte high = Wire.read(); // read the next byte of the buffer.
int result = word (high, low); // melt them into an integer
}

NodeMCU code - Master
void requestEvent() {
int value = analogRead(_analogpinnumber); //reads analog analog pin

// create a buffer. This buffer can be used to send more than only 2 bytes.
byte buffer[10];
buffer[0] = lowByte (value);
buffer[1] = highByte ( value);
Wire.write( buffer, 2);
}