#include <Wire.h>
const int analogInPin = A0;
const int led = 9;
int sensorValue = 0;
int outputValue = 0;
void setup()
{
Wire.begin(2);
Wire.onRequest(requestEvent);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop()
{
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, -100, 100);
// analogWrite(led, outputValue+100);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(led, HIGH); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
void requestEvent()
{
Wire.write(highByte(outputValue));
Serial.println(highByte(outputValue));
Wire.write(lowByte(outputValue));
Serial.println(lowByte(outputValue));
}
I see in terminal:
sensor = 1023 output = 100
0
100
0
100
Or if i turn pot close to zero:
sensor = 0 output = -100
255
156
255
156
I don,t understand, why i see 4 byte, not two in control section....
In the requestEvent you use two Wire.write and two Serial.println.
The Serial.println may not be used in the requestEvent function.
Combine the data in an array and use a single call to Wire.write.