Hey all,
I'm new to I2C and have been testing it out to try and get a firm grip on it before I implement it into a larger project.
I was testing out the Wire.onRequest() function and found it wasn't giving expected outputs after I removed Serial.print() functions I was no longer using, despite me not even running Serial.begin() on the slave.
Master code-
#include <Wire.h>
#define SENSORSLAVEADDRESS 0x20
byte collisionByte = 255;
void setup() {
Serial.begin(9600);
delay(100);
Wire.begin();
Serial.print("READY");
Serial.println(" ");
delay(2500);
}
void loop() {
Wire.requestFrom(SENSORSLAVEADDRESS, 1);
if (Wire.available()) {
collisionByte = Wire.read();
}
Serial.print(collisionByte);
Serial.println(" ");
delay(2500);
}
This is the slave code that gives the expected output-
#include <Wire.h>
#define SENSORSLAVEADDRESS 0x20
byte frontUltrasound = 1;
byte backLeftUltrasound = 2;
byte backRightUltrasound = 4;
byte leftInfrared = 8;
byte rightInfrared = 16;
// front ultrasound 1st bit
// back left ultrasound 2nd bit
// back right ultrasond 3rd bit
// left infrared 4th bit
// right infrared 5th bit
void setup() {
// put your setup code here, to run once:
Wire.begin(SENSORSLAVEADDRESS);
Wire.onRequest(objectDetect);
}
void loop() {
// put your main code here, to run repeatedly:
frontUltrasound = 0;
backLeftUltrasound = 0;
backRightUltrasound = 0;
leftInfrared = 0;
rightInfrared = 0;
Serial.println(" "); //IF U REMOVE THESE SERIAL PRINT IT DOESNT WORK??
delay(1000);
frontUltrasound = 1;
Serial.println(" ");
delay(1000);
backLeftUltrasound = 2;
Serial.println(" ");
delay(1000);
backRightUltrasound = 4;
Serial.println(" ");
delay(1000);
leftInfrared = 8;
Serial.println(" ");
delay(1000);
rightInfrared = 16;
Serial.println(" ");
delay(1000);
}
void objectDetect(){
Wire.write(frontUltrasound | backLeftUltrasound | backRightUltrasound | leftInfrared | rightInfrared);
}
When this code is running the master board outputs the byte increasing and then returning to 0 as expected.
This is the slave code I'm confused about. It is identical to the one above with the exception of the Serial.println() but doesn't give the expected output-
#include <Wire.h>
#define SENSORSLAVEADDRESS 0x20
byte frontUltrasound = 1;
byte backLeftUltrasound = 2;
byte backRightUltrasound = 4;
byte leftInfrared = 8;
byte rightInfrared = 16;
// front ultrasound 1st bit
// back left ultrasound 2nd bit
// back right ultrasond 3rd bit
// left infrared 4th bit
// right infrared 5th bit
void setup() {
// put your setup code here, to run once:
Wire.begin(SENSORSLAVEADDRESS);
Wire.onRequest(objectDetect);
}
void loop() {
// put your main code here, to run repeatedly:
frontUltrasound = 0;
backLeftUltrasound = 0;
backRightUltrasound = 0;
leftInfrared = 0;
rightInfrared = 0;
delay(1000);
frontUltrasound = 1;
delay(1000);
backLeftUltrasound = 2;
delay(1000);
backRightUltrasound = 4;
delay(1000);
leftInfrared = 8;
delay(1000);
rightInfrared = 16;
delay(1000);
}
void objectDetect(){
Wire.write(frontUltrasound | backLeftUltrasound | backRightUltrasound | leftInfrared | rightInfrared);
}
When this code is running the master board outputs 31 every time it makes a request. Interestingly, in my testing I found that whatever the final state of the loop is will be the output (e.g. if I set the variables to 0 at the end of the loop instead of at the beginning the master board outputs 0 every time).
Both the master and the slave are arduino UNO R3.
I'm not sure why one of these works while the other doesn't but would love to learn what's going on.
If there's anything else of use I can provide please let me know.