QTR-1RC reflectance sensor simpler code

Hey everybody!
Working at a project that implied huge amount of sensors on a mini rover, i stumped upon the problem of insuficient memory for my code. One of those sensors was QTR-1RC reflectence sensor, used to read the speed of the rover by reading the alternance of a black and white grid sticked on the wheels:

Initialy i was using the QTRSensors libry from pololu:GitHub - pololu/qtr-sensors-arduino: Arduino library for the Pololu QTR reflectance sensors and using this library the sketch just for the qtr had 5,278 bytes. After optimizing it and geting rid of the library, the sketch 3,318bytes. I know its not big difference but for my project every byte counted.
So, how did I got rid of the library? I just read how this sensor works and code it myself.
Firstly, what is qtr-1rc? The Pololu QTR-1RC reflectance sensor carries a single infrared LED and phototransistor pair. The phototransistor uses a capacitor discharge circuit that allows a digital I/O line on a microcontroller to take an analog reading of reflected IR by measuring the discharge time of the capacitor. Shorter capacitor discharge time is an indication of greater reflection.

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

So it's that simple, i just followed the steps above and done.
Here is the code i used:

unsigned long time;
int color=1,last,last_color;
void setup()
{
 Serial.begin(9600);
}
void loop()
{
 pinMode(9,OUTPUT);
 digitalWrite(9,HIGH);
 delayMicroseconds(12);
 pinMode(9,INPUT);
 time=micros();
 while(digitalRead(9)) {}
// Serial.println(micros()-time);
  
  if((micros()-time)>last && color==1) color=-1;
      else if((micros()-time)<last && color==-1) color=1;
   
    if(color!=last_color) Serial.print(color),Serial.println(" ");
    last=micros()-time;
    last_color=color;
}

This code returns 1 for white and -1 for black.
If you are not working with black and white and you want the response time value use this code that has 2,948bytes:

unsigned long time;
void setup()
{
 Serial.begin(9600);
}
void loop()
{
 pinMode(9,OUTPUT);
 digitalWrite(9,HIGH);
 delayMicroseconds(12);
 pinMode(9,INPUT);
 time=micros();
 while(digitalRead(9)) {}
 Serial.println(micros()-time);

}

Just remember that lower value it's higer reflectence.
I hope this is useful to you:D

thanks for sharing,

some small remarks:
the code contains at least one possible bug as the value for last is not explicitly specified for the first iteration.

if the timing is in the low microseconds one might consider direct port manipulation to be faster than digitalRead()..
also one might use a hardware timer instead to get more precise timing as micros() goes in steps of four at a time..

can this method be used to measure shaft rpm? i has a black motor to which i can place a white strip of paper, tape etc.