I2C Communication Problem

Hi all!

I just want to ask how do I communicate between Arduino Mega and Uno. I am trying to send analog data to change the flow of the loop of my Slave board. However, I am unable to do so.

Is it possible?

Here is my Master Code:

#include <Wire.h>

int DA = A0;
int sensorvalue = 0;

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

void loop() {
  sensorvalue = analogRead(DA);
  Serial.print("Analog: ");
  Serial.println(sensorvalue);
  Wire.beginTransmission(5);
  Wire.write(sensorvalue);
  Wire.endTransmission();
}

And here is my Slave Code:

#include <Wire.h>

int threshold = 532;
byte SlaveReceived = 0;
int North[] = {2, 3, 4};
int West[] = {5, 6, 7};
int South[] = {8, 9, 10};
int East[] = {11, 12, 13};
int redDelay = 5000;
int yellowDelay = 2000;

void setup() {
    for (int i = 0; i < 3; i++) {
    pinMode(North[i], OUTPUT);
    pinMode(West[i], OUTPUT);
    pinMode(South[i], OUTPUT);
    pinMode(East[i], OUTPUT);
    Serial.begin(9600);
    Wire.begin(5);
    Wire.onReceive(receiveEvent);
}
}
void loop() {
    // Making Green  LED at signal 1 and red LED's at other signal HIGH
  digitalWrite(North[2], HIGH);
  digitalWrite(North[0], LOW);
  digitalWrite(West[0], HIGH);
  digitalWrite(South[0], HIGH);
  digitalWrite(East[0], HIGH);
  delay(redDelay);
  // Making Green LED at signal 1 LOW and making yellow LED at signal 1 HIGH for 2 seconds
  digitalWrite(North[1], HIGH);
  digitalWrite(North[2], LOW);
  delay(yellowDelay);
  digitalWrite(North[1], LOW);


  //Serial.println(LastGreen);
  // Making Green  LED at signal 2 and red LED's at other signal HIGH
  digitalWrite(North[0], HIGH);
  digitalWrite(West[2], HIGH);
  digitalWrite(West[0], LOW);
  digitalWrite(South[0], HIGH);
  digitalWrite(East[0], HIGH);
  delay(redDelay);
  // Making Green LED at signal 2 LOW and making yellow LED at signal 2 HIGH for 2 seconds
  digitalWrite(West[1], HIGH);
  digitalWrite(West[2], LOW);
  delay(yellowDelay);
  digitalWrite(West[1], LOW);

 
 // Serial.println(LastGreen);
  // Making Green  LED at signal 3 and red LED's at other signal HIGH
  digitalWrite(North[0], HIGH);
  digitalWrite(West[0], HIGH);
  digitalWrite(South[2], HIGH);
  digitalWrite(South[0], LOW);
  digitalWrite(East[0], HIGH);
  delay(redDelay);
  // Making Green LED at signal 3 LOW and making yellow LED at signal 3 HIGH for 2 seconds
  digitalWrite(South[1], HIGH);
  digitalWrite(South[2], LOW);
  delay(yellowDelay);
  digitalWrite(South[1], LOW);

  //Serial.println(LastGreen);
  // Making Green  LED at signal 4 and red LED's at other signal HIGH
  digitalWrite(North[0], HIGH);
  digitalWrite(West[0], HIGH);
  digitalWrite(South[0], HIGH);
  digitalWrite(East[2], HIGH);
  digitalWrite(East[0], LOW);
  delay(redDelay);
  // Making Green LED at signal 4 LOW and making yellow LED at signal 4 HIGH for 2 seconds
  digitalWrite(East[1], HIGH);
  digitalWrite(East[2], LOW);
  delay(yellowDelay);
  digitalWrite(East[1], LOW);
}

void receiveEvent(int howMany){
 SlaveReceived = Wire.read();

  if (SlaveReceived >= threshold) {
    digitalWrite(North[1], HIGH);
    digitalWrite(West[1], HIGH);
    digitalWrite(South[1], HIGH);
    digitalWrite(East[1], HIGH);
    digitalWrite(North[0], LOW);
    digitalWrite(West[0], LOW);
    digitalWrite(South[0], LOW);
    digitalWrite(East[0], LOW);
    delay(yellowDelay);

    digitalWrite(North[0], HIGH);
    digitalWrite(West[0], HIGH);
    digitalWrite(South[0], HIGH);
    digitalWrite(East[0], HIGH);
    digitalWrite(North[1], LOW);
    digitalWrite(West[1], LOW);
    digitalWrite(South[1], LOW);
    digitalWrite(East[1], LOW);
    delay(redDelay);
  }
}

Thank you so much! <3

why do you have this in your for loop in setup?

   Serial.begin(9600);
    Wire.begin(5);
    Wire.onReceive(receiveEvent);

you need to perform that only once.

also the write method you use will only send 1 byte. You have an int (= 2 bytes on your arduino) so won't send the whole value, only the LSB

I've not read the rest. I suggest you start with something simple where the data is correctly exchanged and then build on top of this (don't forget to connect the GND of both Arduinos together)

Oh okay.

What should be done instead for the write() method?

The Wire.write(sensorvalue); method you use will only send 1 byte. You have an int (= 2 bytes on your arduino) --> you need to use the version with a pointer and a length and on the receiving end you need to rebuild the integer as you'll be receiving bytes

another option, if this works for you, is to bring back the analog value (0 to 1023) to only one byte (0 to 255). If that resolution is enough for you (given you have just a simple threshold it seems) then you can send just that one byte. (the map() function will simplify mapping one range to the other)

Please don't use delay() in the receiveEvent(). It is interrupt handler, just as the requestEvent() function that I wrote about yesterday: 'requestEvent' was not declared in this scope - #6 by Koepel - Programming Questions - Arduino Forum.

Hi,
Did you google

arduino to arduino i2c

Tom... :slight_smile: