Problem sending two integers from a master to a slave arduino!

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);
}

Slave code:

#include <Wire.h> 

int value1 = 0; 
int value2 = 0;

void setup() 
{ 
  Wire.begin(9); 
  Wire.onReceive(receiveEvent); 
  
  Serial.begin(9600); 
}

void receiveEvent(int bytes)
{
  int value1 = (Wire.read() >> 8 | Wire.read());
  int value2 = (Wire.read() >> 8 | Wire.read());
 
  Serial.print(value1);
  Serial.print(value2);
}

void loop()
{
  Serial.println(); 
  Serial.print("value1: "); 
  Serial.println(value1);
  Serial.print("value2: "); 
  Serial.println(value2); 
}

Hi, @peatepeate123
Welcome to the forum.
Thankyou for using code tags. :+1:

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

How have you got the two controllers connected?
Have you got both gnds connected together as well?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

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!

I struggled with this until I came across this .. its a bit basic but it should do the trick.

Hi,
Why?

void setup() 
{ 
  Wire.begin(9);   <<<<<<<<<<<<<<<<<<<<<<<
  Wire.onReceive(receiveEvent); 
  
  Serial.begin(9600); 
}

Shouldn't that be

Wire.begin();

Tom.. :smiley: :+1: :coffee: :australia:

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.