54. I2C Concepts 2

Question.2

What will be printed on the slave’s Serial Monitor?

Note : Both Arduinos are turned ON at the same instant.

Master Arduino Code:

#include <Wire.h>

void setup() {
  Wire.begin();
  delay(1000); 
}

void loop() {
  for (int i = 1; i <= 1000; i++) {
    Wire.beginTransmission(8);  
    Wire.write(i);  
    Wire.endTransmission();
    delay(5);  
  }

  while(1);
}

 

Slave Arduino Code:

#include <Wire.h>

int count = 0;
int numberValue = 0;

unsigned long lastTime = 0;

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

void loop() {
  delay(20000);
  Serial.println((String) "num: " + numberValue);
  Serial.println((String) "count: " + count);
  while (1);
}

void receiveData(int byteCount) {
  while (Wire.available()) {
    numberValue = Wire.read();
    count++;
  }
}


 

Select Answer