virtualwire.h with wire.h, cannot receive the data at the slave using I2C

Hey guys! This is my second post here,Sorry to disturb you all again.

I'm using 3 Arduino UNOs in this project.

#1 Arduino UNO which is collecting the analog values and digital values from Accelerometer and ultrasonic sensor and transmitting it using 433Mhz radio sensor using VirtualWire.h Library.

#2 Arduino UNO which is receiving the analog and digital values transmitted by the #1 Arduino UNO & Sending these values to another #3 Arduino by using I2C, I'm using wire.h Library here.

#3 Arduino UNO which is receiving the analog and digital values from #2 Arduino UNO and i want to work here on those analog and digital values.

The problem that arrives here is
I'm unable to receive the values at #3 Arduino UNO, whereas i can clearly receive it at Arduino UNO #2

At #1 Arduino UNO i can see the values in Serial Monitor.
At #2 Arduino UNO i can see the values in Serial Monitor.
At #3 Arduino UNO i cannot see the values in Serial Monitor.

I made three sketches,Here they are.

Code of #1 Arduino UNO

#include <VirtualWire.h>

const int transmit_pin = 12;

#define echoPin 7
#define trigPin 8 

const int xPin = A0;
const int yPin = A1;
const int zPin = A2;

int maximumRange = 200;
int minimumRange = 0;

int duration;

int minVal = 265;
int maxVal = 402;


int x;
int y;
int z;
int distance;


void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  vw_set_tx_pin(transmit_pin);

  Serial.begin(9600);
  vw_setup(2000);
}

void loop() {


  int xRead = analogRead(xPin);
  int yRead = analogRead(yPin);
  int zRead = analogRead(zPin);

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  int xAng = map(xRead, minVal, maxVal, -90, 90);
  int yAng = map(yRead, minVal, maxVal, -90, 90);
  int zAng = map(zRead, minVal, maxVal, -90, 90);


  x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
  y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);

  distance = duration / 58.2;

  Serial.print("X: ");
  Serial.print(x);

  Serial.print("  Y: ");
  Serial.print(y);

  Serial.print("  Z:");
  Serial.print(z);

  Serial.print("   Distance");
  Serial.println(distance);

  int sensorArray[] = {x, y, z, distance};

  digitalWrite(13, true);
  vw_send((uint8_t *)sensorArray, sizeof(sensorArray));
  vw_wait_tx();
  delay(50);

}

Code of #2 Aduino UNO

#include <VirtualWire.h>
#include <Wire.h>


int ledPin = 13;



int x, y, z, distance;
int sensorArray[4] = {};


const int receive_pin = 3;

void setup() {

  pinMode(ledPin, OUTPUT);

  vw_set_rx_pin(receive_pin);

  vw_setup(2000);
  Serial.begin(9600);

  vw_rx_start();

}

void loop() {
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;


  if (vw_get_message(buf, &buflen))
  {
    memcpy(sensorArray, buf, buflen);

    x = sensorArray[0];
    y = sensorArray[1];
    z = sensorArray[2];
    distance = sensorArray[3];



    Serial.print("X: ");
    Serial.print(x);

    Serial.print("  Y: ");
    Serial.print(y);

    Serial.print("  z: ");
    Serial.print(z);

    Serial.print("  Distance: ");
    Serial.print(distance);

    Serial.println();
    int data[4];

    data[0] = x;
    data[1] = y;
    data[2] = z;
    data[3] = distance;



    Wire.beginTransmission(4); // transmit to device #4

    Wire.write ( (byte *) data , 8 ) ;

    Wire.endTransmission();




    delay(100);

  }

}

Sketch of #3 Arduino

#include <Wire.h>


volatile int data[4];     // received data for x, y, z,Distance.
volatile boolean 
validData = false; // flag to indicate that something is received.

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output

}

void loop()
{
  // The loop does not contain a delay(), since the 'validData' is checked as many times as possible

  if (validData)
  {

    // It is best to make a copy of the data while interrupts are disabled, but for now they are used directly.
    Serial.print("x=");
    Serial.print(data[0]);
    Serial.print(", y=");
    Serial.print(data[1]);
    Serial.print(", z=");
    Serial.print(data[2]);
    Serial.print(", Distance=");
    Serial.print(data[3]);
    Serial.println();


    // Now that the data is used, reset the flag for new data.
    validData = false;


  }
}



void receiveEvent(int howMany)
{
  // 8 bytes are expected.
  // If not 8 bytes received, then ignore everything.

  if ( howMany == 8)
  {
    // extra safety check. If the flag is clear, fill a new value.
    if ( !validData)
    {
      Wire.readBytes( (byte *) data, 8);
      validData = true;
    }
  }
}

Please help me.
Thanks..

Could you add all the things that I wrote about here ? Add more serial messages; Assign all three pins for VirtualWire (rx,tx,ptt); Do a test with dummy data; Blink a led in the onReceive handler; Check the return value of Wire.endTransmission();

Setup or Arduino #2 needs to have a Wire.begin() statement

void setup() {

  pinMode(ledPin, OUTPUT);

  vw_set_rx_pin(receive_pin);

  vw_setup(2000);
  Serial.begin(9600);

  vw_rx_start();

}

You are awesome cattledog :smiley: I missed that :frowning:

You both guys are amazing, Thank you sir
Thank you very much Peter_n (Specially) and thank you Cattledog.
It's working now. :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: