Go Down

Topic: Driving LEDs and sensing at the same time (Read 725 times) previous topic - next topic


I am working on a project where I drive an LED and want to detect (using piezo sensors) whether a person sets off the sensor relatively close to when that LED went on. Think of it as whack-a-mole, and I am trying to detect if someone did actually whack the mole pretty fast. How do I do this considering there is no native multi-threading supported on the Arduino?


I guess this deppends on how many LEDs you're using. If you're using just a few, then you can poll them at regular, or almost regular intervals as fast as possible and use the time between analogue reading to perform any other operation.
Take in considaration that although the human hand may be able to move pretty fast (pressing a button as fast as you can would still take dozens of millis), it's still pretty slow for a microcontroller with a 16MHz clock which can give you a resolution of microseconds, so I wouldn't be too concerned about the lack of multihreading.
One problem could be that you may need to do A LOT of things while you're sensing so you can't keep a reliable timing. For this case you could use a timer interrupt which would let you run any code and every "N" milliseconds would execute a function (like the sensing of your LED). There are no native arduino functions for that but I found a library that seems pretty easy to use for timer interrupts: http://www.arduino.cc/playground/Main/MsTimer2
I hope this helps!


If you wrote this code (with the appropriate definitions)
Code: [Select]

void setup(){
digitalWrite (ledPin, HIGH);
startTime = millis();

void loop(){

if (digitalRead(button) == LOW){
endTime = millis();
digitalWrite(ledPin, LOW);
SerialPrint (endTime - startTime);

and press Reset to start the program, I think you wlll surprised at just how big of a number you get back even if you are pretty fast.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up