Hi, so basically I'm doing a small project that uses I2C (master read, slave write) to read a slave that has a flame sensor and chick whether there is a flame or not. I just need someone to chick on my code and connection to see if I need to fix anything. I uploaded the codes the slave worked fine but the master device couldn't read slave when there's a flame.
slave code:
int buzzer = 8;
int LED = 7;
int flame_sensor = 4;
int flame_detected;
#include <Wire.h>
int f;
void setup()
{
Wire.begin(8);
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(flame_sensor, INPUT);
Wire.onRequest(requestEvent);
}
void loop()
{
flame_detected = digitalRead(flame_sensor);
if (flame_detected == 1)
{
int f=1;
Serial.println("Flame detected...! take action immediately.");
digitalWrite(buzzer, HIGH);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
}
else
{
int f=0;
Serial.println("No flame detected. stay cool");
digitalWrite(buzzer, LOW);
digitalWrite(LED, LOW);
}
delay(1000);
}
void requestEvent() {
Wire.write(f);
Serial.println("Sent");
}
master code:
#include <Wire.h>
int c;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start Serial for output
}
void loop()
{
Wire.requestFrom(8, 1); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
{
int c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
Serial.println();
}
if(c==1){
Serial.println("Flame detected...! take action immediately.");
}
else
{
Serial.println("No flame detected. stay cool");
}
delay(1000);
}
The connection between the master and slave is:
I connected SCL, SDA, and GND for the two devices with each other. The two devices are powered with usb cable. #note I didn't download any library for I2c
You declare a global variable 'f', but then you declare another variable inside loop() also called 'f'
void loop() {
//...
int f=1;
//...
Those variables are different so your local version will be 1 or 0, but the global version will always remain 0.
The solution is to not but the 'int' portion in front of the variable when you are just assigning a value to it. Only declare it [using 'int'] once.
Also, you variable 'f' is an int which is two bytes. When you do a Wire.write(f), it will get truncated down to a single byte. In your case, this does not hurt anything since the value is only 0/1.
You declare a global variable 'f', but then you declare another variable inside loop() also called 'f'
void loop() {
//...
int f=1;
//...
Those variables are different so your local version will be 1 or 0, but the global version will always remain 0.
The solution is to not but the 'int' portion in front of the variable when you are just assigning a value to it. Only declare it [using 'int'] once.
Also, you variable 'f' is an int which is two bytes. When you do a Wire.write(f), it will get truncated down to a single byte. In your case, this does not hurt anything since the value is only 0/1.
Thanks a lot, man! I'm you can tell that I'm quite a noob in c programming lol. Also, I wanna ask about
"Also, you variable 'f' is an int which is two bytes. When you do a Wire.write(f), it will get truncated down to a single byte. In your case, this does not hurt anything since the value is only 0/1." so if I have two digits number written in int let say "int f=35" it will be 4 bytes or not? I have other projects in my mind and I wanna check to wither that's right or not.
shalltear:
Thanks a lot, man! I'm you can tell that I'm quite a noob in c programming lol. Also, I wanna ask about
"Also, you variable 'f' is an int which is two bytes. When you do a Wire.write(f), it will get truncated down to a single byte. In your case, this does not hurt anything since the value is only 0/1." so if I have two digits number written in int let say "int f=35" it will be 4 bytes or not? I have other projects in my mind and I wanna check to wither that's right or not.
No. an 'int' take 16 bits on an Uno. Wire.write() takes a byte which is 8 bits. Therefore, you can only send any value between 0-255. Since you are only sending 0 or 1, no problem. If you tried to send the value 256, you would get 0. 257 -> 1, etc. since the upper 8 bits would be thrown away.
blh64:
No. an 'int' take 16 bits on an Uno. Wire.write() takes a byte which is 8 bits. Therefore, you can only send any value between 0-255. Since you are only sending 0 or 1, no problem. If you tried to send the value 256, you would get 0. 257 -> 1, etc. since the upper 8 bits would be thrown away.
Oh, nice!.... thanks for your help sir I really appreciate it.