Hi:
I am trying to have an ultrasonic rangefinder transfer it's distance readings from one Arduino to another via I2C. I've successfully established the I2C connection (I just sent over an integer variable that increments), and my ultrasonic rangefinder's values can be detected on serial of the Arduino that is controlling it. So, basically, I have the 2 individual parts working, and I need to integrate them.
Here is what my ultrasound slave's code looks like:
#include <Wire.h>
#define COMMANDPIN 2
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
pinMode(COMMANDPIN, OUTPUT);
digitalWrite(COMMANDPIN, HIGH);
Serial.begin(9600);
}
void loop()
{
delay(100);
}
void requestEvent()
{
int index=0;
while(index<100){
delay(1000);
Wire.send(ultrasoundread());// respond with message of 100 bytes
// as expected by master
}
}
int ultrasoundread()
{
pinMode(COMMANDPIN, HIGH);
pinMode(COMMANDPIN, LOW);
delay(50);
boolean startreading = false;
char in[4];
in[3] = 0;
int index = 0;
Serial.flush();
while(index < 3){
if(Serial.available()){
byte incoming = Serial.read();
if(incoming == 'R') startreading = true;
else if(startreading) in[index++] = incoming;
}
}
return atoi(in);
}
The master is:
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Wire.requestFrom(2, 100); // request 6 bytes from slave device #2
delay(1000);
int c = Wire.receive(); // receive a byte as character
Serial.print((int)c); // print the character
delay(500);
}
As you can see, the code for both programs is really straightforward, but I am not receiving any ultrasound readings on the master's serial connection.
My question is how do I dump 100 bytes of data onto the Wire in one shot?