Use of Wire commands on I2C bus

Hello:

I am hooking up 2 Arduino Nanons. Den one as "Master Reader" and the other as "Slave Sender". The wiring is correctly done with A4,A5 and GND.
On the "Slave sender" I have put the text Wire.write("5");
On the "Master Reader" in Serial print, it says "5" and it keeps repeating the number/text.
All OK.
But I want the "Master Reader" to execute a command, like turn on an LED, when the text is equivalent "5" and doing nothing when its not "5".
I have tried to use "if (c == "5"){ and the commands for LED here}, and nothing happens?
What I am doing wrong?
The idea is to send different commands/text from "Slave sender" to "Master reader" and then the "Master reader" will execute commands depending of the text.
Here is the code for "Master reader":

#include <Wire.h>

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

void loop() {
Wire.requestFrom(2, 1);
while(Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
delay(500);
}

Please post your sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

c will never equal "5" because c is declared as a char, ie a single character, not a string of characters

Try if (c == '5') instead

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.