0

Quick question that I had a hard time finding in the documentation.

I am trying to do a while loop that will last for an specific amount of time (like 500ms). How quickly could it do one loop? Every loop will not do a big task, but just read if the light intensity of a sensor is higher than 630nm.

could it work to put every iteration as 1ms?

I am writing in NXC, which is pretty similar to C, I guess...

Diaco
  • 241
  • 1
  • 3
  • 15
  • 2
    How about to use a timer? – LPs Oct 01 '15 at 09:34
  • @LPs came up with that idea too, but still the answer to this question is interesting to me. – Diaco Oct 01 '15 at 09:35
  • How could you thing on an answer? At least post the MCU speed, The environment, and so on.. – LPs Oct 01 '15 at 09:38
  • 1
    "The light intensity of a sensor is higher than 630 nm" makes no sense at all. Light intensity is not measured in nanometers. – unwind Oct 01 '15 at 09:41
  • 630 nm is RED light ;) – LPs Oct 01 '15 at 09:44
  • Sorry, I dont know where that came from lol..... What we are actually doing is we have a light sensor that will see the difference in color. white and black. – Diaco Oct 01 '15 at 10:02
  • As for the MCU Speed. Lets say I have a normal computer in a normal enivronment. (we are using a device that I am not sure about the processor of) – Diaco Oct 01 '15 at 10:03

1 Answers1

1

What you have to do is basically check if the difference between the current system time and the system time at the start is less than your desired value(500ms in this case). As far as how quickly it could do one loop, that depends entirely on your implementation. You could keep track of the time taken for each loop, probably print it for your verification.

As you might be aware currentTick() returns the current system time in milliseconds. So here's what your code should look like:

unsigned int start, current;
start = currentTick();
current = start;
while(cur < (start+500))
{
    //Your task.
    current = curTick();
    printf("Time: %u",current); 

}
Pooja Nilangekar
  • 1,419
  • 13
  • 20
  • Thank you. In the end, this was the way I went about doing my loop. It was the best solution. – Diaco Oct 06 '15 at 18:20