Interfacing with a digital IR reflectance sensor

I recently acquired some Pololu QTR-1RC sensors (digital sensors), and I need to get readings from them. I tried using the Pololu libraries but the pins in the library don’t exactly mesh with my arduino mega pins. So I decided to try the other way:

The typical sequence for reading a sensor is:

Set the I/O line to an output and drive it high
Allow at least 10 us for the 10 nF capacitor to charge
Make the I/O line an input (high impedance)
Measure the time for the capacitor to discharge by waiting for the I/O line to go low

The time it takes for the capacitor to discharge is the value I need to read.
I need help fleshing out how this is going to exactly work with my Arduino Mega…Here’s the code I have so far:


int sensorpin = 37;
int time = 0;
//to hold the time it took for the cap to discharge
void setup() {

Serial.begin(9600);
// For Printing the time it took for the cap to discharge to me
}

void loop() {

pinMode(sensorpin, OUTPUT);
digitalWrite(sensorpin, HIGH);
delayMicroseconds(10);
pinMode(sensorpin, INPUT);

//My question here is that do I need to set the digital I/O pin to LOW even though I switched it to INPUT to get the effect I need?
digitalWrite(sensorpin, LOW);

// I am stuck on how to count the time elapsed of the I/O pin going from HIGH to LOW. I know it will need a digitalRead(); and possibly a millis(); or micros();, though I do not know how to put it together.
(pseudo-code){
while(!digitalRead(sensorpin) == “HIGH”) {
time++;
or
micros(); ?
}
}
Serial.print(time);

}

Thoughts? Sorry if its a bit hard to understand, this is my first post, and I will clarify again if needed tomorrow morning (getting late).

I found a good tutorial on this for you... http://www.pololu.com/docs/0J13/1

SensorPin = 37?... Sounds like you are using the IC's pin instead of the digital pin numbers given for the arduino (1..13)

My question here is that do I need to set the digital I/O pin to LOW even though I switched it to INPUT to get the effect I need?

->No

For the next bit, you need to keep the time from when you make the output low, to when low is read.

There are lots of ways of doing this, but I'm not going to spoil your creativity :)

Thanks, I'm using the Arduino Mega, it has a lot more digital pins, and 37 is one of them. I have a question about the micros and millis functions....do they start counting when they are called or do they count from the beginning of the entire Arduino program when called? Like when the setup() code was executed even though the millis() function is in the loop(), will the millis function somehow count the time starting at setup() or will it just count from within its own method? My reasoning for this is because the page on these functions here define them as starting to count when the program starts...does this refer to the entire program or just the program of the function itself?

Looks like you are on the right track!

Here is millis() explained: http://www.arduino.cc/en/Reference/Millis

Basically it starts when your program does.