1

Slave code

int arr[10] = {0,1,2,11,4,5,6,7,8,9};
char id = '1';
char re[2];
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
int ia;
void loop() {
Serial.readBytes(re,2);// slave gets the query
ia = re[1] - '0';
if (re[0] == id){
Serial.println(arr[ia]);// slave print in serial monitor
} 
delay(500);
}

Master Code

char mystr[2] = "13"; 
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}

void loop() {
Serial.write(mystr,2); //sends query to slave
delay(3000);
}
2
  • check the result value of readBytes. before readBytes check available(). readBytes waits a second for a byte, the it returns count of bytes read. so it could be 0 – Juraj Oct 25 '20 at 9:01
  • Is Slave's Serial connected to Master or to SerialMonitor? Serial is not a bus. – DataFiddler Oct 25 '20 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.