55. I2C Programming

Question.2

What is the last line printed on the slave’s serial monitor?

 

Master Arduino Code:

#include <Wire.h>

int count = 0;

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

void loop() {
  byte receivedData = 0;
  Wire.requestFrom(8, 1); 
  while (Wire.available()) {
    receivedData = Wire.read();
    count++;
  }

  Serial.println((String) "data :" + receivedData);
  Serial.println((String) "cnt :" + count);
  while(1);
 
}

Slave Arduino Code:

#include <Wire.h>


void setup() {
  Wire.begin(8);  
  Wire.onRequest(sendData); 
}


void loop() {
}


void sendData() {
  for(int i = 1; i <= 10; i++) {
    Wire.write(i); 
  }
}

Select Answer