Hello! I am trying to send to values read from potentiometers on a master arduino, to a slave arduino. However I havn't been able to figure out how to format the values for the write function and them how to read them on the slave. Any help would be appreciated. Below is one of my attempts so far.
Master Code:
#include <Wire.h>
#define pot1 A0
#define pot2 A1
int value1 = 1;
int value2 = 1;
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
int x = analogRead(pot1);
value1 = map(x,0,1023,-255,255);
int y = analogRead(pot2);
value2 = map(y,0,1023,-255,255);
Wire.beginTransmission(9);
Wire.write(value1);
Wire.write((value1 >> 8));
Wire.write(value2);
Wire.write((value2 >> 8));
Wire.endTransmission();
Serial.println();
Serial.print("value1: ");
Serial.println(value1);
Serial.print("value2: ");
Serial.println(value2);
delay(100);
}
Hi @TomGeorge
Sorry about the missing info.
I am using two arduino unos with the 3 connections (A5, A4 and ground) and the potentiometers on A0 and A1 of the master arduino.
Thanks!
Where else would you specify the I2C address of the slave?
Part of the OPs problem is declaring local variables for value1 and value2 in receiveEvent, so that the values are not available in loop. The global declarations for value1 and value2 should be declared volatile, along with caution in using those variables since receiveEvent is an interrupt handler. The other major problem is that the OP sends the lower byte followed by the upper byte, then on receive throws away the lower byte by shifting it 8 bits right, and then never shifts the upper byte the required 8 bits left.