Hello Arduino Forum I am currently working on a project that deals with fire alarm systems, at the moment I am still testing and wanted to use the I2C technology. But I don't have so much experience with the I2C system, at the moment I have one or more problems but I don't know where they come from so I ask you for help.
So one problem is when I work with the following code I get the data on the master but there the "if" commands are ignored?
This is the master code:
int attentionLed = 2;
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus
Serial.begin(9600); // start serial for output
pinMode(2,OUTPUT);
}
void loop() {
Wire.requestFrom(8, 4); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
if (c == "8/3 ");{
Serial.print(c); // print the character
digitalWrite(2, HIGH);
}
if (c == "no ");{
Serial.print(c); // print the character
digitalWrite(9, LOW);
}
}
delay(500);
}
And this is the slave:
int test_sensor = 0;
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
Serial.begin(9600);
}
void loop() {
delay(100);
test_sensor = analogRead(A0);
Serial.println(test_sensor);
}
void requestEvent() {
if (test_sensor > 700) {
Wire.write("8/3 "); // respond with message of 6 bytes (detector adress)
// as expected by master
}
else if (test_sensor < 700) {
Wire.write("no ");
}
}
Thanks for your answer but i don't quite understand how you mean this, but my goal is to connect 5 detectors to my master so that when the detector 8/3 triggers, the name (8/3) of the detector is shown on an LCD display. That's why I first want to see how it works if I only have one slave that sends its name to the master when the right condition is met and the master then triggers an LED.
A test done with an if statement returns true or false and the whole point of using it is to execute some code when the if returns true or possibly when it returns false
This code
char c = Wire.read(); // receive a byte as character
if (c == "8/3 ");
{
Serial.print(c); // print the character
digitalWrite(2, HIGH);
}
says "if c equals the string "8/3 " then execute the next code block. In this case the next code block consists of a semicolon and the code block in the curly brackets will be executed regardless of whether the test returns true or false
Incidentally, because c is declared as a char it can only hold a single character (your comment even says so) so it will never equal "8/3 " anyway
for techniques to read multi character strings. Once you have the string you can compare it with fixed strings in you sketch using functions such as strcmp() and others
Can you tell more about your I2C bus ?
Are there long wires or cables and what about pullup resistors ?
Please use binary data for the I2C bus. We don't use readable ASCII strings for the I2C bus.
Would requesting one byte be enough for your project ? Then you have a number from 0...255.
It is actually a school project to represent a fire alarm system, I only want to have around 5 slaves, which is enough. But I don't really have a plan how to program it (I would like to use the I2C bus because a similar kind of system is used in a real fire alarm system).
There are no long cables, it should just stand on the table at a presentation and do its job.
I am trying to get the slave to send its name as soon as it is triggered and the master then gives the slave name on an LCD display and triggers a buzzer.