Analog Read

Hi All,

i am driving one of digital pins to turn on an IR led, and using a one of analog pins on Mega to read the photodiode, as my proximity sensor...

Now i want to turn on the IR led for 200 microsecs and then turn off... would that gives me valid reading from Analog input.. i am not sure about timming of ADC....

Thanks in Advance....

Try it and see.

  delay(.2);

Yeah, right. The useless delay() function takes an unsigned long - an integer value. A delay(0) is even more useless!

if (val>= limit )
{

  digitalWrite(led,LOW);
  delay(20);
}

if (val<limit)
{
 digitalWrite(led,HIGH);
  delay(20);

}

If val is not greater than or equal to limit, what is the possibility that it is not less than limit? Less than a snowball’s chance in hell. Replace the second if statement with an else.

Frankly, I don’t see that the time that the output pin is HIGH has anything to do with what you read from the input pin, as long as the output pin IS high when you are reading, which appears to be the case.

but going back to original question, i do need to take the readings only for 200us when the IRled is on.....

If you want to take readings whilst the IR LED is on you cannot use delay() or delayMicroseconds() because you cannot do anything else whilst the delay is happening.

Time to look at the technique used in the BlinkWithoutDelay example in the IDE methinks.

Turn on the IR LED, take readings until 200us has elapsed (the micros() function will come in handy here) then turn off the IR LED

else if (val<limit)

I can see you’re really worried about the poor snowball.

Please use code tags when posting code.

but going back to original question, i do need to take the readings only for 200us when the IRled is on…

The reading shouldn’t change. There is light, or there isn’t, at the time you read. It won’t change while the IR LED is on. Unless there is something you aren’t telling us. The IR LED isn’t being modulated, so it isn’t going on and off at any frequency you are going to able to infer anything from.