Go Down

Topic: Arduino as a timing device using light beams (Read 1 time) previous topic - next topic

cowasaki

2 x £1.99 laser pointers off ebay.  Disassemble them to remove the laser diodes and attach two wires then use these as the beams!  You can still use an LDR as the sensor with the same circuit ie BC547 or 2N2222 transistor with Emitter to GND then 10K + 10K pot between Base and GND and the LDR between Base and VCC.  Connect the collector to VCC via an LED and a resistor (220 for 5V).  You can then adjust the sensitivity with the pot with the arduino connected to the collector on each circuit.


With the above you simply need some very simple code.

so connect the first circuit to pin 2 and the second to pin 3.

while (digitalRead(2) == LOW) {
// do nothing
}
int first = millis();
while (digitalRead(3) == LOW) {
// do nothing
}
int timetaken = millis()-first

speed is a product of distance and time so the above can also give you the speed if you accurately measure the distance.


robtillaart

#6
Jun 15, 2011, 09:30 am Last Edit: Jun 15, 2011, 09:32 am by robtillaart Reason: 1
millis() returns an unsigned long, an int would overflow within a minute....

Code: [Select]
unsigned long measureTime(uint8_t pinA, uint8_t pinB)
{
 while (digitalRead(pinA) == LOW) ;
 unsigned long first = millis();
 while (digitalRead(pinB) == LOW);
 unsigned long timetaken = millis()-first;
}


Another approach would be IRQ based, leaves room for the Arduino to do something while waiting....

Code: [Select]
volatile unsigned long first = 0;
volatile unsigend long second = 0;

void IRQA()
{
 first = millis();
}

void IRQB()
{
 second = millis();
}

void resetTime()
{
 first = second = 0;
}

unsigned long lapTime()
{
 return millis() - first;
}

unsigned long finalTime()
{
 if (second > 0) return second - first;
 return 0;
}


or use - http://www.arduino.cc/playground/Code/StopWatchClass -

Code: [Select]
volatile unsigned StopWatch MySW;

void IRQA()
{
 MySW.start();
}

void IRQB()
{
 MySW.stop();
}


my 2 cents
Rob
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

cowasaki


millis() returns an unsigned long, an int would overflow within a minute....

my 2 cents
Rob



I actually wrote pseudo code then realised that it was nearly C so just re-wrote it slightly and hadn't even thought about the size of int.  The same code with "long" in front would do it though :)

I am quite new to the Arduino and have not seen lots of things people keep coming up with like the stop watch class!  All very useful stuff.

robtillaart


Spend one evening reading through the playground and you will get enough ideas for a year of happy tinkering, probably even more ;)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up