I2C Arduino to Arduino, Send Ping( any sensors)Data to master?

Trying to send ping data from slave Arduino. wire write at end; returns error 'microseconds not declared in this scope'. I put my ping (ultrasonic sensor) code inside the slave writer code example from the wire library. Any examples of arduino-to-arduino I2C sending 'real' data collected from the Slave Arduino's sensors would be useful as I2c seems economical and practical. First thing i bought after my Arduino was another Arduino :open_mouth:
I suspect part of my problem is not understanding how data is passed between functions

Heres my combined code that fails:

#include <Wire.h>
const int pingPin = 7;
void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  long duration, cm;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
  delay(50);
   
}
long microsecondsToCentimeters(long microseconds)
{
  return (microseconds / 29 / 2);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write(microseconds); // respond with message of 6 bytes
                       // as expected by master
}
long microsecondsToCentimeters(long microseconds)
{
  return (microseconds / 29 / 2);
}

The variable "microseconds" only has scope (exists) inside that function.

Why are you using it here?

 Wire.write(microseconds); // respond with message of 6 bytes

Besides, that is not responding with 6 bytes. Each Wire.write sends one byte.

thanks for the reply Australia

the comment about 6 bytes was there from the slave writer example code originally, which i guess is the 5 charachters hello plus a space. I dont fully understand C++. I need to figure out how to pass the ping data to wire.write(). Originally i was trying to figure out how to read the Ping sensor attached to Arduino in LabView; still working on that as well, thats how I came across I2C and thought how useful it would be for networking sensors around the lab. Any ideas most appreciated. Thank you arduino forum

About my project: I use the Parallax Ultrasonic(ping) sensor to measure wave heights and wave profiles in my wavepool; heights from 4 cm. - 12 cm. the sensor works fine and i'm able to read the data into console or processing, and if I tab space the data and copy it from the console I can import it into labview. If interested you can see Arduino running my wavepool on youtube; Arduino + wavepool + nxt, or username runman69. I use Arduino's pwm to control a motor that opens and closes the valve the holds and releases the wave; higher value of pwm makes the valve open and close quicker and hence loading less water and making smaller wave, shorter pwm means longers load time and bigger wave.

runman69:
thanks for the reply Australia

I am Nick Gammon:

I live in Australia:

You want to do something more like this:

#include <Wire.h>
const int pingPin = 7;

long duration, cm;

void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  noInterrupts ();
  cm = microsecondsToCentimeters(duration);
  interrupts ();
  delay(50);
}

long microsecondsToCentimeters(long microseconds)
{
  return (microseconds / 29 / 2);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write(cm); 
}

I've made "cm" a global variable, so you can access it in requestEvent. There is still the issue that Wire.write will only write a single byte. That will be OK if the number of cm is an integer (no decimal places) and is in the range 0 to 255.

Thank you Nick

It works! it works!
(doing the it-works-dance).

I2C boogity boogity

Heres the slightly modified master reader sketch from the wire libray examples, the slave ping code i got off the net, i'll try and find the original , which has inches to seconds conversion and is fully commented and may has an author. The ultrasonic sensor is from radio shack; 3 pin (plugs into breadboard). the master sketch delay probably has to be adjusted down to get more pings. Also if you're using processing to read serial you can multiply by n the final result of the ping calculation - return (microseconds / 29 / 2)*n;
if you need to scale it and dont know how in processing. the slave code is above(posted by nick), heres the master incase you were wondering.
Thanks again to Nick

Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#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, 1);    // request 1 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c,DEC);
    Serial.print("\t");    // I tab the data so i can copy and import 
    //to labview and  see it on strip chart, wavegraph etc
  }

  delay(500);
}