help with measure speed of object. photo-interrupter

thanks everyone for your replies. i have found them very useful.
i might learn abit more about assembly after this project.
is there a way to view assembly codes compiled by the arduino IDE?
i assume that the IDE compiles the arduino language into regular machine code.

this is my code so far. it works 95% of the time.
sometimes it misses, not sure if its hardware prob. (freq response 28usec)
sensor im using is a TI OPT101
in the next few days i’ll probably add some sort of debounce technique just to be safe.
and also optimize and use ADC.

byte last_input1;
byte last_input2;

void setup(){
Serial.begin(9600); //code starts from here when Serial Monitor is turned on
pinMode(2, INPUT);
pinMode(3, INPUT);
// pinMode(7, OUTPUT);
// pinMode(12, OUTPUT);
}

void loop(){
int count=0;
unsigned long time1[10]={
0,0,0,0,0,0,0,0,0,0 };
unsigned long time2[10]={
0,0,0,0,0,0,0,0,0,0 };

Serial.println(“Measuring Now”);
//make 5 measurements using polling method

while(count < 5) {

if ((digitalRead(2) != last_input1) && (digitalRead(2)== LOW)) {
time1[count] = micros();
//cannot detect anything else when executing this line of code
}
last_input1 = digitalRead(2);

if ((digitalRead(3) != last_input2) && (digitalRead(3) == LOW)) {
time2[count] = micros();
count++;
//cannot detect anything else when executing this 2 lines of code
}
last_input2 = digitalRead(3);

}

for(int i=0; i<5; i++){
Serial.println("----------");
Serial.println(i);
Serial.println(time1*);*
_ Serial.println(time2*);_
_
}_
_ Serial.println(“done.”);_
_
}_
_
[/quote]_
serial monitor:
_
> Measuring Now*_
> ----------
> 0
> 33890956
> 34153560
> ----------
> 1
> 35454352
> 35503152
> ----------
> 2
> 38797916
> 38845356
> ----------
> 3
> 39881408
> 39930036
> ----------
> 4
> 42212780
> 42265348
> done.

//code starts from here when Serial Monitor is turned on

No, it doesn’t.

if ((digitalRead(2) != last_input1) && (digitalRead(2)== LOW)) {
time1[count] = micros();
//cannot detect anything else when executing this line of code
}
last_input1 = digitalRead(2);

All three of these digitalRead() calls can return different values. This is probably not what you want.

The comment, while true, is completely useless.

  //make 5 measurements using polling method

Why?

in the next few days i’ll probably add some sort of debounce technique just to be safe.

You already have that…

and use ADC.

For what?

All three of these digitalRead() calls can return different values. This is probably not what you want.

what im trying to do here is to detect the 'falling edge' of the input. by putting 'digitalRead()' into 'last_input', 'last_input' will always be 1 clock cycle behind 'digitalRead()' if the last input was different from the current input, meaning theres a change in signal. and if the current signal is 'LOW' and it just experienced a change, it is a 'falling edge'.

or are u suggesting that i should put digitalRead() into a variable so that they are all the same value?

//make 5 measurements using polling method

Why?

just for lols

in the next few days i'll probably add some sort of debounce technique just to be safe.

You already have that...

debounce as in, if the signal constantly changes to quickly, i do not want that to register as multiple inputs.

ADC

I am using a sensor that spits out voltage from 0V to 5V depending on light intensity. right now the laser light causes the sensor to produce about 4.5V, which is considered a HIGH in arduino. i might change the laser to something less powerful, like a led or something. so i would like to adjust the HIGH LOW threshold without additional hardware if possible.

thanks for your interest on my project.

If you decide to use the ADC, remember that an analogRead takes around 104 microseconds.
You may be better off fixing the detection threshold for a digitalRead, or port manipulation which will be much faster.

Instead of this:

    if ((digitalRead(2) != last_input1) && (digitalRead(2)== LOW)) {
      time1[count] = micros();
      //cannot detect anything else when executing this line of code
    }
    last_input1 = digitalRead(2);

try this:

    int temp = digitalRead(2);
    if ((temp != last_input1) && (temp == LOW)) {
      time1[count] = micros();
      //cannot detect anything else when executing this line of code
    }
    last_input1 = temp;

Similarly for the bit that monitors pin 3. This avoids the problem that the reading could change within those lines of code and is also faster.

dc42: yes i will do that.

AWOL: i wasnt aware that i can do port manipulation. i will look into that. thanks.